CMake 目标属性 DEFINE_SYMBOL

一 set_target_properties

Targets can have properties that affect how they are built.

  • 目标可能具有影响其构建方式的属性。

  • 语法

    set_target_properties(target1 target2 ...
                          PROPERTIES prop1 value1
                          prop2 value2 ...)
    

Sets properties on targets. The syntax for the command is to list all the targets you want to change, and then provide the values you want to set next. You can use any prop value pair you want and extract it later with the get_property() or get_target_property() command.

  • 在目标上设置属性。命令的语法是列出您想要更改的所有目标,然后提供您接下来要设置的值。您可以使用任何您想要的属性值对,并在以后使用get_property() 或get_target_property()命令提取。

See also the set_property(TARGET) command. See Properties on Targets
for the list of properties known to CMake.

  • 另请参阅set_property(TARGET)命令。
  • 有关 CMake 已知属性列表,请参阅Properties on Targets
二 DEFINE_SYMBOL

Define a symbol when compiling this target’s sources.
DEFINE_SYMBOL sets the name of the preprocessor symbol defined when compiling sources in a shared library. If not set here then it is set to target_EXPORTS by default (with some substitutions if the target is not a valid C identifier). This is useful for headers to know whether they are being included from inside their library or outside to properly setup dllexport/dllimport decorations.

  • 在编译目标源文件时定义符号。
  • DEFINE_SYMBOL设置在共享库中编译源源文件时定义的预处理器符号的名称。如果不设置在此处,则默认情况下将其设置为target_EXPORTS(如果目标不是有效的 C 标识符,则有一些替换)。这是对头文件知道它们是从它们的库内部或外部被包含,以正确设置dllexport/dllimport装饰。
三 举例
  • 对于第三方库jsoncpp的 json/config.h 有如下代码:

    ...
    #ifdef JSON_IN_CPPTL
    #define JSON_API CPPTL_API
    #elif defined(JSON_DLL_BUILD)
    #if defined(_MSC_VER) || defined(__MINGW32__)
    #define JSON_API __declspec(dllexport)
    #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
    #endif // if defined(_MSC_VER)
    #elif defined(JSON_DLL)
    #if defined(_MSC_VER) || defined(__MINGW32__)
    #define JSON_API __declspec(dllimport)
    #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
    #endif // if defined(_MSC_VER)
    #endif // ifdef JSON_IN_CPPTL
    #if !defined(JSON_API)
    #define JSON_API
    #endif
    ...
    
  • 所以,如果要编译动态库,需要在CMakeLists.txt 中加入以下语句:

    set_target_properties(jsoncpp PROPERTIES DEFINE_SYMBOL JSON_DLL_BUILD)
    
四 参考
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值