简体   繁体   English

gcc为不同的文件名生成不同的结果

[英]gcc generates different results for different filenames

Why does gcc generate different executables for different sourcefilenames? 为什么gcc为不同的源文件名生成不同的可执行文件?

to test I have this c-programm called test.c and test2.c: 测试我有这个名为test.c和test2.c的c-programm:

int main(){}

"gcc test.c -o test" and "gcc test2.c -o test2" generate different output files. “gcc test.c -o test”和“gcc test2.c -o test2”生成不同的输出文件。 Using a hex-editor I can see that there still is its source-filename hidden in it. 使用十六进制编辑器,我可以看到它的源文件名仍然隐藏在其中。 Stripping the files still results in different results (the source-filename is gone). 剥离文件仍然会导致不同的结果(源文件名已消失)。 Why does gcc operate this way? 为什么gcc以这种方式运作? I tested clang and tcc as well. 我也测试了clang和tcc。 Clang behaves the like gcc does, whereas tcc generates the same results for different filenames? Clang表现得像gcc那样,而tcc为不同的文件名生成相同的结果?

gcc version 4.9.1 (Debian 4.9.1-1) gcc版本4.9.1(Debian 4.9.1-1)

clang 3.4.2-4 铿锵3.4.2-4

tcc version 0.9.25 tcc版本0.9.25

Doing a diff on the hexdump of both binaries shows a small difference at around offset 0x0280 . 在两个二进制文件的hexdump上执行diff在偏移量0x0280附近显示一个小的差异。 Looking through the sections (via objdump -x ), the differences appear in the .note.gnu.build-id section. 查看各节(通过objdump -x ),差异出现在.note.gnu.build-id部分。 My guess is that this provides some sort of UUID for distinguishing different builds of otherwise similar code, as well as validate debug info (referenced here , about a third of the way down). 我的猜测是,这提供了某种UUID,用于区分其他类似代码的不同构建,以及验证调试信息( 此处引用,大约三分之一)。

The -o option of gcc is to specify the output file. gcc的-o选项是指定输出文件。 If you give him different -o targets, it will generate different files. 如果你给他不同的-o目标,它将生成不同的文件。

gcc test.c -o foo gcc test.c -o foo

And you have a foo executable. 你有一个foo可执行文件。

Also, note that without a -o option, gcc will output a a.out executable. 另请注意,如果没有-o选项,gcc将输出a.out可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM