Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you exclude a CMake target from just one configuration?

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?

like image 412
Brian Avatar asked Dec 16 '22 13:12

Brian


2 Answers

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)
like image 116
Joris Timmermans Avatar answered Dec 22 '22 00:12

Joris Timmermans


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.

like image 34
DLRdave Avatar answered Dec 21 '22 22:12

DLRdave