【发布时间】:2018-08-12 08:00:27
【问题描述】:
为了理解编译过程,我试图得到一个简单程序的可执行文件,分别编译和链接。
#include<stdio.h>
int main(void)
{
printf("Hello! This is a test prgoram.\n");
return 0;
}
有
gcc -o hello hello.c
一切正常,并生成了具有预期输出的所需可执行文件。
你好
制作:
linux-vdso.so.1 => (0x00007ffce53ee000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f923498b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9234d55000)
然后分别尝试每个步骤:
cpp -v hello.c > hello.i
为了得到预处理后的输出,那么,
gcc -S hello.i
然后,要获得汇编代码,
as -o hello.o hello.s
获取目标代码。直到这一步看起来一切正常,但是,
ld --verbose -o hello hello.o -lc -l:ld-linux-x86-64.so.2
我明白了:
attempt to open hello.o succeeded
hello.o
attempt to open //usr/local/lib/x86_64-linux-gnu/libc.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libc.a failed
attempt to open //lib/x86_64-linux-gnu/libc.so failed
attempt to open //lib/x86_64-linux-gnu/libc.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libc.so succeeded
opened script file //usr/lib/x86_64-linux-gnu/libc.so
opened script file //usr/lib/x86_64-linux-gnu/libc.so
attempt to open /lib/x86_64-linux-gnu/libc.so.6 succeeded
/lib/x86_64-linux-gnu/libc.so.6
attempt to open /usr/lib/x86_64-linux-gnu/libc_nonshared.a succeeded
attempt to open /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 succeeded
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
attempt to open //usr/local/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 failed
attempt to open //usr/local/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 failed
attempt to open //lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 succeeded
-l:ld-linux-x86-64.so.2 (//lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400280
显然它成功打开了库和目标文件,但未能将它们全部链接。
版本:
gcc 版本 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) GNU ld(Ubuntu 的 GNU Binutils)2.26.1
我是否在此架构中遗漏了某些东西(库、选项等)?
问候。
【问题讨论】:
-
试试
gcc -v hello.c看看它能做什么。
标签: c gcc linker-errors ld