What is the best way to compile these dependencies?

I have three files I want to compile: Object.h, Object.cpp, Play.cpp.

Both the .cpp files call #include "Object.h'.

Currently, I'm attempting to compile them this way:

1
2
3
4
5
./Build/Play.exe: ./src/Play.cpp ./Build/src/Object.o
	g++ -Wall ./src/Play.cpp ./Build/src/Object.o -o ./Build/Play.exe

./Build/src/Object.o: ./src/Object.cpp ./src/Object.h
	g++ -c -Wall ./src/Object.cpp -o ./Build/src/Object.o


but I get an error stating that I have multiple definitions of a function in Object.h. I chose to compile this way because Play depends on Object, so I should compile Object first, then compile/link it to Play. My guess is that when Play.exe compiles, it compiles Object.h, and sees multiple definitions of a function since Object.h has been compiled in Object.o. Am I correct, and if so, how can i fix this problem?
Last edited on
Headers should not be compiled.
I fixed this problem by implementing the function in the Object.cpp file rather the Object.h file.
Try declaring the function inline in Object.h.

Topic archived. No new replies allowed.