-1

Apologies for such a beginner question, but I have been stuck on making Makefile work on my c++ files for quite a while

My makefile contains two .cpp files and one .h files and are as follows

example.o: example.cpp example.h
    g++ -c example.cpp

main.o: main.cpp example.h
    g++ -c main.cpp

main: main.o example.o
    g++ main.o example.o -o main

and it outputs the following error when I try to make main

Undefined symbols for architecture x86_64: *(with large pieces of code)*
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

However, it works perfectly fine when I just do a compilation of the program by using

g++ main.cpp example.cpp -o main

Is there any reason why Makefile doesn't work but just compiling works? Thanks a lot for any replies!

user4157124
  • 2,809
  • 13
  • 27
  • 42
Kalmeder
  • 11
  • 4
  • 1
    I don't see any obvious problems. What is the output of `make main --trace`? What symbols are reported as undefined? – HolyBlackCat Nov 08 '20 at 13:19
  • 2
    When you run `make` with no parameters, it will build the first thing in your Makefile, so you probably want to move the rule for `main` to be at the top. – Mark Setchell Nov 08 '20 at 14:05

1 Answers1

1

Please show the output of make including the compiler lines that were invoked, not just some of the error output. Also it would be helpful if you provided at least SOME of the symbols that were not found so we could get an idea of what is missing.

There is no reason from the info you've provided why this should happen, so the cause must be related to some detail that you haven't provided.

One difference between the command line and the makefile is that the makefile builds object files then turns them into an executable, while the command line simply compiles the sources directly into an executable.

Maybe you have some .o files lying around from a different compilation? If you use rm -f *.o before you run make do you still see the same errors?

Also, it's a little odd that you're invoking g++ but the error output says clang which is a completely different compiler. Are you trying to build on MacOS? You should always provide your operating system info. If you're building on MacOS you should just use clang directly, unless you've explicitly install GCC and want to use that.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Thanks a lot for the response! And yes I am using MacOS to build it. I've solved the problem and turns out that I was using plain text rather than TextEdit to type the Makefile xd sorry – Kalmeder Nov 09 '20 at 14:21
  • I'm glad you fixed it, but there's no way that "using plain text ... to type the Makefile" would cause an error like that, by itself. There's nothing wrong with using plain text to create a makefile (as long as you get the TAB vs spaces right, and if you didn't you'd get a make syntax error not a linker error). – MadScientist Nov 09 '20 at 14:43