# Language-specific add\_compile\_options?

**URL:** <https://discourse.cmake.org/t/language-specific-add-compile-options/2643>\
**Category:** Code\
**Created:** [January 27, 2021, 7:05pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643 "2021-01-27T19:05:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonRahaman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ronrahaman/32/953_2.png) [@RonRahaman](https://discourse.cmake.org/u/RonRahaman)\
**Post date:** [January 27, 2021, 7:05pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/1 "2021-01-27T19:05:07Z")

</div>

What’s the best way to set compiler flags for all objects compiled with a given language? Basically, I’d like to use `add_compile_options` to set flags for a specific language. Or I’d like a best-practices approach to this:

```auto
if (CMAKE_C_COMPILER_ID STREQUAL GNU)
    list(APPEND CMAKE_C_FLAGS -fopt-info-vec -fopt-info-loop)
    list(APPEND CMAKE_CXX_FLAGS -fopt-info-vec -fopt-info-loop)
elseif (CMAKE_C_COMPILER_ID STREQUAL Clang)
    list(APPEND CMAKE_C_FLAGS -Rpass=.*loop.*)
    list(APPEND CMAKE_CXX_FLAGS -Rpass=.*loop.*)
elseif( ... )
   ... other compilers ...
endif ()

```

My motivation is that I have a mixed C, C++, and CUDA project; and those opt report flags aren’t valide for nvcc.

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:** [January 27, 2021, 7:10pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/2 "2021-01-27T19:10:39Z")

</div>

You can use the `$<COMPILE_LANGUAGE>` genex to limit flags to be per-language. For the case here, you probably want `$<COMPILE_LANG_AND_ID>` instead. [https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html)

---

<div class="post-metadata">

**Author:** ![RonRahaman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ronrahaman/32/953_2.png) [@RonRahaman](https://discourse.cmake.org/u/RonRahaman)\
**Post date:** [January 27, 2021, 7:16pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/3 "2021-01-27T19:16:08Z")

</div>

Awesome! Looks like this works for me (without enclosing it in `if (CMAKE_C_COMPILER_ID STREQUAL GNU)`)

```auto
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fopt-info-vec>)
add_compile_options($<$<COMPILE_LANG_AND_ID:C,GNU>:-fopt-info-vec>)

```

---

<div class="post-metadata">

**Author:** ![RonRahaman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ronrahaman/32/953_2.png) [@RonRahaman](https://discourse.cmake.org/u/RonRahaman)\
**Post date:** [January 27, 2021, 7:20pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/4 "2021-01-27T19:20:41Z")

</div>

Actually, looks like I got something wrong with the generator expression. Does anything look off with my code above?

```auto
Problems were encountered while collecting compiler information:
	gcc: error: $<0:-fopt-info-vec: No such file or directory
	gcc: error: $<1:-fopt-info-vec: No such file or directory
	cc1: error: unrecognized command line option '-fopt-info-loop>'
	g++: error: $<1:-fopt-info-vec: No such file or directory
	g++: error: $<0:-fopt-info-vec: No such file or directory
	cc1plus: error: unrecognized command line option '-fopt-info-loop>'

```

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [January 28, 2021, 8:29am UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/6 "2021-01-28T08:29:04Z")

</div>

Note that those error messages mention `-fopt-info-loop`, which is not present at all in the code you’ve shown. Can you show the real code?

Nevertheless, if it looks like this:

```auto
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fopt-info-vec -fopt-info-loop>)

```

then it’s wrong, because a genex must be a single argument: for CMake, it’s just a string until generate time, and must survive as an intact string until then. So spaces in it require quoting (if you want them to appear as spaces), and lists (which you probably want in your case) must be composed using `$<SEMICOLON>`:

```auto
add_compile_options($<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fopt-info-vec$<SEMICOLON>-fopt-info-loop>)

```

---

<div class="post-metadata">

**Author:** ![asimplecoder](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/f17d59/32.png) [@asimplecoder](https://discourse.cmake.org/u/asimplecoder)\
**Post date:** [January 19, 2024, 12:46pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/7 "2024-01-19T12:46:58Z")

</div>

Hi, how to concatenate genex-es ?

I have already:

```auto
   add_compile_options(
      $<$<CONFIG:RELEASE>:-O3>
      $<$<CONFIG:RELEASE>:-Ob1>
   )

```

to which I now need to add the `$<COMPILE_LANGUAGE:Fortran>` genex, cause I am dealing with multiple languages project ?

I obviously tried

```auto
   add_compile_options(
      $<$<CONFIG:RELEASE>$<COMPILE_LANGUAGE:Fortran>:-O3>
      ...
   )

```

but did not work.

Thanks!

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [January 19, 2024, 1:08pm UTC](https://discourse.cmake.org/t/language-specific-add-compile-options/2643/8 "2024-01-19T13:08:09Z")

</div>

See the [generator expression docs](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html) for details, but this should work:

```auto
$<$<AND:$<CONFIG:RELEASE>,$<COMPILE_LANGUAGE:Fortran>>:-O3>

```
