# One source to create multiple objects

**URL:** https://discourse.cmake.org/t/one-source-to-create-multiple-objects/2819
**Category:** Code
**Created:** [February 23, 2021, 2:35pm UTC](https://discourse.cmake.org/t/one-source-to-create-multiple-objects/2819 "2021-02-23T14:35:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![odevel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/ee59a6/32.png) [@odevel](https://discourse.cmake.org/u/odevel)
#### Post date: [February 23, 2021, 2:35pm UTC](https://discourse.cmake.org/t/one-source-to-create-multiple-objects/2819/1 "2021-02-23T14:35:22Z")

</div>

Hello,

before I start with my question I’ll explain the background. In my C++ project I’ve one source file which is very heavy to compile (boost.spirit.x3) for a linker library of a project. Fortunately I can setup different compile units for low memory situations. Hence my CmakeLists.txt looks like:

```auto
project(testsuite_rules LANGUAGES CXX)
...
add_library(${PROJECT_NAME} "")
add_library(testsuite::rules ALIAS ${PROJECT_NAME})
...
if(localhost_RAM_MB GREATER 12000)
    set(SOURCE_LIST src/rules.cpp)
else()
    set(SOURCE_LIST 
        src/rules_api.cpp
        src/rules_a.cpp src/rules_b.cpp src/rules_c.cpp src/rules_d.cpp 
        src/rules_e.cpp src/rules_f.cpp src/rules_g.cpp src/rules_i.cpp 
        src/rules_l.cpp src/rules_n.cpp src/rules_p.cpp src/rules_q.cpp 
        src/rules_r.cpp src/rules_s.cpp src/rules_t.cpp src/rules_u.cpp 
        src/rules_v.cpp src/rules_w.cpp 
    )
endif()
...
target_sources(${PROJECT_NAME} PRIVATE
    ${SOURCE_LIST}
)
...
```

The problem rise, that I have on changes different places to edit. So the wish rises to have only the  
rules.cpp which can be compiled with different defines (e.g. COMPILE\_RULE{A…Z}) into corresponding object files (rules\_{a…z}.o) to be linked.

The first question is how to write this for CMake? What is the best/simplest solution for my source file, since it must be compiled as a whole without defines or even step-wise with defines.

Thanks

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 23, 2021, 7:02pm UTC](https://discourse.cmake.org/t/one-source-to-create-multiple-objects/2819/2 "2021-02-23T19:02:58Z")

</div>

Maybe [`UNITY_GROUP`](https://cmake.org/cmake/help/latest/prop_sf/UNITY_GROUP.html) is of interest? You can then tweak the group sized based on the memory size by setting the property on the appropriate rule source files. This would mean having separate sources still, but you wouldn’t also need a combined source file.

Other than that, I don’t think there’s a way to do this. I’d do it via:

```auto
#define COMPILE_RULE_A
#define COMPILE_RULE_B
#include "rules.cpp"

```

and then generate the files from CMake. But I think the unity build setup is likely to be way easier.
