cancel
Showing results for 
Search instead for 
Did you mean: 

Execution error when I declare many extern variable.

David Soares
Associate II

I'm working with a STM32F407 and CubeIDE. My program is divided in various small .c files. This cause a lot of extern variables declarations. Recently the programm presents erronious behavior when I create a new extern variable declaration, but with no compiler error. There is some limitation in the number of declared extern variables?

5 REPLIES 5
gbm
Lead III

Make sure you put all extern declarations in .h files and include the .h file in the .c file in which the variable is defined. There is no seriuos limit on the number of vars but it's quite easy to have two different declarations of the same variable, which, if you don't stick to the rule above, may cause hard to detect errors.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Check occupied memory in the mapfile. Do you have an estimate of stack consumption?

JW

FBL
ST Employee

Hello @David Soares​,

There is no limitation in the number of declared extern variables. The issue with the erroneous behavior is likely due to another problem, such as memory overflow or improper linking of the extern variables between the .c files.

You may check the map file as mentioned by waclawek.jan ensuring that the extern variables are properly defined in memory regions, You may also check the order of linking the .c files.

Hope this helps!

Firas

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

David Soares
Associate II

Dear Fellows, I find the cause of this problem.

I've declared my variable as int in main.c, but as float in the file that I uses it.

When I corrected the declarations, the problem is gone. What makes me surprised was the fact that the compiler did not point out any errors or warnings messages due to the inconsistency of the declarations.

Best regards to all.

I don't think the symbol in the object file infers any TYPE information, C++ uses name mangling of the symbols for function to denote the type of input/output parameters.

If you used extern float foo; in a .H file that ALL sources used, it would probably have complained where you actually defined in variable in the .C file

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