# How to limit the scope of compiler flags only to a (my) local library?

**URL:** <https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [January 7, 2020, 12:35pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457 "2020-01-07T12:35:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![discovery](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@discovery](https://discourse.cmake.org/u/discovery)\
**Post date:** [January 7, 2020, 12:35pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/1 "2020-01-07T12:35:20Z")

</div>

1

I am trying the example below :

```auto
add_library(
        mylib
        src/my_code.cpp)

target_include_directories(mylib
        PUBLIC include ${catkin_INCLUDE_DIRS} ${thirdPartyLib_INCLUDE_DIRS})

add_dependencies(
        mylib
        ${mylib_EXPORTED_TARGETS}
        ${catkin_EXPORTED_TARGETS})

target_link_libraries(mylib
        PUBLIC
        ${thirdPartyLib_LIBRARY} ${ ${catkin_LIBRARIES})

target_compile_options(mylib PRIVATE -Werror -Wall -Wextra)

```

The issue is that the compile options also propagate to `thirdPartyLib` , yet I need them only for `mylib` .

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [January 7, 2020, 12:45pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/2 "2020-01-07T12:45:03Z")

</div>

Try adding the thirdPartyLib\_INCLUDE\_DIRS as SYSTEM. That should silence the warnings/errors those headers may cause.

---

<div class="post-metadata">

**Author:** ![tgq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@tgq](https://discourse.cmake.org/u/tgq)\
**Post date:** [January 7, 2020, 1:59pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/3 "2020-01-07T13:59:49Z")

</div>

Isn’t add\_dependencies causing that behavior? Did you try to create a separate target for the third party lib (so it compiles with default flags) and then link it with your main lib using target\_link\_libraries?

---

<div class="post-metadata">

**Author:** ![discovery](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@discovery](https://discourse.cmake.org/u/discovery)\
**Post date:** [January 7, 2020, 2:21pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/4 "2020-01-07T14:21:30Z")

</div>

Did not solve the issue.

---

<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:** [January 7, 2020, 2:41pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/5 "2020-01-07T14:41:00Z")

</div>

Your example is incomplete. How is created thirdPartyLib? Are you sure same compile flags are not also defined for this target?

---

<div class="post-metadata">

**Author:** ![discovery](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@discovery](https://discourse.cmake.org/u/discovery)\
**Post date:** [January 7, 2020, 3:01pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/6 "2020-01-07T15:01:16Z")

</div>

It is found with  
`find_package(thirdPartyLib)`

---

<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:** [January 7, 2020, 3:10pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/7 "2020-01-07T15:10:56Z")

</div>

Is it a module delivered as part of `CMake`? If yes, please give the information.  
If no, how is consumed this library? through a imported target or not?  
Did you check that these flags are not defined elsewhere (globally or at directory level using command `add_compile_options` or variable `CMAKE_<LANG>_FLAGS`)?

Currently, without a complete environment description, it is impossible to identify where come from your problem.

---

<div class="post-metadata">

**Author:** ![discovery](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@discovery](https://discourse.cmake.org/u/discovery)\
**Post date:** [January 7, 2020, 3:22pm UTC](https://discourse.cmake.org/t/how-to-limit-the-scope-of-compiler-flags-only-to-a-my-local-library/457/8 "2020-01-07T15:22:11Z")

</div>

`thirdPartyLib` is a custom lib not part of CMake. The compilation does not break if I remove my compile flags. Then the issue is at my CMake file.
