我正在尝试编写更复杂的makefile,但是我的探索让我陷入了多重定义问题。
该程序旨在强制使用大理石迷宫,因此我在marbleBoard.h
中声明了一个游戏板类,其实现用marbleBoard.cpp
编写。 .cpp
文件#includes
.h
文件。主程序用marble.cpp
编写。变量NORTH
,SOUTH
,EAST
和WEST
指的是代表墙的位掩码,并在类外的marbleBoard.h
中定义。
我将我的makefile基于MrBook的教程Makefiles。
这是Makefile代码:
#!/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
这是我收到的输出:
[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
我花了很多时间寻找答案并尝试不同的解决方案。这是我读过的Q / As列表:
Function multiple definition linker error building on GCC - Makefile issue {
{3}} {
{3}} {
{3}} {
{3}}
非常感谢任何帮助。感谢您的时间和关注。