# How to specify different compile options for C and C++?

**URL:** https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583
**Category:** Usage
**Created:** [February 5, 2020, 5:22pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583 "2020-02-05T17:22:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [February 5, 2020, 5:22pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/1 "2020-02-05T17:22:37Z")

</div>

My CMake project builds an executable consisting of C and C++ source files:

```
add_executable(myAP
    main.cpp
    DpdkSock.c
    <snip>
)

```

I want to specify different compile options for the .cpp and .c files. (For example, compiling the .cpp file requires the -std=c++11 flag, but gcc does not recognise that flag and so it should not be used for the .c file).

How can I achieve this?

(I am using add\_compile\_options() to specify the options).

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [February 5, 2020, 5:41pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/2 "2020-02-05T17:41:16Z")

</div>

You can use generator expressions to state which flags should be used by per language.

My recommendation would be to read Step 10 of the CMake tutorial:

- [https://cmake.org/cmake/help/v3.16/guide/tutorial/index.html#adding-generator-expressions-step-10](https://cmake.org/cmake/help/v3.16/guide/tutorial/index.html#adding-generator-expressions-step-10)

---

<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: [February 5, 2020, 5:51pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/3 "2020-02-05T17:51:36Z")

</div>

And to manage specifically flag `-std=`, `CMake` have native support of this feature, See [Compile Features](https://cmake.org/cmake/help/v3.16/manual/cmake-compile-features.7.html#manual:cmake-compile-features(7)).

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [February 5, 2020, 7:02pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/4 "2020-02-05T19:02:04Z")

</div>

This is definitely the way to go. Using [https://cmake.org/cmake/help/v3.16/command/target\_compile\_features.html](https://cmake.org/cmake/help/v3.16/command/target_compile_features.html).

`target_compile_features(myAP PUBLIC cxx_std_11)` will bring appropriate flags on C++ file and not on C file.

if you want to selectively set compilation options on _file_ then you can look at:  
[https://cmake.org/cmake/help/v3.16/command/set\_source\_files\_properties.html](https://cmake.org/cmake/help/v3.16/command/set_source_files_properties.html)

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [February 6, 2020, 4:02pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/5 "2020-02-06T16:02:41Z")

</div>

Thanks for your answers. I am still struggling somewhat. The suggestions worked for -std=c++11, but I also want to enable ‘-pedantic’ for C++ files but not for C. I tried this:

```auto
# Enable pedantic warnings for C++ files but not C files
target_compile_options(ThebeAP PRIVATE
     $<$<CXX_COMPILER_ID:GNU>:
          -pedantic>)

```

But it seems gcc.exe is still also using -pedantic.

Any thoughts please?

---

<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: [February 6, 2020, 4:12pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/6 "2020-02-06T16:12:56Z")

</div>

You are not using the right generator expression. `$<CXX_COMPILER_ID:GNU>` asks about compiler id, not the compiler used for files. So, as soon as the C++ compiler is `GNU`, the genex is true.

You have to define options based on compiler used for compiling files:  
`$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-pedantic>`.

It is clearly explained here: [$\<COMPILE\_LANG\_AND\_ID\>](https://cmake.org/cmake/help/git-master/manual/cmake-generator-expressions.7.html#variable-queries).

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [February 6, 2020, 4:24pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/7 "2020-02-06T16:24:25Z")

</div>

Thanks. I’m afraid I have a syntax error which I can’t see how to fix from the docs:

```auto
target_compile_options(ThebeAP PRIVATE
                       $<$<COMPILE_LANG_AND_ID:CXX,GNU>:
                           -pedantic>)

```

```auto
Error evaluating generator expression:

$<COMPILE_LANG_AND_ID:CXX,GNU>

Expression did not evaluate to a known generator expression

```

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [February 6, 2020, 5:29pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/8 "2020-02-06T17:29:36Z")

</div>

Maybe `COMPILE_LANG_AND_ID` is not available in the CMake version that you are using. It was introduced in CMake 3.15.

However, you can use a combination of regular condition and generator expression (`COMPILE_LANGUAGE` was introduced in CMake 3.3):

```auto
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  target_compile_options(ThebeAP PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-pedantic>)
endif()

```

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [February 6, 2020, 5:52pm UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/9 "2020-02-06T17:52:36Z")

</div>

@McMartin Thank you. I upgraded to 3.16.4 and COMPILE\_LANG\_AND\_ID then worked fine.

---

<div class="post-metadata">

### Author: ![ycdtosa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ycdtosa/32/1433_2.png) [@ycdtosa](https://discourse.cmake.org/u/ycdtosa)
#### Post date: [May 10, 2021, 10:57am UTC](https://discourse.cmake.org/t/how-to-specify-different-compile-options-for-c-and-c/583/10 "2021-05-10T10:57:24Z")

</div>

This is another alternative using just a combination of generator expressions:

`$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:GNU>>:-pedantic>`

This is also pointed out in the documentation of [COMPILE\_LANG\_AND\_ID](https://cmake.org/cmake/help/git-master/manual/cmake-generator-expressions.7.html#variable-queries) provided by @marck.chevrier
