# How to set different compiler options for different sets of source files?

**URL:** https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342
**Category:** Usage
**Created:** [November 2, 2023, 3:29pm UTC](https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342 "2023-11-02T15:29:09Z")
**Posts on this page:** 4
**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: [November 2, 2023, 3:29pm UTC](https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342/1 "2023-11-02T15:29:09Z")

</div>

For a C++ project, for a single library and CMakeLists.txt file, I would like to set compiler options differently for sets of files. For example, I might want to enable warning **-Wunused-parameter** for some files but not others.

What is the correct pattern for doing this please?

---

<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: [November 2, 2023, 9:23pm UTC](https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342/2 "2023-11-02T21:23:06Z")

</div>

```cmake
set_property(SOURCE file1.cxx dir/file2.cxx … APPEND
  PROPERTY COMPILE_OPTIONS -Wunused-parameter)

```

---

<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: [November 9, 2023, 3:59pm UTC](https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342/3 "2023-11-09T15:59:58Z")

</div>

@ben.boeckel May I follow-up on my question and your answer please?

I’m currently using this pattern:

```auto
set (_lib_name "a_lib")

if(NOT MSVC)
    add_library(${_lib_name} OBJECT "")
    target_compile_options(${_lib_name} PRIVATE -Wunused-parameter)
endif()

target_sources(${_lib_name}
    PRIVATE
        fileA.cpp
        fileB.cpp
        fileC.cpp
        etc.
)

```

But I don’t want to apply -Wunused-parameter to fileC.cpp. So should I use something like the following?

```auto
set (_lib_name "a_lib")

if(NOT MSVC)
    add_library(${_lib_name} OBJECT "")
endif()

set(SRC_FILES
         fileA.cpp
         fileB.cpp
         etc.
)

target_sources(${_lib_name} PRIVATE ${SRC_FILES} fileC.cpp)

if(NOT MSVC)
    set_source_files_properties(${SRC_FILES} PROPERTIES COMPILE_OPTIONS "-Wunused-parameter")

    set_source_files_properties(fileC.cpp PROPERTIES COMPILE_OPTIONS "-fpermissive")
endif()

```

---

<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: [November 18, 2023, 8:16pm UTC](https://discourse.cmake.org/t/how-to-set-different-compiler-options-for-different-sets-of-source-files/9342/4 "2023-11-18T20:16:21Z")

</div>

That seems OK because the second call will overwrite the first one’s settings for `fileC.cpp`. A comment may be in order.
