引用请注明出处:http://blog.youkuaiyun.com/int64ago/article/details/7395418
一个可执行程序的形成,大致的步骤:编译——>链接——>可执行文件,然而,犯错最多的地方可能你以为是编译阶段,但是,实际上是链接阶段,特别是大型工程。下面用一个例子简单说下出现链接错误的一个简单解决方式,先看两个程序:
- //main.c
- void test1();
- void test2();
- int main()
- {
- test1();
- test2();
- return 0;
- }
- //test.c
- #include <stdio.h>
- static void test1()
- {
- printf("This is test1\n");
- }
- void test2()
- {
- printf("This is test2\n");
- test1();
- }
这两个简单的程序也没干啥,就是用来测试的,先编译:
gcc -c main.c
gcc -c test.c
没有错误,生成了main.o 和 test.o两个目标文件,接下来链接:
gcc -o main main.o test.o
出现了下面的错误:
main.o: In function `main':
main.c:(.text+0x7): undefined reference to `test1'
collect2: ld returned 1 exit status
意思是没有定义test1,可能此时你已经知道了错误的原因及地方,可以改了,但是试想一下如果工程巨大,很多地方用了test1,你这时就需要另外方法了(除非你用的是IDE,这里我们不讨论)。再仔细看看,还给了另一条信息:main.c:(.text+0x7),这就是出错位置,但是这时汇编的位置,所以我们要深入汇编,可以用下面的命令:
gcc -c -g -Wa,-adlhn main.c >/dev/null
objdump -S main.o >log
查看log:
- main.o: file format elf32-i386
- Disassembly of section .text:
- 00000000 <main>:
- void test1();
- void test2();
- int main()
- {
- 0: 55 push %ebp
- 1: 89 e5 mov %esp,%ebp
- 3: 83 e4 f0 and $0xfffffff0,%esp
- test1();
- 6: e8 fc ff ff ff call 7 <main+0x7>
- test2();
- b: e8 fc ff ff ff call c <main+0xc>
- return 0;
- 10: b8 00 00 00 00 mov $0x0,%eax
- }
- 15: c9 leave
- 16: c3 ret