【问题标题】:Define macro from a file using cmake and use it later on the code使用 cmake 从文件中定义宏并稍后在代码中使用它
【发布时间】:2017-09-08 13:33:47
【问题描述】:

如何使用 cmake 从文件中定义宏及其值,然后以正确的方式在代码中使用它?这是我所做的:

CmakeList.txt

file(STRINGS server.txt _SERVERS)
add_definitions(-DSERVERS=${_SERVERS})

文件服务器.txt

http://192.168.1.150:8080

运行 cmake 后,我在命令行中找到了宏:

/D "SERVERS=http://192.168.1.150:8080"

但是当我输入代码时 main.cpp

  QString test = QString(SERVERS);

Visual Studio 将其标记为错误,一旦我将鼠标移到变量上,它就会给我以下错误:

错误:标识符“http://192.168.1.150:8080”未定义

知道如何进行吗?

【问题讨论】:

  • 对于这类事情,通常使用 configure_file() 来配置“config.hpp”头文件更容易且更易于维护。

标签: c++ file cmake macros


【解决方案1】:

错误在文件中 该值必须使用引号,如

"http://192.168.1.150:8080"

抱歉这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多