• Advertisement
Sign in to follow this  

makefile question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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`

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I want to be able to do like

int a = RANDOM("132");

int b = RANDOM("56");


and the macro runs the program with the specified parameter and a and b gets the output

Share this post


Link to post
Share on other sites
Not going to work; it's the wrong way around. The preprocessor can't invoke programs.

Is there a reason you can't just do it at runtime? What *exactly* are you trying to do? I really can't think of why you would want to produce a different "randomized" executable each time you compile the same code o_O

Share this post


Link to post
Share on other sites
I want a program to solve for different things of course it shouldn't run at runtime, but if the parameter is constant wouldn't it be possible to run the program with the specified parameter and let a variable be equal to the output? depeding of the parameter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement