如何让CMake创建一个xcode项目,在该项目中系统头中的警告被抑制?

时间:2015-12-08 15:00:59

标签: xcode cmake

我有一个包含第三方库依赖项的CMake项目。第三方库的标题会生成我关心源代码的警告。

CMake生成的Ninja项目(使用clang)不会为第三方库标题生成警告。

CMake生成的Xcode项目会为第三方库标题生成警告。我尝试使用CMake命令包含第三方库路径

include_directories(SYSTEM dirname1 dirname2)

但Xcode似乎没有对此信息做任何事情。

如何从CMake生成不会产生系统标头警告的Xcode项目?

Xcode命令行只是将-I附加到标头包含路径上,无论我是否将SYSTEM添加到CMake。

Darwin.cmake的以下部分不太令人鼓舞:

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

添加额外信息

我已经对这个问题进行了小规模的再现:

== 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;
}

运行cmake -GNinja && cmake --build .版本没有错误。

运行cmake -GXcode && cmake --build .会出现以下错误: system_headers/system_header.h:3:7: error: unused variable 'x' [-Werror,-Wunused-variable] int x;

完整的Xcode编译路径是: /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

直接在命令行上运行Xcode编译命令会产生相同的错误。

2 个答案:

答案 0 :(得分:4)

CMake解决方案

您可以通过

明确设置-isystem标志
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /path/to/system/header")

C ++解决方案

您可以通过添加编译指示

来忽略系统标头中的警告
#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

有关详细信息,请参阅此答案:

注意:对于这两种情况,您必须检查是否正在使用适当的编译器。例如。 -isystem在MSVC中没有意义。

答案 1 :(得分:0)

看起来答案是,“你不能”通过在CMake中使用SYSTEM来做到这一点。上面解释的解决方法可以解决这个问题。

此问题与CMake target_include_directories(SYSTEM ...): SYSTEM option does not seem to work on recent versions of CMake targeting Xcode

类似