简体   繁体   中英

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. For a single source file, I want to pass a different compile definition (ie -Dfoo=bar ) when compiling for the shared library only.

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.

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 .

The simplest way add the definition -Dbaz=True for objects compiled for the library target dyn_lib is to use 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.

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() .

set_source_files_properties(file.cpp PROPERTIES COMPILE_DEFINITIONS -Dbaz=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM