Skip to main content
Tobe
Senior III
December 21, 2023
Solved

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

  • December 21, 2023
  • 1 reply
  • 2544 views

How is this possible with the #ifndef catch?

no its not.jpg

This topic has been closed for replies.
Best answer by TDK

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;

1 reply

TDK
TDKBest answer
Super User
December 21, 2023

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""."