Operating System - Linux
1748080 Members
5068 Online
108758 Solutions
New Discussion юеВ

How to Pass Variables to Makefile

 
Chakkarapani
New Member

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.
4 REPLIES 4
VK2COT
Honored Contributor

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
Dennis Handly
Acclaimed Contributor

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.
Steven Schweda
Honored Contributor

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
VK2COT
Honored Contributor

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