C++ 无法在eclipse cdt中构建makefile

C++ 无法在eclipse cdt中构建makefile,c++,c,makefile,C++,C,Makefile,我总是出错 make: *** No rule to make target `all'. Stop. 我确信我的makefile工作得很好,因为我确实在终端上运行了它,一切都很好。但是,当我试图通过创建一个空的Makefile项目将所有内容导入eclispe时,我无法编译该程序。那么,我是否错过了eclipse配置中的某些内容 不管怎样,这是我的makefile,请看一下,并纠正我。谢谢 CC = g++ prog: legrec.o game.o board.o piece.o

我总是出错

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之后,奇怪的错误让我发疯。

你的第一条规则不是很好

您可以将其重命名为
all
,它将“起作用”。但更好的方法是:

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.规则名称应与生成的输出匹配


如果在命令行中键入just
make
,则会运行遇到的第一条规则(这就是为什么它适用于您)。我猜您的IDE正在运行
makeall
,而您还没有定义这样一个规则。

您的第一条规则不是很好

您可以将其重命名为
all
,它将“起作用”。但更好的方法是:

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.规则名称应与生成的输出匹配


如果在命令行中键入just
make
,则会运行遇到的第一条规则(这就是为什么它适用于您)。我猜您的IDE正在运行
make all
,而您还没有定义这样的规则。

我不是专家,但我会尽力帮助您。 将maketarget
prog
重命名为
legrec“
尝试添加以下行
.PHONY:都是干净的

all:legrec

此外,您的makefile没有干净的目标。为此,请查看您的make文件,我建议添加

clean: @rm *.o legrec
清洁:
@rm*.o legrec我不是专家,但我会尽力帮忙。 将maketarget
prog
重命名为
legrec“
尝试添加以下行
.PHONY:都是干净的

all:legrec

另外,您的makefile没有干净的目标。对于查看您的make文件,我建议添加

clean: @rm *.o legrec
清洁:
@rm*.o legrec您可以通过多种方式改进makefile以减少重复

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

make
将构建它在makefile中找到的第一个目标—在本例中为“legrec”。LD(link)命令中的$@指的是
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

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

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

make
将构建它在makefile中找到的第一个目标—在本例中为“legrec”。LD(link)命令中的$@指的是
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

我不知道你的最终作品名为“legrec”。您可以按照@Mat的建议将目标名称从prog更改为legrec。您没有意识到您的最终成品名为“legrec”。您可以按照@Mat的建议将目标名称从prog更改为legrec。您没有完全阅读我的答案。“prog”目标也必须重命名。您没有完全阅读我的答案。“prog”目标也必须重命名。