I have four C++ files that i'm trying to compile. Two are .h and two are .cpp. I created a makefile that produces two objects and compiles those with g++. There's an int defined in one of the .h files that the linker insists is defined twice. What am I doing wrong? These are the source files:
"foo.cpp"
#include "foo.h"
#include "bar.h"
int main ()
{
X();
}
"foo.h"
#ifndef j
#define j
bool a;
#endif
"bar.cpp"
#include "bar.h"
int X()
{
b = 0;
return b;
}
"bar.h"
#ifndef k
#define k
int b;
int X();
#endif
"Makefile"
exec: foo.o bar.o
g++ -oexec foo.o bar.o
foo.o: foo.cpp foo.h
bar.o: bar.cpp bar.h
This is the compile error I get:
/usr/bin/ld: bar.o:(.bss+0x0): multiple definition of `b'; foo.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
P.S. No, this isn't part of an assignment. I've made a simplified example for this post.