views:

444

answers:

4

Hi guys I'm using the SKELETON_JAR variable on my c++ code in one header. However I want to allow the user to define the place of the jar in the compile time easily. I think the easiest way to do that is to put this define in makefile is that so?

#define SKELETON_JAR "./Util.jar"

??

+1  A: 

Depending on your compiler, the normal way to do this is to use the compiler's -D flag in the makefile. For example:

MYFLAGS = -DSKELETON_JAR="foo"

then later on:

gcc $(MYFLAGS) $(OTHER_STUFF)

anon
To the downvoter - which bit of this answer is incorrect?
anon
Was wondering the same thing
Igor Zevaka
Some people just like to down-vote I guess. I would perhaps have simply described the -D<macro>=<value> GCC option rather than make any possibly confusing assumptions about the OP's makefile.
Clifford
the OP tagged the question with the `makefile` though
Gregory Pakosz
Not only did he tag it, he explicitly asked about makefile in the question!
anon
thanks very much :D
Marcos Roriz
+4  A: 

In your code:

#ifndef SKELETON_JAR
  #define SKELETON_JAR "./Util.jar" // default path
#endif

and then in the makefile use CPPFLAGS:=-DSKELETON_JAR="./Util.jar".

Of course you have to make sure CPPFLAGS are passed to the compiler as part of the compile rule which is the case if you're using the default implicit rules.

From GNU Make documentation:

Compiling C programs

n.o is made automatically from n.c with a command of the form `$(CC) -c $(CPPFLAGS) $(CFLAGS)'

Gregory Pakosz
Useful bit of conditional compilation, though I would not have risked making any assumptions about the nature of the OP's makefile. The important part is just the -D command line switch.
Clifford
thanks guys, both answers are wonderful
Marcos Roriz
As an alternative to defining a default path you could also do something like this : #ifndef SKELETON_JAR #error "SKELETON_JAR hasn't been defined"#endifThat way you'll easily catch any cases where it hasn't been defined in a Makefile
Glen
A: 

Use compilation flags for same and define flag in Makefile.

Vivek
A: 

Another method is to place the JAR path as an environment variable. Some platforms have a getenv function for retrieving environment variables.

You may want to consider allowing the User to change the path before execution, such as using a configuration file.

Thomas Matthews