# How to set a macro to all target\_link\_libraries with target\_compile\_definitions

**URL:** <https://discourse.cmake.org/t/how-to-set-a-macro-to-all-target-link-libraries-with-target-compile-definitions/11000>\
**Category:** Code\
**Created:** [June 12, 2024, 9:04am UTC](https://discourse.cmake.org/t/how-to-set-a-macro-to-all-target-link-libraries-with-target-compile-definitions/11000 "2024-06-12T09:04:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbizimis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pbizimis/32/4661_2.png) [@pbizimis](https://discourse.cmake.org/u/pbizimis)\
**Post date:** [June 12, 2024, 9:04am UTC](https://discourse.cmake.org/t/how-to-set-a-macro-to-all-target-link-libraries-with-target-compile-definitions/11000/1 "2024-06-12T09:04:51Z")

</div>

Hey everyone,  
I cannot get a preprocessor macro from CMake to the whole project.

I have a simple project structure ([GitHub - pbizimis/keyboard](https://github.com/pbizimis/keyboard)):

```auto
Project/
├─ CMakeLists.txt (1)
├─ src/
│ ├─ CMakeLists.txt (2)
│ ├─ main.c
│ ├─ rotary/
│ │ ├─ CMakeLists.txt (3)
│ │ ├─ rotary.c
│ ├─ other_module/
│ │ ├─ CMakeLists.txt (4)
│ │ ├─ module.c

```

My CMakeLists.txt (1):

```auto
cmake_minimum_required(VERSION 3.29)

include(pico_sdk_import.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

set(FAMILY rp2040)
set(BOARD pico_sdk)

add_subdirectory(src)

add_executable(keyboard_firmware)

pico_set_linker_script(keyboard_firmware ${CMAKE_CURRENT_LIST_DIR}/memmap_custom.ld)

target_link_libraries(keyboard_firmware PRIVATE Main)

pico_add_extra_outputs(keyboard_firmware)

```

then my src/CMakeLists.txt

```auto
add_library(Main main.c)

add_subdirectory(config)
add_subdirectory(usb)
add_subdirectory(rotary)
add_subdirectory(oled)
add_subdirectory(flash)
add_subdirectory(halves)

target_link_libraries(Main PRIVATE USB ROTARY OLED FLASH HALVES)
target_link_libraries(Main PUBLIC pico_stdlib pico_unique_id pico_cyw43_arch_none hardware_pio)

```

then each module such as src/rotary/CMakeLists.txt

```auto
add_library(ROTARY rotary.c)
target_include_directories(ROTARY PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(ROTARY PUBLIC pico_stdlib USB)

```

I would love to just define a macro in my base CMakeLists.txt (1) with `target_compile_definitions(keyboard_firmware PUBLIC EXAMPLE_MACRO)` but the modules (e.g. `rotary.c`) do not get access to it. Any ideas if that is even possible? What I ultimately want to do is to have a second executable:

```auto
cmake_minimum_required(VERSION 3.29)

include(pico_sdk_import.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

set(FAMILY rp2040)
set(BOARD pico_sdk)

add_subdirectory(src)

add_executable(keyboard_firmware_left)
pico_set_linker_script(keyboard_firmware_left ${CMAKE_CURRENT_LIST_DIR}/memmap_custom.ld)
target_link_libraries(keyboard_firmware_left PRIVATE Main)
pico_add_extra_outputs(keyboard_firmware_left)
target_compile_definitions(keyboard_firmware_left PUBLIC LEFT)

add_executable(keyboard_firmware_right)
pico_set_linker_script(keyboard_firmware_right ${CMAKE_CURRENT_LIST_DIR}/memmap_custom.ld)
target_link_libraries(keyboard_firmware_right PRIVATE Main)
pico_add_extra_outputs(keyboard_firmware_right)
target_compile_definitions(keyboard_firmware_right PUBLIC RIGHT)

```

I would appreciate any tips,  
Thank you ❤

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 12, 2024, 9:42am UTC](https://discourse.cmake.org/t/how-to-set-a-macro-to-all-target-link-libraries-with-target-compile-definitions/11000/2 "2024-06-12T09:42:09Z")

</div>

> ```auto
> target_compile_definitions(keyboard_firmware PUBLIC EXAMPLE_MACRO)
> 
> ```

Here you’ve set the compile definitions for this particular target `keyboard_firmware`, and any other target linking to this one should get its definitions too (_because it has `PUBLIC` scope_). But here your target is an executable, and your other targets don’t link to it (_which makes sense_), so they are not getting this definition.

It looks like it’s the `Main` target (_which is a library_) that you should have added the definitions to.

And if some of your targets don’t link to the `Main` target either, then I guess the only alternative is to add the definitions “globally” with [add\_compile\_definitions()](https://cmake.org/cmake/help/latest/command/add_compile_definitions.html#command:add_compile_definitions).

At least that is my understanding of what’s happening.

---

<div class="post-metadata">

**Author:** ![pbizimis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pbizimis/32/4661_2.png) [@pbizimis](https://discourse.cmake.org/u/pbizimis)\
**Post date:** [June 12, 2024, 12:23pm UTC](https://discourse.cmake.org/t/how-to-set-a-macro-to-all-target-link-libraries-with-target-compile-definitions/11000/3 "2024-06-12T12:23:31Z")

</div>

Thank you! That worked.

Is there any way I can get this to work with the `add_compile_definitions()`:

```auto
pico_set_linker_script(keyboard_firmware_left ${CMAKE_CURRENT_LIST_DIR}/memmap_custom.ld)
target_link_libraries(keyboard_firmware_left PRIVATE Main)
pico_add_extra_outputs(keyboard_firmware_left)
# add_compile_definitions(LEFT)

add_executable(keyboard_firmware_right)
pico_set_linker_script(keyboard_firmware_right ${CMAKE_CURRENT_LIST_DIR}/memmap_custom.ld)
target_link_libraries(keyboard_firmware_right PRIVATE Main)
pico_add_extra_outputs(keyboard_firmware_right)
# add_compile_definitions(RIGHT)

```
