# How to use target\_compile\_definitions ?

**URL:** https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982
**Category:** Code
**Created:** [November 29, 2022, 3:08pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982 "2022-11-29T15:08:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [November 29, 2022, 3:08pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982/1 "2022-11-29T15:08:39Z")

</div>

Here is my naive attempt at using `target_compile_definitions`:

```auto
cmake_minimum_required(VERSION 3.12.1 FATAL_ERROR)
project(p)

add_library(foo foo.cpp)

set(defs "-D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D __STDC_CONSTANT_MACROS -D__ STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS")
target_compile_definitions(foo PRIVATE ${defs})

```

This gives:

```auto
/usr/bin/c++ -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS="64 -D __STDC_CONSTANT_MACROS -D__ STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS" 

```

How should I use this function instead in this example ?

Thanks much

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 29, 2022, 3:16pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982/2 "2022-11-29T15:16:24Z")

</div>

`target_compile_definitions` expects a **list** , not a string. Moreover, the format expected, as specified in the [documentation](https://cmake.org/cmake/help/latest/command/add_compile_definitions.html), is:

> Definitions are specified using the syntax VAR or VAR=value

So:

```cmake
set(defs _GNU_SOURCE _LARGEFILE_SOURCE _FILE_OFFSET_BITS=64 __STDC_CONSTANT_MACROS__ STDC_FORMAT_MACROS __STDC_LIMIT_MACROS)
target_compile_definitions(foo PRIVATE ${defs})

```

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [November 29, 2022, 3:19pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982/3 "2022-11-29T15:19:34Z")

</div>

Very nice ! Thanks for the quick answer. Now would you have a suggestion to refactor this:

```auto
find_package(LLVM REQUIRED)
target_compile_definitions(foo PRIVATE ${LLVM_DEFINITIONS})

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 29, 2022, 3:45pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982/4 "2022-11-29T15:45:35Z")

</div>

A little correction: `target_compile_definitions` supports -D flag as part of arguments (I confuse with `add_compile_definitions`).

Now, I don’t know `LLVM` package. But if `LLVM_DEFINITIONS` is specified as a string, I think `target_compile_definitions(foo PRIVATE ${LLVM_DEFINITIONS})` is OK.

But, it seems there is a bug in the parsing of the arguments. As you already discovered. If a definition has a value, the rest of the arguments are included as part of the value. So I suggest you to create a [bug](https://gitlab.kitware.com/cmake/cmake/-/issues) for that.

And as a workaround, use `separate_arguments`:

```cmake
set (CLEAN_DEFS ${LLVM_DEFINITIONS})
separate_arguments(CLEAN_DEFS)
target_compile_definitions(target PRIVATE ${CLEAN_DEFS})

```

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [November 29, 2022, 3:49pm UTC](https://discourse.cmake.org/t/how-to-use-target-compile-definitions/6982/5 "2022-11-29T15:49:30Z")

</div>

Thanks much !

- [https://gitlab.kitware.com/cmake/cmake/-/issues/24208](https://gitlab.kitware.com/cmake/cmake/-/issues/24208)
