60

Is there any way in CMake to force a path specified via include_directories (or perhaps through a different function) to use the -isystem flag instead of the -I flag when building with gcc?

See http://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options for details on -I and -isystem.

2
  • 3
    Hi. I'm here from 8 years in the future. The use of isystem by cmake has now , strangely enough, with the addition of include_next, broken a good number of builds, especially cross builds.
    – don bright
    Aug 13, 2018 at 1:01
  • 1
    Regarding what @donbright said, consider visiting this question: stackoverflow.com/q/37218953/1233251
    – E_net4
    Oct 18, 2018 at 13:23

3 Answers 3

71

Yes you force a path to be a system include by using the optional SYSTEM flag

include_directories(SYSTEM path)

https://cmake.org/cmake/help/latest/command/include_directories.html

Starting with CMake 2.8.12 you can use the new target_include_directories to include system directory includes at the target level, while leveraging the new usage requirement features of cmake:

target_include_directories(foo SYSTEM PUBLIC path)

Now target foo will use path as a system include, and anything that links to foo will also use path as automatically as a system include. You can control the propagation of these usage requirements by changing the PUBLIC keyword to PRIVATE or INTERFACE.

https://cmake.org/cmake/help/latest/command/target_include_directories.html

7
  • 1
    The command should be include_directories without set_ prepended.
    – Fraser
    Mar 7, 2012 at 9:46
  • 6
    For me this did not worked on MAC OS 10.8, cmake 2.8.1: until I used a CMAKE_INCLUDE_SYSTEM_FLAG_CXX="isystem" just before the INCLUDE_DIRECTORIES directive.
    – math
    Dec 18, 2012 at 6:18
  • @math have you tried upgrading cmake to a newer version? 2.8.1 is a couple years old. Jan 23, 2013 at 18:23
  • 2
    On OS X with CMake 3.1 this works with include_directories, but not target_include_directories, for some reason.
    – bames53
    Mar 21, 2015 at 8:28
  • 1
    On OS X with CMake 3.2.2 this doesn't work with include_directories, nor with target_include_directories, setting CMAKE_INCLUDE_SYSTEM_FLAG_CXX doesn't help in either case.
    – emlai
    Jul 13, 2015 at 14:38
13

As stated already, the correct way to include system paths is:

include_directories(SYSTEM path1 path2)

However as of CMake 2.8.4 and Makefiles, This is only used for C++ and not C, I looked into it and GNU.cmake does not initialize: CMAKE_INCLUDE_SYSTEM_FLAG_C

So you can set this yourself right after calling project().

if(CMAKE_COMPILER_IS_GNUCC)
  set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
endif()

Update:

The CMake developers have fixed this in 2.8.5

1
  • 1
    Yes, it was fixed in 2.8.5. This is the commit with the fix: cmake.org/…
    – sleske
    Aug 22, 2013 at 21:10
-8

You could try using CMAKE_C_FLAGS and CMAKE_CXX_FLAGS to add additional flags.

2
  • 3
    While you can add includes with CMAKE_C/CXX_FLAGS This isnt really useful, CMake has methods to add includes and typically you want to make use of those.
    – ideasman42
    Aug 23, 2013 at 3:45
  • You should always keep your hands off of CMAKE_CXX_FLAGS or CMAKE_C_FLAGS. There is target_compile_options to add flags.
    – jaques-sam
    Jul 30, 2019 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.