cancel
Showing results for 
Search instead for 
Did you mean: 

Using STM32412KBT6, gives a error when trying to extern ENUM

bn.1
Associate

I have defined the enum in lets say x.h file so i can use it in x.c file , but need to use the same enum in y.c so included x.h in y.h and tried to extern it in the same file was give out error, but was working when i extern the same in y.c file. why such problem occuring could anyone explain , used to work with nordic this error never occurred there .I have use typedef to define the enum.

IN x.h

typedef enum

{

   NORMAL,

   ABNORMAL,

   EXTREME

}temp_mode_e;

IN y.c

extern temp_mode_e temp_mode;

(same when declared in y.h gives error)

4 REPLIES 4
KnarfB
Principal III

A global variable like

temp_mode_e temp_mode;

shall be defined in exactly one .c file. The type must be known and a type definition used in more than one .c file is usually in an included .h file.

If you want to use the same variable in more than one .c fie, define it external in all but one .c files.

> this error never occurred there

You don't give the error message, so I can only guess: Some C constucts result in unspecified/undefined/implementation-defined behaviour and what happens depends on the exact compiler type, version and flags used. Try to use the most pedantic compiler settings possible to get the most portable code.

hth

KnarfB

gbm
Lead III

Put

extern temp_mode_e temp_mode;

in the .h file (the same in which the enum is defined), put

temp_mode_e temp_mode;

in a single .c file, which includes the .h file.

Do not put extern declaration in a .c file; it's not forbidden but it may make your life harder.

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

-->Do not put extern declaration in a .c file; it's not forbidden but it may make your life harder.

Currrently i am externing the enum variable in source file, could you tell me what problems can occur in such case ?

--->Put extern temp_mode_e temp_mode; in the .h file (the same in which the enum is defined),

But the problem with this is that, another developer who reads the code won't be able to trace back from where the enum variable was defined/declared, so for the ease of reading I have stopped putting them there.

gbm
Lead III

By putting extern in a .c file you risk that when the definition of a variable changes where it is defined, your single file will see it as it used to and you will not get any warning from the compiler on that matter. The whole art of C programming is to allow the compiler to detect as many errors as possible.

I don't uderstand your second remark. If an object defined in abc.c is publicly available, then you should create abc.h containing its extern declaration, then include abc.h in abc.c and any other .c file from which you want to reference the object. An object declared in abc.h is supposed to be defined in abc.c. These are just conventions, not language requirements, but very useful ones.

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