# Observation on compiler options

**URL:** https://discourse.cmake.org/t/observation-on-compiler-options/1481
**Category:** Usage
**Created:** [July 2, 2020, 9:29pm UTC](https://discourse.cmake.org/t/observation-on-compiler-options/1481 "2020-07-02T21:29:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lfcarreon1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/258eb7/32.png) [@lfcarreon1](https://discourse.cmake.org/u/lfcarreon1)
#### Post date: [July 2, 2020, 9:29pm UTC](https://discourse.cmake.org/t/observation-on-compiler-options/1481/1 "2020-07-02T21:29:28Z")

</div>

Hi,

I’m new to CMake and I have started experimenting with it by converting my hand coded make files into CMakeLists.txt after having read the book Professional CMake: A Practical Guide.

I’m using CMake 3.17.3 that comes with Fedora 32 Linux and I’m building a C++20 executable using GCC 10.1.1. After running CMake, I tried to determine what compiler and linker options were used.

This is the value of CMAKE\_CXX\_FLAGS\_RELEASE:

> CMAKE\_CXX\_FLAGS\_RELEASE = -O2 -DNDEBUG

This is what I found in the file called flags.make:

> CXX\_FLAGS = -O2 -DNDEBUG -m64 -Wall -Werror -std=c++2a
> 
> CXX\_DEFINES = -DBOOST\_ALL\_NO\_LIB -DBOOST\_UNIT\_TEST\_FRAMEWORK\_DYN\_LINK
> 
> CXX\_INCLUDES =

And this is what I found in the file called link.txt:

> /usr/bin/c++ -O2 -DNDEBUG -m64 -Wl,–no-undefined CMakeFiles/test\_builtins.dir/src/test\_builtins.cpp.o -o bin/test\_builtins -Wl,-rpath,/usr/local/lib64 /usr/local/lib64/libboost\_unit\_test\_framework.so.1.73.0

Based on the above observations, CMake appears to be re-using the compiler options in the linker command. I have been hand coding make files for a long time now and have never needed to re-use compiler options on the linker command for GCC. Note that some of the additional compiler and linker options I added using target\_compile\_definitions(), target\_compile\_options() and target\_link\_options().

Is this going to be the behavior of CMake for all Unix like platforms?

Kind regards,  
Leo

---

<div class="post-metadata">

### Author: ![lfcarreon1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/258eb7/32.png) [@lfcarreon1](https://discourse.cmake.org/u/lfcarreon1)
#### Post date: [July 2, 2020, 10:56pm UTC](https://discourse.cmake.org/t/observation-on-compiler-options/1481/2 "2020-07-02T22:56:03Z")

</div>

Just found out that the same thing happens when building a shared library. The compile options contained in CMAKE\_CXX\_FLAGS\_RELEASE are also used in the linker command.

---

<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: [July 6, 2020, 12:17pm UTC](https://discourse.cmake.org/t/observation-on-compiler-options/1481/3 "2020-07-06T12:17:26Z")

</div>

> [@lfcarreon1](#):
>
> CMake appears to be re-using the compiler options in the linker command

This will only occur for flags that are set via the `CMAKE_CXX_FLAGS` and `CMAKE_CXX_FLAGS_<CONFIG>`.

Compiler flags and options that are provided via `target_compile_definitions` and `target_compile_options` will only passed when doing object compilation.

Linker flags provided by `target_link_options` will only be passed when doing a link step.

This behavior is consistent across all CMake supported platforms, generators, and compilers. The reasoning for this is historical and changing this behavior would break numerous projects.
