1

我不断收到错误

make: *** No rule to make target `all'.  Stop.

我确信我的 makefile 可以完美运行,因为我确实在终端上运行了它并且一切正常。但是当我尝试通过创建一个空的 Makefile 项目将所有内容导入 eclispe 时,我无法编译该程序。那么我错过了eclipse配置中的一些东西吗?

无论如何,这是我的makefile,请看一下,并纠正我。谢谢

CC = g++
prog: legrec.o game.o board.o piece.o 
    $(CC) legrec.o game.o board.o piece.o -Wall -Werror -pedantic -o legrec
legrec.o: legrec.cpp game.h
    $(CC) -Wall -Werror -pedantic -c legrec.cpp
game.o: game.cpp game.h board.h piece.h move.h player.h
    $(CC) -Wall -Werror -pedantic -c game.cpp
board.o: board.cpp board.h piece.h move.h player.h
    $(CC) -Wall -Werror -pedantic -c board.cpp
piece.o: piece.cpp piece.h board.h move.h player.h
    $(CC) -Wall -Werror -pedantic -c piece.cpp

编辑:感谢所有回复,我确实将第一行更改为 all:legrec,之前的错误消息消失了,但是又出现了另一个错误

     cc   legrec.o   -o legrec
 Undefined symbols for architecture x86_64:
    "game::game()", referenced from:
         _main in legrec.o
    "game::printMenu()", referenced from:
         _main in legrec.o
    "game::printBoard()", referenced from:
         _main in legrec.o
    "game::nextMove()", referenced from:
         _main in legrec.o
    "game::ended()", referenced from:
         _main in legrec.o
    "game::printWinner()", referenced from:
         _main in legrec.o
    "game::~game()", referenced from:
         _main in legrec.o
    "std::terminate()", referenced from:
         _main in legrec.o
    "std::ios_base::Init::Init()", referenced from:
         __static_initialization_and_destruction_0(int, int)in legrec.o
    "std::ios_base::Init::~Init()", referenced from:
         ___tcf_0 in legrec.o
    "___gxx_personality_v0", referenced from:
         Dwarf Exception Unwind Info (__eh_frame) in legrec.o
 ld: symbol(s) not found for architecture x86_64
 collect2: ld returned 1 exit status
 make: *** [legrec] Error 1

我只是不明白为什么运行相同的平台但程序执行不同。在我在终端上运行并在那里进行编辑之前,这看起来非常好,但是在移植到 Eclipse 之后,它让我对奇怪的错误感到疯狂。

4

3 回答 3

1

Your very first rule is not really good.

You could rename it to all and it would "work". But a better approach would be:

all: legrec

legrec: legrec.o game.o board.o piece.o 
    $(CC) legrec.o game.o board.o piece.o -Wall -Werror -pedantic -o legrec

i.e. the rule name should match the output produced.

If you type just make on the command line, the very first rule encountered is run (that's why it works for you). I'm guessing your IDE is running make all, and you haven't defined such a rule.

于 2012-05-17T10:30:57.197 回答
0

我不是专家,但我会尽力提供帮助。将 maketarget 重命名proglegrec" 尝试添加以下行
.PHONY: all clean
all: legrec

Also your makefile doesn't have clean target. For that looking at your make file I suggest adding

clean:
    @rm *.o legrec
于 2012-05-17T10:37:45.257 回答
0

您可以通过多种方式改进 makefile 以减少重复。

make知道如何将 cpp 文件转换为目标文件,因此您无需告诉它。只需定义编译器和要使用的选项:我添加了 CPPFLAGS。

make将构建它在 makefile 中找到的第一个目标 - 在本例中为“legrec”。LD(链接)命令中的 $@ 指的是legrec. $^ 指的是先决条件(即对象列表)

这是我的版本:

CC = g++
CPPFLAGS = -Wall -Werror -pedantic
LDFLAGS =

legrec: legrec.o game.o board.o piece.o
    $(LD) $(LDFLAGS) $^ -o $@

legrec.o: game.h
game.o:   board.h piece.h move.h player.h game.h
board.o:  board.h piece.h move.h player.h
piece.o:  board.h piece.h move.h player.h

.PHONY: clean
clean:
    @rm -f *.o legrec

请注意,您可以将 -g 或 -O 等添加到 CPPFLAGS 行。顺便说一句,编译器可以给你的警告比 -Wall 产生的警告多得多。对于 C,我通常使用:

-Wall \
-Wextra \
-Wshadow \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wundef \
-Wunreachable-code \
-Wunused \
-Wcast-qual
于 2012-05-17T13:23:00.843 回答