Reputation: 22094
I want to create a static and a shared library using CMake to create the build environment. Furthermore it should create an ANSI
and a UNICODE
version.
I found this posting: Is it possible to get CMake to build both a static and shared version of the same library?
which told me that I can use multiple add_library
statements to achieve this. When I tried it, it creates the libraries fine, but the posting doesn't tell me how to set different -D
options depending on which version is built.
My CMakeLists.txt
currently looks like this:
list(APPEND SUPPORT_SOURCE
dll_main.cpp
)
add_definitions(-DBUILD_SUPPORT_DLL)
add_library(support SHARED ${SUPPORT_SOURCE} )
add_library(support_s STATIC ${SUPPORT_SOURCE} )
add_library(support_u SHARED ${SUPPORT_SOURCE} )
add_library(support_su STATIC ${SUPPORT_SOURCE} )
After all, when I build the DLL, the functions needs the __declspec(dllexport)
declaration which it should not have in the static versions. Furthermore to build the UNICODE
variant I need to pass -DUNICODE
.
So I need to know which version is built and use appropriate build flags for it for the various targets.
Another thing I don't understand is, how I can create debug builds with a different name. I usually use the pattern libname.lib
and libname_d.lib
so I can have all possible versions in a single directory to link against.
Upvotes: 2
Views: 1366
Reputation: 78468
You can set compiler flags per-target by using the target_compile_definitions
command. For example:
target_compile_definitions(support PRIVATE MY_SHARED_DEFINITION)
target_compile_definitions(support_s PRIVATE MY_STATIC_DEFINITION)
As for adding a postfix to your debug libraries, you can achieve this by setting the CMake variable CMAKE_DEBUG_POSTFIX
:
set(CMAKE_DEBUG_POSTFIX _d)
This will cause all non-executable targets to have _d
appended when built in Debug. If you need more fine-grained control, you can set the DEBUG_POSTFIX
property for individual targets:
set_target_properties(support support_s PROPERTIES DEBUG_POSTFIX -dbg)
This will override the global CMAKE_DEBUG_POSTFIX
value for these two targets and give them a -dbg
postfix instead.
Upvotes: 2