1

I am new to using makefiles. I have four files and one of them is a header file. And i want to write the command in the terminal: make do nu=20. This should compile all the files and the c++ program whereas nu=20 is for the argc and argv variables in the main function.

But errors occur, that is they aren't linking properly. Here's the makefile Sorry, for mistakes, but i am pretty new to this stuff. How should it really be written?

nu = 15

main.o: main.cpp ballbounce.h threads.cpp GUI.cpp
        g++ -c main.cpp

threads.o: threads.cpp ballbounce.h main.cpp GUI.cpp
        g++ -c threads.cpp

GUI.o: GUI.cpp ball.h main.cpp threads.cpp
        g++ -c GUI.cpp

run: main.o threads.o GUI.o ball.h
        g++ -o run main.o threads.o GUI.o

do: run
  ./run $(nu)

clean:
        rm main.o threads.o GUI.o run
Cherry Cool
  • 69
  • 2
  • 7
  • Improve your `Makefile` to use conventional make variables and to compile with `g++ -Wall -Wextra -g`, see [this](http://stackoverflow.com/a/14180540/841108); you probably need some GUI toolkit (e.g. [Qt](http://qt-project.org/)...). **Edit your question to improve it** (explain on which OS, with which graphical toolkit, what is `GUI.cpp`, and *give the error messages* etc...) – Basile Starynkevitch Jan 24 '15 at 08:24
  • Depending on the shell, you might be able to say `nu=20 make run`. But you have to remove `nu=15` from the makefile. – juanchopanza Jan 24 '15 at 08:26
  • @junchopanza: this is probably not the issue. I guess that he is using some graphical library, but does not link it (and perhaps missing `-I` compilation flags), so the *compilation fails* – Basile Starynkevitch Jan 24 '15 at 08:27
  • @BasileStarynkevitch Good point. But the whole paragraph is about setting some value of `nu` from the command-line invocation of make. – juanchopanza Jan 24 '15 at 08:34
  • But in `make do nu=20` the `nu=20` (for the *make* variable `nu`, not a *shell* variable) is processed by `make` so is not depending of the shell, and there is no need to remove `nu=15` in the `Makefile` ! – Basile Starynkevitch Jan 24 '15 at 08:35

1 Answers1

1

You can do it like this:

Edit your lines in makefile to be like this

do: run
    ./run ${nu} 

And then do the call

make do nu=20

When you will call it like this nu will be 20, and when you call make without providing nu

make do

nu will be 15 (as you declare it in makefile, like default value)

Nikolay K
  • 3,770
  • 3
  • 25
  • 37