# Best way to create multiple binaries

**URL:** <https://discourse.cmake.org/t/best-way-to-create-multiple-binaries/665>\
**Category:** Code\
**Created:** [February 20, 2020, 1:06pm UTC](https://discourse.cmake.org/t/best-way-to-create-multiple-binaries/665 "2020-02-20T13:06:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![satrapes](https://discourse.cmake.org/user_avatar/discourse.cmake.org/satrapes/32/377_2.png) [@satrapes](https://discourse.cmake.org/u/satrapes)\
**Post date:** [February 20, 2020, 1:06pm UTC](https://discourse.cmake.org/t/best-way-to-create-multiple-binaries/665/1 "2020-02-20T13:06:45Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)\
**Post date:** [February 22, 2020, 1:17am UTC](https://discourse.cmake.org/t/best-way-to-create-multiple-binaries/665/2 "2020-02-22T01:17:14Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [February 22, 2020, 1:27am UTC](https://discourse.cmake.org/t/best-way-to-create-multiple-binaries/665/3 "2020-02-22T01:27:29Z")

</div>

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).
