1

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.

Googe
  • 53
  • 2
  • 7
  • 1
    Not a makefile problem. Variable declarations in include files, variable definitions in .cpp files (unless they are const or maybe static). – Avi Berger Nov 15 '16 at 06:16
  • Thank you very much! Defining the compass directions as static variables solved it. – Googe Nov 15 '16 at 06:29
  • 1
    I qualified static with "maybe" as it is normally NOT a good solution. I can't think of a case that I would actually recommend that. In most cases it will likely cause other problems. Unless you are dealing with true constants, you should almost always go with a declaration (extern) in the header file with a definition in one .cpp. That is, if you really, truly need a global variable, – Avi Berger Nov 15 '16 at 06:43
  • 1
    And if you are using true constants, then do use const. – Avi Berger Nov 15 '16 at 06:45
  • Okay, I took your advice, and moved the definition into the .cpp file. It actually works better. Thank you very much @AviBerger. – Googe Nov 17 '16 at 04:22
  • As of C++17, use `inline` on your constants to allow defining them in the header and avoid certain subtle traps if you take references to them (*e.g.*, by calling `std::min` on one). – Davis Herring May 15 '20 at 20:58

0 Answers0