58

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.

JRM
  • 1,114
  • 2
  • 9
  • 10
  • 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 '18 at 01:01
  • 1
    Regarding what @donbright said, consider visiting this question: https://stackoverflow.com/q/37218953/1233251 – E_net4 - Krabbe mit Hüten Oct 18 '18 at 13:23

3 Answers3

70

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

include_directories(SYSTEM path)

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:include_directories

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.

http://cmake.org/cmake/help/v2.8.12/cmake.html#command:target_include_directories

RobertJMaynard
  • 2,151
  • 14
  • 13
  • 1
    The command should be `include_directories` without `set_` prepended. – Fraser Mar 07 '12 at 09: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 '12 at 06:18
  • @math have you tried upgrading cmake to a newer version? 2.8.1 is a couple years old. – RobertJMaynard Jan 23 '13 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 '15 at 08: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 '15 at 14:38
  • @zenith Neither can I get it to work on OS X with recent CMake versions (3.x). I opened a question with a minimal example: http://stackoverflow.com/q/31722426/1027706 – Ad N Aug 04 '15 at 09:54
  • For me it is not working, but I guess this is because the included directory only contains a header file, i.e. all warnings are propagated to the target's object files? Correct? – DrumM Jul 30 '19 at 08:37
11

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

Kara
  • 5,996
  • 16
  • 49
  • 56
ideasman42
  • 35,906
  • 32
  • 172
  • 287
  • 1
    Yes, it was fixed in 2.8.5. This is the commit with the fix: http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=33f5a83a5d7aa4c5a021c33a3fa55069da5eeed8 – sleske Aug 22 '13 at 21:10
-8

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

the_void
  • 5,398
  • 2
  • 27
  • 33
  • 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 '13 at 03:45
  • You should always keep your hands off of `CMAKE_CXX_FLAGS` or `CMAKE_C_FLAGS`. There is `target_compile_options` to add flags. – DrumM Jul 30 '19 at 08:31