Ask Your Question
1

How does CMakeList compile one class into two binaries with distinct configurations?

asked 2023-06-16 08:33:28 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-16 08:37:02 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-16 08:33:28 +0000

Seen: 11 times

Last updated: Jun 16 '23