Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define an environment variable and use it in conditional compilation?

I know that I can do this in a *.h file:

#ifdef _DEBUG
#pragma comment(lib, "libtiffd.lib")
#else
#pragma comment(lib, "libtiff.lib")
#endif

But I want a way that I can do something such as this:

#ifdef V2.4.6
#ifdef _DEBUG
#pragma comment(lib, "opencv_calib3d246d.lib")
#else
#pragma comment(lib, "opencv_calib3d246.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "opencv_calib3d249d.lib")
#else
#pragma comment(lib, "opencv_calib3d249.lib")
#endif
#endif

and V2.4.6 be an environment variable. Can I do this?

I don't want to define V2.4.6 inside Visual Studio or code as it would be different on different systems.

like image 459
mans Avatar asked Aug 07 '14 13:08

mans


People also ask

What statements are used for conditional compilation?

The $ELSE statement is used in conjunction with the $IF statement to control conditional compilation. The $END statement is used in conjunction with the $IF statement to control conditional compilation. A $IF statement provides the means whereby selected parts of the source text are not included in the compilation.

What command can you use to define an environment variable?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").

What statements are used for conditional compilation in C?

The compiler directives that are used for conditional compilation are the DEFINE directive, the EVALUATE directive, and the IF directive.


1 Answers

My test: Create environment variable MY_VERSION = V2_4_6. Start VS, in project properties, C++, Preprocessor, Preprocessor Definitions, add $(MY_VERSION). This program:

#ifdef V2_4_6
    cout << "OK" << endl;
#else
    cout << "??" << endl;
#endif

prints "OK". Exit Visual Studio, change MY_VERSION value to another value or remove it. Start VS, rebuild the program. Now it prints "??".

Note that after changing the variable value it is neccesary to restart Visual Studio (since environment variables are not refreshed dynamically), and make Rebuild All.

like image 58
Alex F Avatar answered Sep 19 '22 10:09

Alex F