makefile question

Started by
11 comments, last by ProblemBaby 18 years, 3 months ago
is it possible to have a macro/define in my makefile which I can use in my source files? I want to excute a program for a macro and get the return value. thanks
Problems every where
Advertisement
The -D flag to your compiler allows you to define macros when it is invoked.
Free Mac Mini (I know, I'm a tool)
Quote:Original post by igni ferroque
The -D flag to your compiler allows you to define macros when it is invoked.


To clarify a bit, the syntax is -Dname=value.
cool, but is it possible to let value=return value from an application ?
and have a parameter to the application like if I define RANDOM

in my C code:
int t = RANDOM(1000);

and RANDOM executes a program that returns a value
Problems every where
Quote:Original post by ProblemBaby
cool, but is it possible to let value=return value from an application ?


You mean is it possible to set a variable in the Makefile to the text written to standard output from an application? If so, this might work:
VAR=`program`
Or if you want the exit value of the program, use $? (for Bash)
fooCFLAGS=$CFLAGS -D$?

Or for CMD use %ERRORLEVEL%.

Yes its the exit value I want.
but what should it look like?

RANDOM = `Random.exe`
CFLAGS = -D$?RANDOM

is it possible to send a parameter?

thanks
Problems every where
Quote:Original post by ProblemBaby
Yes its the exit value I want.
but what should it look like?

RANDOM = `Random.exe`
CFLAGS = -D$?RANDOM

is it possible to send a parameter?


I don't think you want the exit value, since you cant use an integer as a preprocessor identifier in any language I know. It's more likely you want to capture stdout and transform it into a macro value.

Something like
CFLAGS = -DRANDOM="`Random`"

WOuld set the C macro RANDOM to be the value fo the output of the "Random" program. This is assuming your Makefile is run from a Bourne-like shell (eg. Cygwin, Msys, Linux).

The trick is the backticks cause substitution of the output of the command for the command in a Bourne-style shell.

Stephen M. Webb
Professional Free Software Developer

Yeah nice it works!
now the last problem... if I want a parameter for the Random program
so when I write this in my C-code:
RANDOM("132")

"132" is sent as a parameter to the program.
Problems every where
Quote:Original post by ProblemBaby
Yeah nice it works!
now the last problem... if I want a parameter for the Random program
so when I write this in my C-code:
RANDOM("132")

"132" is sent as a parameter to the program.


I'm not quite sure what you want. If you want to pass a parameter to the program in the makefile, you would say something like this.
CFLAGS = -DRANDOM="`Random 132`"


If it's in the C code that you're trying to pass the parameter (if, say, the Random program chose a function name), you don't have to do anything, since the preprocessor is just text substitution and knows nothing about the C (or C++) language.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement