CMakeLists can compile one class into two binaries with distinct configurations by creating two separate build targets for each configuration. This can be achieved using the CMake add_executable()
or add_library()
commands and specifying different compiler flags, linker flags, and source files for each target.
For example, the following CMakeLists file can create two separate binaries for a class named MyClass
with different optimization levels:
project(MyProject)
set(SOURCES
MyClass.cpp
)
add_executable(MyBinary-O0 ${SOURCES})
target_compile_options(MyBinary-O0 PRIVATE -O0)
add_executable(MyBinary-O3 ${SOURCES})
target_compile_options(MyBinary-O3 PRIVATE -O3)
This will create two separate executables, MyBinary-O0
and MyBinary-O3
, with different optimization levels. The source files for each target are the same (MyClass.cpp
), but each target is compiled with different compiler options.
To build both targets, the user can run cmake --build . --target MyBinary-O0 MyBinary-O3
. Alternatively, they can also build the targets one at a time using the make MyBinary-O0
or make MyBinary-O3
commands, depending on what they want to build.
Asked: 2023-06-16 08:33:28 +0000
Seen: 11 times
Last updated: Jun 16 '23