我有一个带有第三方库依赖项的CMake项目。第三方库的标头会生成我关心的源代码警告。
CMake生成的Ninja项目(使用clang)不会为第三方库标头生成警告。
CMake生成的Xcode项目为第三方库标头生成警告。我尝试使用CMake命令包含第3方库路径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 compile命令会产生相同的错误。
最佳答案
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中没有意义。