Skip to content
Advertisement

makefile linking does not work (although no error message)

I am having issue with Makefile that I produced. It consists of one .cpp file with main() inside and I want to create executable from it. While putting in terminal make command I get following:

JavaScript

While putting first make STutorial.o (.o created) and then make get this:

JavaScript

Firstly, why make does not go from the beginning? Secondly, why this reference is undefined as if I did not include library, I did that in Makefile aswell as in STutorial.cpp file. Can you please help me out? I was reading up what could I do wrong and see no clue. (I am beginner and maybe mistake is a rookie one, I apologise in advance but cannot understand it alone) Makefile:

JavaScript

Advertisement

Answer

You have a few problem in your Makefile starting with:

JavaScript

You are redefining the FLAGS variable here. So what you should have is a different variable for your compiler and linker flags:

JavaScript

Now, for the sake of giving a complete answer, and not solving your immediate problem only I’ll try to show how to make the Makefile more flexible:

  1. Start with the sources – you should have a variable for them as well; it’s useful when adding/removing source files to/from the project:

    JavaScript
  2. define a variable for your object files (this will come in handy at link-time):

    JavaScript
  3. Compile all source files into object files:

    JavaScript
  4. Link your binary file using the compiled object files:

    JavaScript
  5. Add a clean command for completeness (removes the binary and object files):

    JavaScript

Now, putting it all together:

JavaScript

This should allow you to flexibly build, rebuild, clean you project.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement