Xcode 4.3和C++ 11包含路径

Sta*_*ked 25 c++ xcode clang c++11

我安装了Xcode 4.3并想测试这个C++ 11程序:

#include <type_traits>

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

但是,它找不到type_traits标题:

~ $ c++ -o test main.cpp
main.cpp:1:10: fatal error: 'type_traits' file not found
#include <type_traits>
         ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

看来我正在使用正确的编译器:

~ $ c++ -v
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)

我检查了默认的包含路径:

~ $ `c++ --print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin11"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
Run Code Online (Sandbox Code Playgroud)

上面的路径确实不包含type_traits标题.搜索命令显示可以在两个位置找到:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/c++/v1/type_traits
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/type_traits
Run Code Online (Sandbox Code Playgroud)

显然我的编译器默认值有问题.如何配置我的编译器,以便它type_traits在正确的位置找到标头?

更新

遵循@ sehe的建议:

~ $ clang++ -v -fshow-source-location -std=c++0x main.cpp
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
 "/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.3 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/bin/../lib/clang/3.1 -fmodule-cache-path /var/folders/d6/sf96r2ps457230x3v8yj52s40000gp/T/clang-module-cache -std=c++0x -fdeprecated-macro -fdebug-compilation-dir /Users/francis -ferror-limit 19 -fmessage-length 174 -stack-protector 1 -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/d6/sf96r2ps457230x3v8yj52s40000gp/T/main-sUcT7k.o -x c++ main.cpp
clang -cc1 version 3.1 based upon llvm 3.1svn default target x86_64-apple-darwin11.3.0
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "/usr/include/c++/4.0.0"
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /usr/bin/../lib/clang/3.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
main.cpp:1:10: fatal error: 'type_traits' file not found
#include <type_traits>
         ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

它似乎根本没有查看Xcode.app包.

一个可能的原因是我安装了Xcode和"Xcode的命令行工具".后者在/usr文件夹中安装了二进制文件.

我刚刚发现type_traits标题也可以在/usr/include:

~ $ find /usr/include -type f -name type_traits
/usr/include/c++/4.2.1/tr1/type_traits
/usr/include/c++/v1/type_traits
Run Code Online (Sandbox Code Playgroud)

How*_*ant 25

你需要:

-std=c++0x
Run Code Online (Sandbox Code Playgroud)

选择C++ 11.你需要:

-stdlib=libc++
Run Code Online (Sandbox Code Playgroud)

选择libc ++.默认情况下,使用gcc 4.2附带的std :: lib,它是C++ 11之前的版本.

  • 问题中的测试程序仅使用`-stdlib = libc ++`标志进行编译.编译时不需要`-std = c ++ 0x`标志(虽然它是合适的).此外,Xcode 4.3的clang允许标志`-std = c ++ 11`. (3认同)

Joh*_*wer 6

Howard Hinnant的答案(带有更正)是命令行的正确答案.

要在Xcode中使用新的C++ 11标准库:

  • 在项目的"构建设置"选项卡中,向下滚动到"Apple LLVM编译器4.1 - 语言"
  • 将"C++语言方言"设置为"C++ 11 [-std = c ++ 11]"
  • 将"C++标准库"设置为"libc ++(支持C++ 11的LLVM标准C++库)"