0

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
2
  • 1
    Is your makefile named either makefile or Makefile? Or did you name it something else? Commented May 31, 2016 at 18:16
  • Thanks - changed it to makefile and it worked, Commented May 31, 2016 at 23:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.