[英]CMake: Different compile definitions for static and shared library
In a project using cmake, I build two versions of a library, one statically and one dynamically linked.在一个使用 cmake 的项目中,我构建了一个库的两个版本,一个是静态的,一个是动态链接的。 For a single source file, I want to pass a different compile definition (ie -Dfoo=bar
) when compiling for the shared library only.对于单个源文件,我只想在为共享库编译时传递不同的编译定义(即-Dfoo=bar
)。
I know about set_target_properties
where I can use the COMPILE_DEFINITIONS
for a single source, but I don't know how to add that definition only for the shared library.我知道set_target_properties
可以在其中将COMPILE_DEFINITIONS
用于单个源,但我不知道如何仅为共享库添加该定义。
How can this be done?如何做到这一点?
Edit编辑
To clarify how this question is different, I am already making two versions of the same library.为了澄清这个问题的不同之处,我已经在制作同一个库的两个版本。
add_library(static_lib STATIC foo.cpp bar.cpp)
add_library(dyn_lib SHARED foo.cpp bar.cpp)
What I would like to do is to add the target property that foo.cpp
is compiled with -Dbaz=True
only when compiling foo.cpp
for dyn_lib
.我想这样做是为了添加目标属性, foo.cpp
与编译-Dbaz=True
编译只有当foo.cpp
的dyn_lib
。
The simplest way add the definition -Dbaz=True
for objects compiled for the library target dyn_lib
is to use target_compile_definition() .为库目标dyn_lib
编译的对象添加定义-Dbaz=True
的最简单方法是使用target_compile_definition() 。
target_compile_definition(dyn_lib PRIVATE -Dbaz=True)
This effectively is a shorter version of setting COMPILE_DEFINITIONS
property for dyn_lib
target.这实际上是为dyn_lib
目标设置COMPILE_DEFINITIONS
属性的较短版本。
set_target_properties(dyn_lib PROPERTIES COMPILE_DEFINITIONS -Dbaz=True)
To compile a single source file with the definition -Dbaz=True
use set_source_files_properties() .要编译定义为-Dbaz=True
的单个源文件,请使用set_source_files_properties() 。
set_source_files_properties(file.cpp PROPERTIES COMPILE_DEFINITIONS -Dbaz=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.