C++11代码使用"clang++"编译,但不使用"clang-x C++"编译



基本问题

我有以下代码

#include <iostream>
#include <cstdint>
using namespace std;
int main ()
{
int32_t spam;
spam=5;
cout << "Hello World! We like " << spam << endl;
return 0;
}

当我执行clang++ -stdlib=libc++ cpptest.cpp时,它编译得很好。然而,Sublime Text 2的优秀SublimeClang解析器似乎并不理解它。我认为这可能是因为它使用了clang -x c++而不是clang++,并试图使用clang -x c++ -std=c++11 -stdlib=libc++ cpptest.cpp(或其各种排列)编译我的上面的代码片段,但失败得很厉害,抱怨链接器。

所以我的问题是,clang++clang -x c++有何不同?是否可以使用clang -x c++而不是clang++来编译上面的代码段?

我的机器是一台2012年MacBook Air运行的Mountain Lion。

调试内容

现在回到工作中,我已经对此做了更多的介绍,并比较了不同版本的详细输出。

呼叫clang -x c++

当我运行clang -v -x c++ -stdlib=libc++ cpptest.cpp时,我得到

Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -x c++ cpptest.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/c++/v1
/usr/local/include
/usr/bin/../lib/clang/4.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
[snip]
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

呼叫clang++

当我呼叫clang++ -v -stdlib=libc++ cpptest.cpp时,我得到以下信息。

clang++ -v -stdlib=libc++ cpptest.cpp 
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -x c++ cpptest.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/c++/v1
/usr/local/include
/usr/bin/../lib/clang/4.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -lc++ -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a

差异和解决方案

我能找到的唯一主要区别是,当我使用-x c++标志运行clang时,-lc++标志不会添加到以"/usr/bin/lb"开始的行的末尾。当我改为使用clang -v -lc++ -x c++ -stdlib=libc++ cpptest.cpp编译时,一切都正常。我仍然不明白clang++clang -x c++命令到底有什么不同,但这似乎至少使我的示例程序可以编译,并且似乎表明命令对链接器的作用不同。

Sublime文本2问题的解决方案

这个小小的bug搜寻是从我试图弄清楚如何让Sublime Text 2停止抱怨我拥有的真正有效的代码开始的,现在我想我已经做到了。所需要的只是按照这里的说明,并将包含设置为

/usr/lib/c++/v1
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/ext

在CCD_ 19文件中。

你说得对。只有连接阶段才有区别。您可以运行clang只是为了使用-c标志进行编译,您会得到相同的结果。

然后添加-lc++标志,两个样本都是相同的。

最新更新