我可以定义一个环境变量并将其用于条件编译中吗? [英] Can I define an environment variable and use it in conditional compilation?

查看:141
本文介绍了我可以定义一个环境变量并将其用于条件编译中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在* .h文件中执行此操作:

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

和V2.4.6是环境变量.我可以这样做吗?

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

我不想在Visual Studio或代码中定义V2.4.6,因为在不同系统上可能会有所不同.

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

推荐答案

我的测试:创建环境变量MY_VERSION = V2_4_6.启动VS,在项目属性,C ++,预处理器,预处理器定义中添加$(MY_VERSION).该程序:

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

打印确定".退出Visual Studio,将MY_VERSION值更改为另一个值或将其删除.启动VS,重新生成程序.现在它显示"??".

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

请注意,更改变量值后,有必要重新启动Visual Studio(因为不会动态刷新环境变量),并进行全部重建.

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.

这篇关于我可以定义一个环境变量并将其用于条件编译中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆