Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ preprocessor variable

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"
like image 751
Marcos Roriz Junior Avatar asked Dec 11 '09 11:12

Marcos Roriz Junior


People also ask

What are preprocessor variables?

A preprocessor variable is specified in a %DECLARE statement with the FIXED, CHARACTER, or INITIAL attribute. No other attributes can be declared for a preprocessor variable, and attributes must not be repeated. (Other attributes are supplied by the preprocessor, however.)

Where are preprocessor variables defined?

The #define directive is used to "define" preprocessor "variables", which can then be used in one of three ways, as shown in the following three sections. It is also possible to define preprocessor variables when invoking the compiler, either as command-line arguments or through the IDE.

What is preprocessor in C?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What does ## mean in C preprocessor?

## is Token Pasting Operator. The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros.


1 Answers

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)'

like image 147
Gregory Pakosz Avatar answered Sep 19 '22 11:09

Gregory Pakosz