3

I have a CMake project with 3rd party library dependencies. The headers for the 3rd party libraries generate warnings that I care about for our source code.

The CMake-generated Ninja project (using clang) does not generate warnings for 3rd party library headers.

The Xcode project generated by CMake produces warnings for 3rd party library headers. I've tried including the 3rd party library paths with the CMake command

include_directories(SYSTEM dirname1 dirname2)

but Xcode does not seem to do do anything with this information.

How can I generate an Xcode project from CMake that does not produce warnings for system headers?

The Xcode command line just appends -I onto the header include paths regardless of whether I add SYSTEM to CMake.

The following section from Darwin.cmake is less than encouraging:

# Xcode does not support -isystem yet.
if(XCODE)
  set(CMAKE_INCLUDE_SYSTEM_FLAG_C)
  set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX)
endif()

Extra info added

I've put together a small reproduction of the issue outlined:

== CMakeLists.txt ==
include_directories(local_headers)
include_directories(SYSTEM system_headers)

set(CMAKE_CXX_FLAGS "-Werror=unused-variable")

add_executable(main main.cpp)

== main.cpp ==
#include <system_header.h>
#include "local_header.h"

int main(int, const char**)
{
}

== system_headers/system_header.h ==
int f()
{
  int x;
  return 5;
}

== local_headers/local_header.h ==
int g()
{
  int x = 5;
  return x;
}

Running cmake -GNinja && cmake --build . builds without errors.

Running cmake -GXcode && cmake --build . gives the following error: system_headers/system_header.h:3:7: error: unused variable 'x' [-Werror,-Wunused-variable] int x;

The full Xcode compile path is: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.11 -g -Wno-sign-conversion -I/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Debug/include -I/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/local_headers -I/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/system_headers -I/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Project.build/Debug/main.build/DerivedSources/x86_64 -I/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Project.build/Debug/main.build/DerivedSources -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -F/Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Debug -Werror=unused-variable -MMD -MT dependencies -MF /Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Project.build/Debug/main.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Project.build/Debug/main.build/Objects-normal/x86_64/main.dia -c /Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/main.cpp -o /Users/jon/DEV/CMakeSandbox/XcodeSystemIncludes/xcode_build/Project.build/Debug/main.build/Objects-normal/x86_64/main.o

Running the Xcode compile command directly on the command line gives the same errors.

jbcoe
  • 3,611
  • 1
  • 30
  • 45
  • Supressing warnings can be done only by compiler(`clang` in your case), not by CMake itself. According to [clang manual](http://clang.llvm.org/docs/UsersManual.html#id26), `clang` automatically suppress warnings in system headers, which paths are specified with `-isystem` option. What precise command line is used for compile your executable? (Probably, you could find command line executed in the Ninja build log). – Tsyvarev Dec 08 '15 at 16:56
  • 1
    I'm only seeing the issue in Xcode. Ninja + clang seems to merrily suppress warnings for includes brought in by `#include <...>` – jbcoe Dec 08 '15 at 17:22
  • Adding `SYSTEM` to the CMake `include_directories` command does set the include paths as `-isystem` as you say but that does not seem to be needed for clang or used by Xcode. – jbcoe Dec 08 '15 at 17:27
  • 1
    Ok, so `include_directories(SYSTEM)` actually uses `-isystem` option. Probably, XCode adds some other parameters to the compiler, which disables warnings suppression. But it is hard to debug this issue without seeing full command line which builds executable. – Tsyvarev Dec 08 '15 at 17:45

2 Answers2

4

CMake solution

You can set -isystem flag explicitly by

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /path/to/system/header")

C++ solution

You can ignore warnings in system headers by adding pragmas

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#include <some/system/header.hpp> // warning ignored here
#pragma clang diagnostic pop
#include <myproject/header.hpp> // warning reported here

See this answer for more details:

Note: for both cases you have to check that you're using appropriate compiler. E.g. -isystem doesn't make sense in MSVC.

Community
  • 1
  • 1
  • The CMake option seems to work for all headers nested within the path given. Tested on OS X 10.10.5 with Xcode 7.0.1. – Rotsiser Mho Feb 21 '16 at 05:02
0

It looks like the answer is, "You can't" do this by using SYSTEM in CMake for now. The workaround explained above will do the trick though.

This question is similar to CMake target_include_directories(SYSTEM ...): SYSTEM option does not seem to work on recent versions of CMake targeting Xcode

jbcoe
  • 3,611
  • 1
  • 30
  • 45