Why aren’t my compile guards preventing multiple definition inclusions?

If the linker is complaining, it means you have definitions rather than just declarations in your header. Here’s an example of things that would be wrong.

#ifndef X_H
#define X_H

int myFunc()
{
  return 42; // Wrong! definition in header.
}

int myVar; // Wrong! definition in header.

#endif

You should split this into source and header file like this:

Header:

#ifndef X_H
#define X_H

extern int myFunc();

extern int myVar; 

#endif

C Source:

int myFunc()
{
  return 42; 
}

int myVar; 

Leave a Comment