Click to See Complete Forum and Search --> : makefile arguments


tecknophreak
12-22-2004, 12:14 AM
I'm trying to create a makefile which uses the arguments to change a variable, i.e.

make touchscreen

this make will look something like


ifeq $1 "touchscreen"
DEFINE = -D_TOUCHSCREEN
else
DEFINE = -D_NORMAL
endif

all:
g++ program.cpp -o program $(DEFINE)


Except not like that, cause it doesn't work.

I just don't want to create basically two different makes, i.e.



all:
g++ program.cpp -o program -D_NORMAL

touchscreen:
g++ program.cpp -o program -D_TOUCHSCREEN



Now of course, I have a bunch of files which are compiled, but not linked which all need this before the final compile/linkage.

truls
12-24-2004, 08:36 AM
As far as I remember (and I'm stuck at a windows computer during the holidays here) you can do something like this.

From the command line you can write:
make -DFoo
You can then write the makefile as:
#ifdef Foo
C_FLAGS = ...
#else
C_FLAGS = ...
#endif
Where ... is replaced by whatever you need.
I think you can also use += for flags, so that you can just add the additional flags if a certain flag is set.

I hope that's somewhat right :p

evac-q8r
12-24-2004, 02:01 PM
I think the alternative way you don't want to use it is the best way if not the only way. The reason why is I don't believe make excepts arguments the way you're thinking they should as opposed to something like a shell script. It only accepts or looks for targets. So to try and expand $1 doesn't even apply in this situation. You can check this by trying to echo $1 back on the terminal if you think it should be accepting it as an true argument. Technically, it is an argument but make looks for it within the targets in the file. Only if you specify -f will it look for an argument other than a target namely a specific Makefile that you want to use, otherwise any other arguments you specified will be interpreted as targets. You'll have to think of a more clever way if you don't want to use a seperate target in this situation.

EVAC

bwkaz
12-25-2004, 11:44 PM
You could structure the Makefile like this:

USE_TSCREEN=0

ifeq(${USE_TSCREEN}, 0)
DEFINES=-D_NORMAL
else
DEFINES=-D_TOUCHSCREEN
endif

target:
g++ program.cpp -o program ${DEFINES} This makes the default be non-touchscreen. But the user can override the USE_TSCREEN variable from the command line, like so:

make USE_TSCREEN=1

and then ${DEFINES} would include -D_TOUCHSCREEN instead of -D_NORMAL, which would be propagated to the g++ command.

The ifeq(...) part may be GNU make specific, though...

tecknophreak
01-01-2005, 09:17 PM
thanks bwkaz! i guess it's the best i'll get.