c++ - Xcode 4.3 和 C++11 包含路径

标签 c++ xcode c++11 clang

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

#include <type_traits>

int main()
{
}

但是,它没有找到 type_traits header :

~ $ c++ -o test main.cpp
main.cpp:1:10: fatal error: 'type_traits' file not found
#include <type_traits>
         ^
1 error generated.

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

~ $ 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

我检查了默认包含路径:

~ $ `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.

上述路径确实不包含 type_traits header 。搜索命令显示可以在两个位置找到:

/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

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

更新

遵循@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.

它似乎根本看不到 Xcode.app 包。

一个可能的原因是我同时安装了 Xcode 和“Xcode 命令行工具”。后者将二进制文件安装在 /usr 文件夹中。

我刚刚发现 type_traits header 也可以在 /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

最佳答案

你需要:

-std=c++0x

选择 C++11。你需要:

-stdlib=libc++

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

关于c++ - Xcode 4.3 和 C++11 包含路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9345271/

相关文章:

c++ - 未初始化的变量被使用

c++ - 在 C++11 中实现干净的 lambda 函数

c++ - 可变数量的构造函数参数取决于整数模板

c++ - 使用可变参数模板进行类型推导和参数传递

c++ - 'void *' to ' void (*) (bool)' 使用 C++ 编译器在 C 中编译文件的指针错误

c++ - Boost.Multiprecision cpp_int - 转换成字节数组?

xcode - gitignore 不忽略文件

ios - 当目标为 7.0 时,Xcode 引发 "EXC_BAD_ACCESS code=1"

c++ - Visual Studio 生成 "Error: uninitialized local variable ' x' "而在线编译器不生成 - 为什么?

objective-c - Xcode 无法识别的选择器实现