Making variables visible to a header file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 12:19 AM
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 ?
- Labels:
-
STM32CubeIDE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 6:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 9:49 AM
Thanks,
The variables whose scope I want to extend are autogenerated by the IDE.
Let's see if I understand your suggestions:
- The first one is simply writing the word "extern" before each auto generated variable. Right ?
- The second suggestion is simply to copy the auto generated variables also to the custom header file. Correct ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 10:18 AM
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 10:57 AM
But I don't want to modify the auto-generated code...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 11:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-09-24 2:19 PM
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 ??
