Issue
In my particular CMake project, I have three C++ files:
a.cpp
b.cpp
c.cpp
I want 'a.cpp' to be compiled in all configurations (release & debug).
I only want 'b.cpp' to be compiled in DEBUG configuration.
I only want 'c.cpp' to be compiled in RELEASE configuration.
How can I do this? I need something similar to the debug
and optimized
keywords that are accepted by the target_link_libraries()
CMake operation.
Solution
As far as I know cmake does not provide a way to exclude some files based on configuration. So you need a workaround to achieve this behavior.
For example, you can wrap the whole content of b.cpp
and c.cpp
with some guard macro:
#ifdef BUILD_ME
//original content of your file
#endif
and next set configuration-specific compiler definitions in cmake:
if (${CMAKE_GENERATOR} MATCHES "Make" AND "${CMAKE_BUILD_TYPE}" STREQUAL "")
# This block might be needed because CMAKE_BUILD_TYPE is usually
# undefined for makefiles.
# And COMPILE_DEFINITIONS_RELEASE are not chosen by default.
set(CMAKE_BUILD_TYPE RELEASE)
endif()
set_source_files_properties(b.cpp PROPERTIES COMPILE_DEFINITIONS_DEBUG BUILD_ME )
set_source_files_properties(c.cpp PROPERTIES COMPILE_DEFINITIONS_RELEASE BUILD_ME )
add_executable(target_name a.cpp b.cpp c.cpp)
Answered By - Andrey Kamaev Answer Checked By - Gilberto Lyons (WPSolving Admin)