1

My Makefile:

helloworldlib.obj: helloworldlib.cpp
    g++ -Wall -o helloworldlib.obj -c helloworldlib.cpp

helloworld.obj: source.cpp
    g++ -Wall -o helloworld.obj -c source.cpp

helloworld.exe: source.cpp helloworld.obj
    g++ -Wall -o helloworld.exe helloworld.obj helloworldlib.obj

I'm not sure what's wrong with this, when I run mingw32-make it only executes the first g++ -Wall -o helloworldlib.obj -c helloworldlib.cpp. As far as I know this makefile is syntactically correct, mingw just doesn't seem to be able to find the other lines.

1 Answers1

1

This is how make works. If no target is provided on the command line (e.g. mingw32-make helloworld.exe), by default it builds the first target defined in the file. See for instance: https://stackoverflow.com/a/2057716/2249356.

As a quick fix, you can just move the rule for helloworld.exe to the top of the file and then make will build all.

And, I think that the last rule is supposed to read

helloworld.exe: helloworld.obj helloworldlib.obj
    g++ -Wall -o helloworld.exe helloworld.obj helloworldlib.obj

rather then with the source.cpp and its object code helloworld.obj as dependencies.

jacob
  • 1,535
  • 1
  • 11
  • 26
  • Thank you, this worked for me. As an aside, do you know why using `ming32-make` with the `-B` (make all targets) switch doesn't work, if it actually does read the other lines? –  Sep 23 '19 at 19:42
  • Unfortunately, no, I have never used `-B`. The documentation is quite brief, it might have some restrictions. – jacob Sep 23 '19 at 20:05
  • 1
    The documentation for [-B](https://www.gnu.org/software/make/manual/html_node/Options-Summary.html) says: `Consider all targets out-of-date`. Which means it will always rebuild all dependencies even if no sources changed. But you still have to choose the actual target, or it uses the first one in the makefile as before. – ssbssa Sep 24 '19 at 10:14