I've recently added a module to a CMake project that depends on a library that I only have compiled against the release CRT. It looks like this in CMakeLists.txt:
IF(WIN32)
ADD_LIBRARY(mymodule MODULE ${MY_LIBRARY_FILES})
TARGET_LINK_LIBRARIES(mymodule libVendor)
INSTALL(TARGETS mymodule LIBRARY)
ENDIF(WIN32)
If I try to compile this module in MSVC with debug settings, the compile fails. So what I want to do is exclude it from being compiled and installed in the debug configuration. In the release configuration, it would be used as normal. Is it possible to do this with CMake?
What you can also do is exclude the target from the default build in a certain configuration:
SET_TARGET_PROPERTIES(mymodule PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_DEBUG True)
You can't have a target that is left out of a configuration, but you can have a library that is empty (or nearly empty) due to conditional compilation of its source code. And you can link to another library in a configuration specific way using the "optimized" and "debug" keywords to target_link_libraries.
For example, in your library source files, you could do:
#ifdef _DEBUG
// ... Debug code, possibly just a dummy function if necessary, goes here
#else
// ... Release code, the real deal, goes here
#endif
Then, you can specify that you only link to libVendor in the Release build, by using the "optimized" keyword for target_link_libraries, like this:
if(WIN32)
add_library(mymodule ...)
target_link_libraries(mymodule optimized libVendor)
install(TARGETS mymodule LIBRARY)
endif()
The target_link_libraries documentation explains the use of these keywords, and also mentions that you can define IMPORTED targets to achieve per-configuration effects. In order to define IMPORTED targets, though, the library files must have already been built, and you'd have to point to them. So... conditional compilation is probably the easiest way to do what you want to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With