【问题标题】:Can I define an environment variable and use it in conditional compilation?我可以定义一个环境变量并在条件编译中使用它吗?
【发布时间】:2014-09-30 18:59:23
【问题描述】:

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

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

但我想要一种方法来做这样的事情:

#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 是一个环境变量。我可以这样做吗?

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

【问题讨论】:

  • AFAIK 这仅在代码中是不可能的 - 您需要某种自定义的预构建规则,可以读取环境变量并将它们转换为宏定义。
  • @DrewMcGowen 如你所知,我们可以在make文件中使用环境变量,所以应该可以在Visual Studio中做同样的事情,因为我认为VS使用某种make(我认为它是使用 nmake)
  • 我很确定您不允许在宏名称中使用点。
  • @ams 谢谢你。但这不是重点。变量可以命名为 V_2_4_6。
  • @mans:Visual Studio 使用 MSBuild 构建系统,当然不是 nmake。

标签: c++ c visual-studio conditional-compilation


【解决方案1】:

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

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

打印“OK”。退出 Visual Studio,将 MY_VERSION 值更改为另一个值或将其删除。启动VS,重建程序。现在它打印“??”。

请注意,更改变量值后,需要重新启动 Visual Studio(因为环境变量不会动态刷新),然后重新构建。

【讨论】:

    猜你喜欢
    • 2011-05-25
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多