How to Pass Variables to Makefile (120 Views)
Reply
Occasional Visitor
Chakkarapani
Posts: 1
Registered: ‎03-18-2008
Message 1 of 5 (120 Views)

How to Pass Variables to Makefile

Hi, I want to know how to pass a variable to makefile from the command prompt. And inside the makefile i want to check this variable and do conditional compilation.
In the command prompt when i just call make i starts executing blindly and no variables i passing for this make. But i need an option like to conditionally compile upon users choice.Please tell me how to establish this.
Honored Contributor
VK2COT
Posts: 921
Registered: ‎01-17-2006
Message 2 of 5 (120 Views)

Re: How to Pass Variables to Makefile

Hello,

You did not specify if you are using
GNU make or standard HP-UX.

Several possibilities:

a) For example, in a properly written
Makefile, the variable CFLAGS is included
in each command that runs the C compiler,
so a file myfile.c would be compiled as:

# cc -c $(CFLAGS) myfile.c

Whatever value is set for CFLAGS, it affects
each compilation.

When you run make, you can override it.

For example

# make CFLAGS='-g -O'

b) In HP-UX, there is make(1) flag "-e":

Environment variables override assignments
within Makefiles

c) HP-UX make(1) supports ifdef
test in Makefile. An example:

ifdef UNAME
SYSTEM = $(UNAME)
else
SYSTEM = "UNSUPPORTED"
endif

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 3 of 5 (120 Views)

Re: How to Pass Variables to Makefile

>And inside the makefile i want to check this variable and do conditional compilation.

As VK2COT alluded, you can't do this with HP's make, you must use gmake.

>But i need an option like to conditionally compile upon users choice.

You might be able to do this by explicitly mentioning a target. This allows you to build only parts of the makefile.

>VK2COT: c) HP-UX make(1) supports ifdef
test in Makefile.

I'm not sure where you got this but it isn't in the man page.

The only new feature I see is "conditional macro definition" but this probably won't help.
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 4 of 5 (120 Views)

Re: How to Pass Variables to Makefile

> I'm not sure where you got this but it
> isn't in the man page.

Nor, apparently, in reality:

dyi # cat makefile
ifdef UNAME
SYSTEM = $(UNAME)
else
SYSTEM = "UNSUPPORTED"
endif

dyi # make
Make: Must be a separator on rules line 2. Stop.

dyi # gmake
gmake: *** No targets. Stop.

dyi # uname -a
HP-UX dyi B.11.31 U ia64 4235313755 unlimited-user license
Honored Contributor
VK2COT
Posts: 921
Registered: ‎01-17-2006
Message 5 of 5 (120 Views)

Re: How to Pass Variables to Makefile

Hello,

Ahh, wrong statement by me (option c) in previous post).

GNU make supports two types of If test, ifdef
and ifeq (and the negative versions ifndef,
ifneq), not the HP-UX native one!

gmake != HP-UX make

Cheers,

VK2COT
VK2COT - Dusan Baljevic
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.