【问题标题】:Xcode 4.3 and C++11 include pathsXcode 4.3 和 C++11 包含路径
【发布时间】:2012-03-09 20:34:50
【问题描述】:

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

#include <type_traits>

int main()
{
}

但是,它没有找到type_traits 标头:

~ $ 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 标头。搜索命令显示可以在两个位置找到:

/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 标头?

更新

遵循@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这个标头也可以在/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

【问题讨论】:

    标签: c++ xcode c++11 clang


    【解决方案1】:

    你需要:

    -std=c++0x
    

    选择 C++11。你需要:

    -stdlib=libc++
    

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

    【讨论】:

    • 问题中的测试程序仅使用 -stdlib=libc++ 标志编译。 -std=c++0x 标志不需要使其编译(尽管它是合适的)。此外,Xcode 4.3 的 clang 允许使用标志 -std=c++11。
    • @robmayoff:对。 libc++ 将在 C++03 模式下工作(不太好)。我没有注意到-std=c++11,谢谢!我将开始使用并推荐它。
    • 用“c++11”选项确认;用“clang++ -std=c++11 -stdlib=libc++ -o test main.cpp”命令测试,编译没有问题。
    【解决方案2】:

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

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

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

    【讨论】:

    • 在我为我的项目设置这些特定设置之前出现链接错误。
    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多