Best way to create multiple binaries

Hello cmake noob here.
I have a project where I would like to build an executable for various products that have different settings.

Folder looks a bit like
bin (folder where final binaries are stored) e.g. main.0.debug.out, main.1.debug.out main.0.release.out, main.1.release.out
lib (folder where libraries are stored) e.g. common.a
cmake_bin (out of source build)
src
– common source code
– product0 - settings.c
– product1 - settings.c

The idea is that you only have to build the time consuming library once and only link the small settings files for different builds to improve the build speed. Also I wouldn’t mind being able to have both CMAKE_BUILD_TYPE=Debug and Release for each of these products.

What is the recommended way to do this?
Should I have separate CMakeLists.txt for each product?
Should I have custom CMAKE_BUILD_TYPE’s Debug_Product0, Debug_Product1, Release_Product0, Release_Product1? Then do I run cmake once for each type?

I have read https://cmake.org/cmake/help/latest/guide/tutorial/index.html#id19 and it seems to indicate a separate cmake_bin for each build type. Can you help me delineate my thinking?

Many thanks,

Sat

Why not create an OBJECT library with the common sources and then create two (or more) executables via:

add_library(my_app_common OBJECT ${COMMON_SOURCES})
add_executable(product0 settings0.c $<TARGET_OBJECTS:my_app_common>)
add_executable(product1 settings1.c $<TARGET_OBJECTS:my_app_common>)

You would still have to compile once for each of Debug and Release (or any other configuration like RelWithDebInfo), but you would have to do that anyway if you want the different configurations to apply to the common sources, too.

Shouldn’t need an object library for this, an ordinary library should be fine (and is likely to be simpler and more familiar for the average user). If that library is a shared library, it might also mean faster builds (linking is likely to be quicker) and with lots of executables, it will also save space (if that is important to you).

1 Like