Harry
Harry

Reputation: 313

Fail to build makefile in eclipse cdt

I keep getting error

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

I am sure my makefile works perfectly, because I did run it on the terminal and everything fine. But when I try to import everything into eclispe by create an Empty Makefile Project, I couldn't compile the program. So did I miss something in eclipse configuration ?

Anyway, this is my makefile, please take a look, and correct me. Thanks

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

EDIT: Thanks for all replies, I did change the first line into all:legrec, and the previous error message was gone, however another errors came out

     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

I just don't understand why running same platform but the program performs differently. Before I was run on the terminal and edit on there that seems very well, but after porting into Eclipse, it drives me insance with the weird errors.

Upvotes: 1

Views: 628

Answers (3)

William Morris
William Morris

Reputation: 3684

You can improve the makefile in a number of ways to reduce duplication.

make knows how to turn a cpp file into an object file so you dont need to tell it. Just define the compiler and the options to use: I have added CPPFLAGS.

make will build the first target it finds in the makefile - in this case 'legrec'. The $@ in the LD (link) command refers to legrec. The $^ refers to the prerequisites (ie the list of objects)

Here is my version:

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

Note that you can add -g or -O etc to the CPPFLAGS line. By the way, there are many more warnings that the compiler can give you than are produced by -Wall. For C, I normally use:

-Wall \
-Wextra \
-Wshadow \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wundef \
-Wunreachable-code \
-Wunused \
-Wcast-qual

Upvotes: 0

Aravind
Aravind

Reputation: 603

I am not an expert, but I'll try to help. Rename the maketarget prog to legrec" Try adding the following lines
.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

Upvotes: 0

Mat
Mat

Reputation: 206909

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.

Upvotes: 1

Related Questions