我有以下代码
#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款运行Mountain Lion的MacBook Air。
现在回到工作中,我已经玩了一些,并且比较了不同版本的详细输出。
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 Text 2停止抱怨我实际有效的代码,现在我想我已经得到了它。所需要的只是按照说明here并将包含设置为
/usr/lib/c++/v1
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/ext
在SublimeClang.sublime-settings
文件中。
答案 0 :(得分:3)
然后你添加-lc ++标志,两个样本都是一样的。