C ++ 11代码用“clang ++”编译,但不用“clang -x c ++” [英] C++ 11 code compiles with `clang++`, but not with `clang -x c++`

查看:2989
本文介绍了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<<垃圾邮件< 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 4.1版本(tags / Apple / clang-421.11.66) 3.1svn)
目标:x86_64-apple-darwin12.2.1
线程模型: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 -how-option -fcolor-diagnostics -o / var / folders / 8m / b4wllzbs67d9zxcj1dd8q6912jclkf / T / cpptest-1eOyZn.o -x c ++ cpptest.cpp
clang -cc1 4.1基于LLVM 3.1svn默认目标x86_64-apple-darwin12.2.1
忽略不存在的目录/ usr / include / c ++ / v1
#include...搜索从这里开始:
#include< ...>搜索从这里开始:
/usr/bin/../lib/c++/v1
/ usr / local / include
/usr/bin/../lib/clang/4.1/include
/ usr / include
/ System / Library / Frameworks(框架目录)
/ Library / Frameworks(框架目录)
搜索结束。
/ usr / bin / ld-demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/c​​pptest-1eOyZn.o -lSystem / usr / bin /../ lib / clang / 4.1 / lib / darwin / libclang_rt.osx.a
架构x86_64的未定义符号:
[snip]
ld:找不到符号架构x86_64
clang:错误:linker命令失败,退出代码1(使用-v查看调用)


$ b b

调用 clang ++ -v -stdlib = libc ++时调用 clang ++



cpptest.cpp
我得到以下结果。

  clang ++ -v -stdlib = libc ++ cpptest.cpp 
Apple clang版本4.1(标签/ Apple / clang-421.11.66)(基于LLVM 3.1svn)
目标:x86_64-apple-darwin12.2.1
线程模型: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/c​​pptest-E7FTDV.o -x c ++ cpptest.cpp
clang基于LLVM的-cc1版本3.1svn默认目标x86_64 -apple-darwin12.2.1
忽略不存在的目录/ usr / include / c ++ / v1
#include...搜索从这里开始:
#include< ...> ;搜索从这里开始:
/usr/bin/../lib/c++/v1
/ usr / local / include
/usr/bin/../lib/clang/4.1/include
/ usr / include
/ System / Library / Frameworks(框架目录)
/ Library / Frameworks(框架目录)
搜索结束。
/ usr / bin / ld-demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/c​​pptest-E7FTDV.o -lc ++ -lSystem / usr / bin /../ lib / clang / 4.1 / lib / darwin / libclang_rt.osx.a



差异和解决方案?



我可以发现的唯一主要区别是,当我使用 -x c ++ 标志 -lc ++ 标志不会在以/ usr / bin / lb。当我用 clang -v -lc ++ -x c ++ -stdlib = libc ++ cpptest.cpp 编译时,一切正常。我仍然不明白 clang ++ clang -x c ++ 命令应该有什么不同,但这似乎至少让我的示例程序编译,并似乎表明这些命令与链接器做不同的事情。



解决我的Sublime Text 2问题



这个整个小虫狩猎开始与我试图找出如何获得Sublime Text 2停止抱怨的代码我实际工作,现在我认为我有它。所需要的只是按照此处的说明操作,并将包含设置为

  / usr / lib / c ++ / v1 
/usr/include/c++/4.2.1
/ usr /在中的include / c ++ / 4.2.1 / ext

SublimeClang.sublime-设置文件。

解决方案

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



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


Basic Problem

I have the following code

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

This compiles nicely when I do clang++ -stdlib=libc++ cpptest.cpp. However, the otherwise excellent SublimeClang parser for Sublime Text 2 doesn't seem to understand it. I figured that might be because it uses clang -x c++ instead of clang++, and tried to compile my snippet above using clang -x c++ -std=c++11 -stdlib=libc++ cpptest.cpp (or various permutations of that) instead, but that fails horribly, complaining about the linker.

So my question is, how does clang++ differ from clang -x c++? Is it possible to get the above snippet to compile using clang -x c++ instead of clang++?

My machine is a 2012 MacBook Air running Mountain Lion.

Debugging stuff

Now back at work, I've played a bit more with this, and compared the verbose output of the different versions.

Calling clang -x c++

When I run clang -v -x c++ -stdlib=libc++ cpptest.cpp I get

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)

Calling clang++

When I call clang++ -v -stdlib=libc++ cpptest.cpp I get the following.

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

Differences and solution?

The only major difference I can find is that when I run clang with the -x c++ flag the -lc++ flag does not get added at the end of the line that starts with "/usr/bin/lb". When I instead compile with clang -v -lc++ -x c++ -stdlib=libc++ cpptest.cpp, everything works. I still don't understand how exactly the clang++ and clang -x c++ commands are supposed to differ, but this seems to make my example program compile at least, and seems to indicate that the commands do different things with the linker.

Solution to my Sublime Text 2 problem

This whole little bug hunt was started with me trying to figure out how to get Sublime Text 2 to stop complaining about code I have that actually works, and now I think I've got it. What was needed was simply to follow the instructions here and set the includes to be

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

in the SublimeClang.sublime-settings file.

解决方案

You're right. There are the difference only for linking stage. You could run clang just for compilation with -c flag and you'll get the same result.

Then you adding the -lc++ flag, boths samples are coming the same.

这篇关于C ++ 11代码用“clang ++”编译,但不用“clang -x c ++”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆