加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11代码用`clang`编译,但不用`clang -x c`编译

发布时间:2020-12-16 05:02:30 所属栏目:百科 来源:网络整理
导读:基本问题 我有以下代码 #include iostream#include cstdintusing 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的优
基本问题

我有以下代码

#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 -xc而不是clang,并尝试使用clang -xc -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 -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时,-c标志不会在以“/usr/bin/lb”开头的行的末尾添加.当我用clang -v -lc -x c -stdlib = libc cpptest.cpp进行编译时,一切正常.我仍然不明白clang和clang -x c命令究竟是如何区别的,但这似乎使我的示例程序至少编译,并且似乎表明这些命令与链接器做了不同的事情.

解决我的Sublime Text 2问题

这整个小虫狩猎开始于我试图弄清楚如何让Sublime Text 2停止抱怨我实际上有效的代码,现在我想我已经得到了它.所需要的只是按照指令here并将包含设置为

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

在SublimeClang.sublime-settings文件中.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读