cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple definition error but "load once"-#ifndef should catch it...

Tobe
Senior III

How is this possible with the #ifndef catch?

no its not.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

If you have multiple compilations units (i.e. multiple *.c files), this gets included from each of them (only once). When the linker tries to resolve everything at the end, it sees multiple definitions of ledState.

Solution: don't put variables definitions in header files. Put them in source files. If you need them in multiple source files, additional put an "extern" declaration in the header.

In a header file:

extern bool ledState;

 

In one source file:

bool ledState = false;
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
TDK
Guru

If you have multiple compilations units (i.e. multiple *.c files), this gets included from each of them (only once). When the linker tries to resolve everything at the end, it sees multiple definitions of ledState.

Solution: don't put variables definitions in header files. Put them in source files. If you need them in multiple source files, additional put an "extern" declaration in the header.

In a header file:

extern bool ledState;

 

In one source file:

bool ledState = false;
If you feel a post has answered your question, please click "Accept as Solution".