c – 为什么链接时不解析共享库的符号?

这是我在本网站上的第二篇文章,我努力了解与 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'

所以我的问题是为什么在链接共享库时符号没有得到解决.当我使用这个库来制作可执行文件时,需要完成这项工作(解析其下游的符号),但这意味着我们需要知道这个库在使用这个库时所依赖的是什么,但这不是不可取的吗?

谢谢,
Jagrati

解决方法

在构建库时添加-z defs可以做你想要的吗?如果没有,请查看ld手册页,处理未定义的符号有很多选项.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...