cancel
Showing results for 
Search instead for 
Did you mean: 

Making variables visible to a header file

skon.1
Senior

Hello,

I'm using a STM32CubeIDE.

A lot of the in the main.c file is auto generated.

For example:

UART Handlers, DMA Handlers etc.

I also have a header file where I wrote a lot of user custom functions inside a file named: custom.h

The functions in this header file make use of the auto generated variables from main.c

What's the best way to make the auto generated variables from main.c visible to the functions at custom.h ?

6 REPLIES 6
TDK
Guru

One way is to include their definitions as extern in main.h and #include it.

For example, if "uint32_t variable" is in main.c, put this in main.h:

extern uint32_t variable;

You could also just put this statement in custom.h on its own.

If you feel a post has answered your question, please click "Accept as Solution".
skon.1
Senior

Thanks,

The variables whose scope I want to extend are autogenerated by the IDE.

Let's see if I understand your suggestions:

  1. The first one is simply writing the word "extern" before each auto generated variable. Right ?
  2. The second suggestion is simply to copy the auto generated variables also to the custom header file. Correct ?

Put prototypes with extern into the .H file, put the actual definitions in ONE .C file.

If you put the definitions in the .H file, and include it in multiple other source files you'll get "multiple definition" errors.

This is C, standard rules/methods apply.

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

But I don't want to modify the auto-generated code...

There are user sections where you could add "extern uint32_t ..." in the header.
If you feel a post has answered your question, please click "Accept as Solution".
skon.1
Senior

I don't understand you suggestion. The variables are already declared in the auto-generated code section. Do you suggest I declare the same ones again using "extern" in the user sections ??