# Assembler and C - handle flags and defines

**URL:** https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872
**Category:** Usage
**Created:** [January 17, 2024, 9:48pm UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872 "2024-01-17T21:48:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Claudio\_Eterno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/claudio_eterno/32/2509_2.png) [@Claudio\_Eterno](https://discourse.cmake.org/u/Claudio_Eterno)
#### Post date: [January 17, 2024, 9:48pm UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/1 "2024-01-17T21:48:10Z")

</div>

Hi,  
today I have been working with cmake in order to translate a project based on eclipse. I faced a problem and really don’t know if the founded solution is good or not. So, if you have a minute, tell me what you think about…  
Problem: the project contains c-language sources and files written in assembler.  
The first version of the cmake project obtained was incorrect because the include paths and options, for both kind of files, were the same.  
Some errors were present on assembler compilation.  
So this is the solution I found:

- create a library with a add\_library(CSources STATIC ${SOURCE});
- append includes to the lib with target\_include\_directories(CSources …file1.c … filen.c);
- set compiler flags with set\_target\_properties(CSources PROPERTIES COMPILE\_DEFINITIONS ${PRJ\_DEFINES});
- final target add\_executable(${PROJECT\_NAME} file\_assembly.s $\<TARGET\_OBJECTS:CSources\> );

It works but… I’m quite sure that for the purists I made mistakes…  
Personally I dislike to pass defines with a variable PRJ\_DEFINES instead to use ADD\_DEFINITIONS but if I use it these will be also part of the assembler compilation.

Suggestion are welcome…  
Bye

---

<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: [January 18, 2024, 3:39am UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/2 "2024-01-18T03:39:33Z")

</div>

If you have language-specific bits, you can use `$<$<COMPILE_LANGUAGE:C>:${flags}>` to make them conditional (here, they’ll only be given to C sources). See [`cmake-generator-expressions(7)`](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html).

---

<div class="post-metadata">

### Author: ![Claudio\_Eterno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/claudio_eterno/32/2509_2.png) [@Claudio\_Eterno](https://discourse.cmake.org/u/Claudio_Eterno)
#### Post date: [January 18, 2024, 8:34am UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/3 "2024-01-18T08:34:32Z")

</div>

This way?

```cmake
# tell CMake the target we want to compile
add_executable(${PROJECT_NAME}.elf ${SOURCE})

```

# options to apply fo C-language only

```cmake
target_compile_options( ${PROJECT_NAME}.elf PRIVATE $<$<COMPILE_LANGUAGE:C>: -DCORE_CM4 -DUSE_HAL_DRIVER -DSTM32H755xx > )

```

---

<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: [January 18, 2024, 8:41pm UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/4 "2024-01-18T20:41:54Z")

</div>

> [@Claudio\_Eterno](#):
>
> `$<$<COMPILE_LANGUAGE:C>: -DCORE_CM4 -DUSE_HAL_DRIVER -DSTM32H755xx >`

The spaces are a problem here. For `-D`, also prefer compile definitions. How about

```cmake
target_compile_definitions(${PROJECT_NAME}.elf PRIVATE "$<$<COMPILE_LANGUAGE:C>:CORE_CM4;USE_HAL_DRIVER;STM32H755xx>")

```

instead.

---

<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: [January 18, 2024, 8:54pm UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/5 "2024-01-18T20:54:57Z")

</div>

> [@ben.boeckel](#):
>
> ```auto
> target_compile_definitions(${PROJECT_NAME}.elf PRIVATE $<$<COMPILE_LANGUAGE:C>:CORE_CM4;USE_HAL_DRIVER;STM32H755xx>
> 
> ```

You’d need to put quotes around that generator expression, otherwise the embedded semicolons will still split the string to be seen by the command as multiple arguments, just like the situation when spaces were used.

---

<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: [January 18, 2024, 9:07pm UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/6 "2024-01-18T21:07:23Z")

</div>

Whoops, indeed. Fixed. Thanks.

---

<div class="post-metadata">

### Author: ![Claudio\_Eterno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/claudio_eterno/32/2509_2.png) [@Claudio\_Eterno](https://discourse.cmake.org/u/Claudio_Eterno)
#### Post date: [January 19, 2024, 5:58am UTC](https://discourse.cmake.org/t/assembler-and-c-handle-flags-and-defines/9872/7 "2024-01-19T05:58:41Z")

</div>

Thank you all very much!  
Precious info.  
Claudio
