什么 CMake 变量用于在 XCode 中设置 C++ 标准库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19345180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What CMake variable is used to set C++ Standard Library in XCode?
提问by S.Richmond
I have a pure C++11 DLL (No dependencies of any kind) I have been able to compile on Linux and Windows for some time now using CMake to generate the project files and make/MSVC to compile in each respective native system.
我有一个纯 C++11 DLL(没有任何类型的依赖项)我已经能够在 Linux 和 Windows 上编译一段时间了,现在使用 CMake 生成项目文件和 make/MSVC 在每个各自的本机系统中编译。
I want to compile on OSX now and I have been having a lot of problems getting CMake to set the correct project settings in XCode to compile the DLL.
我现在想在 OSX 上编译,但我在让 CMake 在 XCode 中设置正确的项目设置来编译 DLL 时遇到了很多问题。
Software versions:
软件版本:
XCode v5.0
CMake v2.8.12
The relevant CMake script code:
相关的 CMake 脚本代码:
# Set output directory if Apple OSX:
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
message("CMAKE HAS DETECTED A OSX SYSTEM - BUILDING FOR XCODE!")
set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++0x -stdlib=libc++")
IF(CMAKE_BUILD_TYPE MATCHES Release)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin/OSX/Release)
ENDIF(CMAKE_BUILD_TYPE MATCHES Release)
IF(CMAKE_BUILD_TYPE MATCHES Debug)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin/OSX/Debug)
ENDIF(CMAKE_BUILD_TYPE MATCHES Debug)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
However the settings do not correctly come through into the XCode project file:
但是,设置没有正确进入 XCode 项目文件:
You can see that the CMake commands make their way into the 'Other C++ Flags' area. But XCode will still fail to compile. However, if I change the 'C++ Standard Library' variable to 'libc++' it will compile fine.
您可以看到 CMake 命令进入了“其他 C++ 标志”区域。但是 XCode 仍然无法编译。但是,如果我将“C++ 标准库”变量更改为“libc++”,它将可以正常编译。
Note: I could post the compile error logs however I think that the above correctly identifies the root cause - I just need to know what CMake command actually sets the correct XCode property above.
注意:我可以发布编译错误日志,但是我认为以上正确识别了根本原因 - 我只需要知道什么 CMake 命令实际上设置了上面正确的 XCode 属性。
回答by Mario
For a minimal test project the following does the job:
对于最小的测试项目,以下工作可以完成:
SET(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
Maybe it's interfering with the following line:
也许它会干扰以下行:
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++0x -stdlib=libc++")
Also one remark on this line, is there the ${CMAKE_C_FLAGS}
on intention, or did you really mean ${CMAKE_CXX_FLAGS}
?
在这一行还有一个评论,是${CMAKE_C_FLAGS}
故意的,还是你的意思${CMAKE_CXX_FLAGS}
?
回答by zhang xiang
you can use set_property
你可以使用 set_property
macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY}
${XCODE_VALUE})
endmacro (set_xcode_property)
set_xcode_property(${your_target_name} CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set_xcode_property(${your_target_name} CLANG_CXX_LIBRARY "libc++")