# Community practices for platform/compiler specific compiler options

**URL:** <https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104>\
**Category:** Code\
**Created:** [December 21, 2022, 9:27am UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104 "2022-12-21T09:27:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![some\_name](https://discourse.cmake.org/user_avatar/discourse.cmake.org/some_name/32/3023_2.png) [@some\_name](https://discourse.cmake.org/u/some_name)\
**Post date:** [December 21, 2022, 9:27am UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104/1 "2022-12-21T09:27:46Z")

</div>

I would like to build my project under Windows using Visual Studio 2022, and under Linux using Clang and Gcc. For each case, I have a set of options that I would like to apply to the entire project. I would like to know the community practices for achieving this.

---

<div class="post-metadata">

**Author:** ![some\_name](https://discourse.cmake.org/user_avatar/discourse.cmake.org/some_name/32/3023_2.png) [@some\_name](https://discourse.cmake.org/u/some_name)\
**Post date:** [December 25, 2022, 12:59pm UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104/2 "2022-12-25T12:59:06Z")

</div>

I decided to kick start this discussion by sharing what I personally decided to do. This is my project structrue:

```auto
Project Root
|
---- cmake [dir]
     |
     ---- clang_config.cmake
     ---- gcc_config.cmake
     ---- msvc_config.cmake
---- CMakeLists.txt

```

---

<div class="post-metadata">

**Author:** ![some\_name](https://discourse.cmake.org/user_avatar/discourse.cmake.org/some_name/32/3023_2.png) [@some\_name](https://discourse.cmake.org/u/some_name)\
**Post date:** [December 25, 2022, 12:59pm UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104/3 "2022-12-25T12:59:55Z")

</div>

In the root CMakeLisrts.txt, I include the cmake directory in the search path for modules and include all the modules:

set(CMAKE\_MODULE\_PATH  
“${CMAKE\_CURRENT\_SOURCE\_DIR}/cmake”  
)

include(“clang\_config”)  
include(“gcc\_config”)  
include(“msvc\_config”)

These modules are responsible for creating the variables

\_COMPILE\_DEFINITIONS, e.g., MSVC\_COMPILE\_DEFINITIONS  
\_COMPILE\_OPTIONS, e.g., MSVC\_COMPILE\_OPTIONS  
\_LINK\_OPTIONS, e.g., MSVC\_LINK\_OPTIONS

And then I use them as follows:

add\_compile\_definitions(  
“$\<$\<CXX\_COMPILER\_ID:Clang\>:${CLANG\_COMPILE\_DEFINITIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:GNU\>:${GCC\_COMPILE\_DEFINITIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:MSVC\>:${MSVC\_COMPILE\_DEFINITIONS}\>”  
)

add\_compile\_options(  
“$\<$\<CXX\_COMPILER\_ID:Clang\>:${CLANG\_COMPILE\_OPTIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:GNU\>:${GCC\_COMPILE\_OPTIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:MSVC\>:${MSVC\_COMPILE\_OPTIONS}\>”  
)

add\_link\_options(  
“$\<$\<CXX\_COMPILER\_ID:Clang\>:${CLANG\_LINK\_OPTIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:GNU\>:${GCC\_LINK\_OPTIONS}\>”  
“$\<$\<CXX\_COMPILER\_ID:MSVC\>:${MSVC\_LINK\_OPTIONS}\>”  
)

---

<div class="post-metadata">

**Author:** ![some\_name](https://discourse.cmake.org/user_avatar/discourse.cmake.org/some_name/32/3023_2.png) [@some\_name](https://discourse.cmake.org/u/some_name)\
**Post date:** [December 25, 2022, 1:05pm UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104/4 "2022-12-25T13:05:04Z")

</div>

An example of how the MSVC module could create the MSVC\_COMPILE\_OPTIONS variable is as follows.

#NOTE The forum software is NOT allowing me to add the forward slashes to the options below because it interprets them as links, and new users are only allowed two links in their posts.

# Generic options for all configurations

list(APPEND  
MSVC\_COMPILE\_OPTIONS  
permissive-  
Wall  
WX  
)

# Debug options

list(APPEND  
MSVC\_COMPILE\_DEBUG\_OPTIONS  
fsanitize=address  
fsanitize=fuzzer  
)  
list(APPEND  
MSVC\_COMPILE\_OPTIONS  
$\<$CONFIG:Debug:${MSVC\_COMPILE\_DEBUG\_OPTIONS}\>  
)

---

<div class="post-metadata">

**Author:** ![some\_name](https://discourse.cmake.org/user_avatar/discourse.cmake.org/some_name/32/3023_2.png) [@some\_name](https://discourse.cmake.org/u/some_name)\
**Post date:** [December 25, 2022, 1:05pm UTC](https://discourse.cmake.org/t/community-practices-for-platform-compiler-specific-compiler-options/7104/5 "2022-12-25T13:05:44Z")

</div>

# Release options

list(APPEND  
MSVC\_COMPILE\_RELEASE\_OPTIONS  
O2  
Oi  
Ot  
Oy  
)  
list(APPEND  
MSVC\_COMPILE\_OPTIONS  
$\<$CONFIG:Release:${MSVC\_COMPILE\_RELEASE\_OPTIONS}\>  
)

# RelWithDebInfo options

list(APPEND  
MSVC\_COMPILE\_REL\_WITH\_DEB\_INFO\_OPTIONS  
${MSVC\_COMPILE\_RELEASE\_OPTIONS}  
JMC  
Zi  
Zo  
)  
list(APPEND  
MSVC\_COMPILE\_OPTIONS  
$\<$CONFIG:RelWithDebInfo:${MSVC\_COMPILE\_REL\_WITH\_DEB\_INFO\_OPTIONS}\>  
)
