一 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)