I have the following file structure:
X.h
#pragma once
#include "Y.h"
int ONE = 1;
int combine();
X.c
#include "X.h"
int combine()
{
return ONE + TWO;
}
Y.h
#pragma once
int TWO = 2;
Y.c
#include "Y.h"
Main.c
#include "X.h"
int main()
{
int fusion = combine();
return 0;
}
I get the following errors:
LNK1169 one or more multiply defined symbols found
LNK2005 _ONE already defined in Main.obj
LNK2005 _TWO already defined in Main.obj
KLNK2005 _TWO already defined in Main.obj
This makes zero sense. If we start at Main.c
the compiler must include X.h
. The compiler then looks for the C file associated with X.h
. Inside X.c
it needs to include X.h
but #pragma once
SHOULD guard against that. Then it needs to include Y.h
. It looks for the C file and finds Y.c
which says to include Y.h
, but Y.h
has already been included. Then, it returns to Main.c and should successfully compile... but nope.
I can add /FORCE
to my project settings which makes my code run perfectly but still outputs the:
ONE has already been defined, second definition ignored.