I'm trying to write more sophisticated makefiles, but my explorations have gotten me into a multiple definition issue.
The program is meant to brute-force a marble maze, so I have a class for the game board declared in marbleBoard.h, with its implementation written in marbleBoard.cpp. The .cpp file #includes the .h file. The main program is written in marble.cpp. The Variables NORTH, SOUTH, EAST, and WEST refer to bit masks that symbolize walls, and are defined in marbleBoard.h outside of the class.
I based my makefile on MrBook's tutorial Makefiles.
Here is the Makefile code:
#!/bin/bash
COMPILER=g++
CFLAGS=-c -g -std=c++14
all: marble
marble: marble.o marbleBoard.o
$(COMPILER) marble.o marbleBoard.o -o marble
marble.o: marble.cpp
$(COMPILER) $(CFLAGS) marble.cpp
marbleBoard.o: marbleBoard.cpp marbleBoard.h
$(COMPILER) $(CFLAGS) marbleBoard.cpp marbleBoard.h
clean:
rm *o marble
This is the output I recieve:
[uthranuil@Palantir marble]$ make
g++ -c -g -std=c++14 marbleBoard.cpp marbleBoard.h
g++ marble.o marbleBoard.o -o marble
marbleBoard.o:(.data+0x0): multiple definition of `NORTH'
marble.o:(.data+0x0): first defined here
marbleBoard.o: In function `marbleBoard::marbleBoard()':
/home/uthranuil/csci242/marble/marbleBoard.cpp:6: multiple definition of `EAST'
marble.o:(.data+0x1): first defined here
marbleBoard.o: In function `marbleBoard::marbleBoard()':
/home/uthranuil/csci242/marble/marbleBoard.cpp:6: multiple definition of `SOUTH'
marble.o:(.data+0x2): first defined here
marbleBoard.o: In function `marbleBoard::marbleBoard()':
/home/uthranuil/csci242/marble/marbleBoard.cpp:6: multiple definition of `WEST'
marble.o:(.data+0x3): first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:10: marble] Error 1
I have spent a lot of time looking for answers and trying different solutions. Here is a list of Q/As I have read:
Function multiple definition linker error building on GCC - Makefile issue
Makefile: multiple definition and undefined reference error
Makefile: multiple definition of TMC_END
Error in makefile: multiple definition of _start
multiple definition errors when using makefile
Any help is greatly appreciated. Thank you for your time and attention.