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

被刻印的时光 ゝ 提交于 2019-12-01 14:55:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/25184148/can-i-define-an-environment-variable-and-use-it-in-conditional-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!