# makefile question

This topic is 4521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
The -D flag to your compiler allows you to define macros when it is invoked.

##### Share on other sites
Quote:
 Original post by igni ferroqueThe -D flag to your compiler allows you to define macros when it is invoked.

To clarify a bit, the syntax is -Dname=value.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by ProblemBabycool, 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%. #### Share this post ##### Link to post ##### Share on other sites 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

##### Share on other sites
Quote:
 Original post by ProblemBabyYes its the exit value I want.but what should it look like?RANDOM = Random.exeCFLAGS = -D\$?RANDOMis 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by ProblemBabyYeah nice it works!now the last problem... if I want a parameter for the Random programso 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.

• 10
• 17
• 9
• 14
• 41