# Setting compiler flags in CMakeLists.txt

**URL:** https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820
**Category:** Usage
**Created:** [August 26, 2023, 12:59am UTC](https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820 "2023-08-26T00:59:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![morpheus](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ce73a5/32.png) [@morpheus](https://discourse.cmake.org/u/morpheus)
#### Post date: [August 26, 2023, 12:59am UTC](https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820/1 "2023-08-26T00:59:48Z")

</div>

In my CMakeLists.txt I have:

```auto
SET( CMAKE_CXX_FLAGS "-Ofast -DNDEBUG -std=c++20 -march=native -fpic -ftree-vectorize")

```

However I found that CMake also adds following flags:

```auto
-O3 -DNDEBUG -std=gnu++20 -arch arm64

```

I found this by running following command:

```auto
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON build/Release

```

These flags are actually causing my program to run almost 2x slower! and took me a long time to figure it out. How can I tell CMake to use only the flags I have told it to use? Could someone please help me?

I am using

```auto
cmake --version [17:44:08]
cmake version 3.27.0-rc4

```

on mac os

---

<div class="post-metadata">

### Author: ![ringla](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@ringla](https://discourse.cmake.org/u/ringla)
#### Post date: [August 26, 2023, 4:56pm UTC](https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820/2 "2023-08-26T16:56:24Z")

</div>

It seems like CMake uses `CMAKE_CXX_FLAGS_RELEASE` and `CMAKE_CXX_FLAGS_DEBUG` (depending on the current value of `CMAKE_BUILD_TYPE`) in addition to `CMAKE_CXX_FLAGS`. The `-O3` you see is likely set by default in `CMAKE_CXX_FLAGS_RELEASE`.

If you want to use only the flags you manually specified, you can set the build type-specific flags to the empty string like this:

```auto
SET( CMAKE_CXX_FLAGS_DEBUG "")
SET( CMAKE_CXX_FLAGS_RELEASE "")
SET( CMAKE_CXX_FLAGS "-Ofast -DNDEBUG -std=c++20 -march=native -fpic -ftree-vectorize")

```

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [August 27, 2023, 4:01am UTC](https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820/3 "2023-08-27T04:01:53Z")

</div>

This is addressed by using “add\_compile\_options()”, which is generally preferred.

```cmake
cmake_minimum_required(VERSION 3.10)

project(foo LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_compile_options(
"$<$<COMPILE_LANGUAGE:CXX>:-Ofast;-DNDEBUG;-std=c++20;-march=native;-fpic;-ftree-vectorize>"
)

file(GENERATE OUTPUT main.cpp CONTENT "int main(void) { return 0; }")

add_executable(main ${CMAKE_CURRENT_BINARY_DIR}/main.cpp)

```

```sh
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release --fresh

```

```json
  "command": "/usr/bin/cc -O3 -DNDEBUG -Ofast -DNDEBUG -std=c++20 -march=native -fpic -ftree-vectorize 

```

I find this puts the user flags at the end, e.g. the order is like “-O3 -Ofast” which is what you desire, to have -Ofast take precedence.

---

<div class="post-metadata">

### Author: ![morpheus](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ce73a5/32.png) [@morpheus](https://discourse.cmake.org/u/morpheus)
#### Post date: [August 28, 2023, 1:51am UTC](https://discourse.cmake.org/t/setting-compiler-flags-in-cmakelists-txt/8820/4 "2023-08-28T01:51:42Z")

</div>

thank you.
