Praveen Sripati
Praveen Sripati

Reputation: 33495

Error using make file

I am new to C/C++ and am running make hello with the below make file. I am using Ubuntu 14.04 64-bit OS.

hello: main.o factorial.o hello.o
    gcc main.o factorial.o hello.o -o hello -lstdc++
main.o: main.cpp functions.h
    gcc -c main.cpp
factorial.o: factorial.cpp functions.h
    gcc -c factorial.cpp
hello.o: hello.cpp functions.h
    gcc -c hello.cpp

-- In the make file I do specify gcc, but the g++ is being used as below. Why is it?

-- Also, in the g++ command the -c option is missing and so the below error. How to get around this?

vm4learning@vm4learning:~/make$ make hello    
g++     hello.cpp   -o hello
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

Upvotes: 0

Views: 112

Answers (1)

John
John

Reputation: 3520

See https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html for how to name your makefile

As twalberg pointed out, it looks like Make is not seeing the rules you attached at the top of the file. The default behavior for make to build an unknown target includes looking if there's a .cpp file with the targets name, and then running g++ target.cpp -o target if that file exists. So, make is not seeing your rules. Either your makefile is not present in the directory you're calling make from, it is misnamed, your rules are in a conditional, or you have a higher precedence Makefile in the same directory without a hello target.

Your makefile should be named GNUMakefile, makefile or Makefile, and if you have two, make will choose the first one. You can try adding

 $(info "Running This Makefile)

to the top of your Makefile to see if it's being seen.

Upvotes: 1

Related Questions