当我运行Xcode4.6中的以下代码片段时,它编译并运行良好。但当我尝试使用命令行工具(clang++)编译它时,它没有做到
#include <iostream>
#include <memory>
int main(int argc, const char * argv[])
{
std::unique_ptr<int> foo(new int(0));
// insert code here...
std::cout << "Hello, this is cool giri World!n";
return 0;
}
以下是编译日志:
$clang--版本Apple LLVM 4.2版(clang-425.0.24)(基于LLVM 3.2svn)目标:x86_64-apple-darwin12.2.0螺纹型号:posix$clang++main.cpp-stdlib=libc++-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/DDeveloper/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/-I/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include/main.cpp:7:10:错误:命名空间"std"中没有名为"unique_ptr"的成员std::unique_ptr foo(new int(0));~~~~~^main.cpp:7:24:错误:函数样式转换或类型构造应为"("std::unique_ptr foo(new int(0));~~~^main.cpp:7:26:错误:使用未声明的标识符"foo"std::unique_ptr foo(new int(0));^生成3个错误。
尝试使用clang自己的标准库:
clang -std=c++11 -stdlib=libc++ main.cpp
默认的是GNU的标准库(libstdc++
),但Apple包含的版本很旧,不支持C++11。
您可以自己查看Xcode使用了什么命令行。
- 用Xcode构建您的项目
- 切换到日志视图。它的图标看起来像一个演讲气泡,里面有几行字
- 单击最新版本
- 构建步骤的列表将显示在主编辑区域中。右键单击"Compile main.cpp"并选择"Copy Transcript for Shown Results"
- 将其粘贴到您喜欢的文本编辑器中,以查看Xcode用于构建项目的确切命令行
确保为编译器和链接器调用clang++
,而不是clang
。
clang++
(作为编译器)需要-std=c++11
和-stdlib=libc++
编译器标志,而clang++
(作为链接器)需要-stdlib=libc++
链接器标记。
感谢大家向我推荐了让我坚持下去的解决方案。
最后,这对我来说是有效的。
我使用中提到的shell脚本卸载了命令行工具http://www.cocoanetics.com/2012/07/you-dont-need-the-xcode-command-line-tools/
然后使用$xcode select-switch/Applications/xcode.app/Contents/Developer/设置xcode版本。并最终使用$xcrun clang++main1.cpp-stdlib=libc++
编译我的代码。
这很好。谢谢