Skip to main content
skon.1
Associate III
September 24, 2020
Question

Making variables visible to a header file

  • September 24, 2020
  • 6 replies
  • 2519 views

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 ?

This topic has been closed for replies.

6 replies

TDK
September 24, 2020

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
skon.1Author
Associate III
September 24, 2020

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 ?

Tesla DeLorean
Guru
September 24, 2020

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
skon.1
skon.1Author
Associate III
September 24, 2020

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

skon.1
skon.1Author
Associate III
September 24, 2020

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 ??