clang似乎使用了gcc

clang似乎使用了gcc,gcc,clang++,Gcc,Clang++,当我使用clang编译一个简单的hello world程序时,在elf64文件的注释中,我仍然可以找到与GCC相关的信息。为什么?我用的是叮当声,不是gcc 我使用的是ubuntu 16.04 username@ubuntu:~$ clang++ -stdlib=libc++ test.cpp username@ubuntu:~$ objdump --full-contents --section=.comment a.out a.out: file format elf64-x8

当我使用clang编译一个简单的hello world程序时,在elf64文件的注释中,我仍然可以找到与GCC相关的信息。为什么?我用的是叮当声,不是gcc

我使用的是ubuntu 16.04

username@ubuntu:~$ clang++ -stdlib=libc++ test.cpp 
username@ubuntu:~$ objdump --full-contents --section=.comment a.out 

a.out:     file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 20285562 756e7475 20352e34  GCC: (Ubuntu 5.4
 0010 2e302d36 7562756e 7475317e 31362e30  .0-6ubuntu1~16.0
 0020 342e3429 20352e34 2e302032 30313630  4.4) 5.4.0 20160
 0030 36303900 636c616e 67207665 7273696f  609.clang versio
 0040 6e20342e 302e3020 28746167 732f5245  n 4.0.0 (tags/RE
 0050 4c454153 455f3430 302f6669 6e616c29  LEASE_400/final)
 0060 00                                   .               
username@ubuntu:~$ 
test.cpp是:

#include <iostream>

int main(int argc, char* argv[])
{
std::cout << argc << std::endl;

return 0;
}
#包括
int main(int argc,char*argv[])
{

这是因为您正在链接一个用GCC构建的C运行时(来自Glibc),而链接器正在将两者合并在一起

$ clang++ -c --stdlib=libc++ test.cpp
$ objdump --full-contents --section=.comment test.o

test.o:     file format elf64-x86-64

Contents of section .comment:
 0000 00636c61 6e672076 65727369 6f6e2034  .clang version 4
 0010 2e302e30 20287461 67732f52 454c4541  .0.0 (tags/RELEA
 0020 53455f34 30302f66 696e616c 2900      SE_400/final).  
$ clang++ --verbose test.o
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/crtbegin.o -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib test.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/crtend.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crtn.o
$ objdump --full-contents --section=.comment /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o

/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o:     file format elf64-x86-64

Contents of section .comment:
 0000 00474343 3a202847 4e552920 362e332e  .GCC: (GNU) 6.3.
 0010 31203230 31373033 303600             1 20170306.     
$ objdump --full-contents --section=.comment a.out

a.out:     file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 2028474e 55292036 2e332e31  GCC: (GNU) 6.3.1
 0010 20323031 37303330 3600636c 616e6720   20170306.clang 
 0020 76657273 696f6e20 342e302e 30202874  version 4.0.0 (t
 0030 6167732f 52454c45 4153455f 3430302f  ags/RELEASE_400/
 0040 66696e61 6c2900                      final).         

我的系统版本与您的略有不同,但您可以看到,Clang编译对象中只包含Clang标识符,C运行时对象中包含GCC标识符,最终二进制文件中同时包含这两个标识符。

您也应该查看Clang源代码。这很有趣;)
$ clang++ -c --stdlib=libc++ test.cpp
$ objdump --full-contents --section=.comment test.o

test.o:     file format elf64-x86-64

Contents of section .comment:
 0000 00636c61 6e672076 65727369 6f6e2034  .clang version 4
 0010 2e302e30 20287461 67732f52 454c4541  .0.0 (tags/RELEA
 0020 53455f34 30302f66 696e616c 2900      SE_400/final).  
$ clang++ --verbose test.o
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/crtbegin.o -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib test.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/crtend.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crtn.o
$ objdump --full-contents --section=.comment /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o

/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib64/crt1.o:     file format elf64-x86-64

Contents of section .comment:
 0000 00474343 3a202847 4e552920 362e332e  .GCC: (GNU) 6.3.
 0010 31203230 31373033 303600             1 20170306.     
$ objdump --full-contents --section=.comment a.out

a.out:     file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 2028474e 55292036 2e332e31  GCC: (GNU) 6.3.1
 0010 20323031 37303330 3600636c 616e6720   20170306.clang 
 0020 76657273 696f6e20 342e302e 30202874  version 4.0.0 (t
 0030 6167732f 52454c45 4153455f 3430302f  ags/RELEASE_400/
 0040 66696e61 6c2900                      final).