cancel
Showing results for 
Search instead for 
Did you mean: 

Generated private variables in main.c

neel1311
Associate

In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". Since these variables are declared only in main.c and not in main.h, does that mean that the use of these variables in other modules is not encouraged? 
If I need to use them in other modules, can I declare these variable as extern in main.h or is there a better way to handle this?

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions

@neel1311 wrote:

In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". !


Please show that code.

See: How to insert source code.

In the C language, variables (and functions) defined at file scope are public unless specifically qualified as static

 

EDIT:

You mean stuff like this:

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32g0xx_nucleo_32.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

That comment is actually misleading.

As noted above, the hadc1 definition there is public - because it is not qualified as static.

So, as @Karl Yamashita said, you can provide your own extern declarations anywhere you want to use it - either in your own header file, or in a USER section of one of the auto-generated headers.

If you configure the project to generate separate .c & .h files for peripheral initialisation:

AndrewNeil_0-1734432078943.png

The extern declarations for the peripheral handles will be in the generated headers; eg,

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern ADC_HandleTypeDef hadc;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

 

 

 

 

 

View solution in original post

3 REPLIES 3
Karl Yamashita
Lead III

You can use them as extern. If the variables are not meant to be used outside of the file then it would be declared static.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

You typically don't define variables in .H files, you provide prototypes, extern for those you wish to pull into multiple source files, so as not to get "multiple definition" errors.

You can define and share these in ONE .C file, or make them static to hide them from the global name-space.

Often you'd want to share peripheral instances with stm32xyz_it.c so you could keep all the IRQHandler over there, but HAL/CUBE generally just does what it wants.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@neel1311 wrote:

In the generated code in main.c file, there are variables that are declared as "private variables" by the software, such as "ADC_HandleTypeDef hadc1"; "TIM_HandleTypeDef htim1". !


Please show that code.

See: How to insert source code.

In the C language, variables (and functions) defined at file scope are public unless specifically qualified as static

 

EDIT:

You mean stuff like this:

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32g0xx_nucleo_32.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

That comment is actually misleading.

As noted above, the hadc1 definition there is public - because it is not qualified as static.

So, as @Karl Yamashita said, you can provide your own extern declarations anywhere you want to use it - either in your own header file, or in a USER section of one of the auto-generated headers.

If you configure the project to generate separate .c & .h files for peripheral initialisation:

AndrewNeil_0-1734432078943.png

The extern declarations for the peripheral handles will be in the generated headers; eg,

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern ADC_HandleTypeDef hadc;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */