Clang从源代码编译C而不是c++代码
Clang built from source compiles C but not C++ code
我最近在windows上编译了clang (host: x86_64-pc-windows64;编译器:i686-pc-mingw32;目标:i686-pc-mingw32)。
CMakeCache(用于配置)可以在这里找到:
我的问题是,虽然clang工作得很好(对于C), clang++(对于c++)将"成功"编译和链接,但结果程序本身不会运行,并将以错误代码1退出。下面是一个示例(oh-my-zsh):
➜ bin cat test.c
#include <stdio.h>
int main()
{
printf("Hello World!n");
return 0;
}
➜ bin cat test.cpp
#include <iostream>
int main()
{
std::cout<<"Hello World!"<<std::endl;
return 0;
}
➜ bin ./clang++ test.cpp -o a.exe
➜ bin ./clang test.c -o b.exe
➜ bin ./a.exe
➜ bin ./b.exe
Hello World!
➜ bin
如图所示,b.exe(在C中)工作正常,但a.exe (c++)在编译和链接时没有输出。
有没有人提示我为什么这是如此,我怎么能解决它?
注意:clang的预编译快照(也是32位)在我当前的路径配置下工作良好。
注意:a.exe (c++, failed)返回非零。
数据:
铿锵声版本:Snap: clang version 3.5 (208017)
;Comp: clang version 3.4 (tags/RELEASE_34/final)
LLVM文件:快照;编译;diff
预处理文件:快照;编译;diff
ASM文件:快照;编译;diff
详细输出:快照;编制
您的新clang使用不同的(不正确的)调用约定,而不是x86_thiscallcc
。
snap.s
from good clang:
movl $__ZStL8__ioinit, %ecx
calll __ZNSt8ios_base4InitC1Ev
movl %esp, %ecx
movl $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, (%ecx)
movl %eax, %ecx
calll __ZNSolsEPFRSoS_E
与自定义clang相同的代码,comp.s
:
leal __ZStL8__ioinit, %eax
movl %eax, (%esp)
calll __ZNSt8ios_base4InitC1Ev
movl %eax, (%esp)
movl %ecx, 4(%esp)
calll __ZNSolsEPFRSoS_E
和其他几个
在llvm位码(*.ll
文件)中,右调用约定在函数定义和call
指令之后标记为x86_thiscallcc
:
< call void @_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"* @_ZStL8__ioinit)
> call x86_thiscallcc void @_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"* @_ZStL8__ioinit)
< declare void @_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"*) #0
> declare x86_thiscallcc void @_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"*) #0
32c33
< declare void @_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"*) #0
> declare x86_thiscallcc void @_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"*) #0
< call void @_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"* @_ZStL8__ioinit)
> call x86_thiscallcc void @_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"* @_ZStL8__ioinit)
< %3 = call %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %2, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
> %call1 = call x86_thiscallcc %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %call, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
< declare %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"*, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)*) #0
> declare x86_thiscallcc %"class.std::basic_ostream"* @_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"*, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)*) #0
在预处理文件中,我看到了差异。在snap.E
中,许多函数是用__attribute__((__cdecl__))
定义的,而在comp.E
中,它们只用__cdecl__
定义。您应该检查预处理后定义不同的原因。我认为,新的clang可能会预定义不同的宏集(gcc有-dM -E
选项转储预定义的,不知道如何在clang中做到这一点)。或者你的clang只是使用不同的头文件(或者不同版本的头文件,你可以用-H
选项列出clang编译时使用的头文件)。
另一种方法是检查__attribute__((__cdecl__))
是否应该等于__cdecl__
,以及新版本的clang是否在处理它们时改变了任何东西
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值