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

I recently added a module to the CMake project, which depends on the library that I just compiled with the CRT release. This is similar to 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, compilation will fail. Therefore, I want to exclude it from compilation and installation in the debug configuration. In the release configuration, it will be used as usual. Is it possible to do this with CMake?

+4
source share
2 answers

You do not have a goal that is not taken into account in the configuration, but you may have an empty (or almost empty) library due to conditional compilation of the source code. And you can link to another library in a specific way using the "optimized" and "debug" keywords for target_link_libraries.

For example, in the library source files, you can:

 #ifdef _DEBUG // ... Debug code, possibly just a dummy function if necessary, goes here #else // ... Release code, the real deal, goes here #endif 

You can then indicate that you are only referencing libVendor in the Release assembly using the "optimized" keyword for target_link_libraries, for example:

 if(WIN32) add_library(mymodule ...) target_link_libraries(mymodule optimized libVendor) install(TARGETS mymodule LIBRARY) endif() 

The documentation for target_link_libraries explains the use of these keywords, and also mentions that you can define IMPORTED goals to achieve effects for each configuration. However, to define IMPORTED targets, library files must already be created, and you will need to point to them. So ... conditional compilation is probably the easiest way to do what you want to do.

+3
source

What you can also do is to exclude the target from the default assembly in a specific configuration:

 SET_TARGET_PROPERTIES(mymodule PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_DEBUG True) 
+6
source

All Articles