【发布时间】:2010-07-01 15:45:14
【问题描述】:
这是我在此站点上的第二篇文章,旨在了解 gcc 的编译/链接过程。当我尝试制作可执行文件时,需要在链接时解析符号,但是当我尝试制作共享库时,符号不会在链接时解析该库。当我尝试使用此共享库制作可执行文件时,它们可能会得到解决。动手:
bash$ cat printhello.c
#include <stdio.h>
//#include "look.h"
void PrintHello()
{
look();
printf("Hello World\n");
}
bash$ cat printbye.c
#include <stdio.h>
//#include "look.h"
void PrintBye()
{
look();
printf("Bye bye\n");
}
bash$ cat look.h
void look();
bash$ cat look.c
#include <stdio.h>
void look()
{
printf("Looking\n");
}
bash$ gcc printhello.c printbye.c
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cck21S0u.o: In function `PrintHello':
printhello.c:(.text+0x7): undefined reference to `look'
/tmp/ccNWbCnd.o: In function `PrintBye':
printbye.c:(.text+0x7): undefined reference to `look'
collect2: ld returned 1 exit status
bash$ gcc -Wall -shared -o libgreet printhello.c printbye.c
printhello.c: In function 'PrintHello':
printhello.c:6: warning: implicit declaration of function 'look'
printbye.c: In function 'PrintBye':
printbye.c:5: warning: implicit declaration of function 'look'
所以我的问题是为什么在链接共享库时符号没有解析。当我将使用这个库来制作可执行文件时,需要完成这项工作(解析其下游的符号),但这意味着我们需要知道在使用这个库时这个库依赖于什么,但这不是不可取的吗?
谢谢, 贾格拉蒂
【问题讨论】: