微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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手册页,处理未定义的符号有很多选项.

原文地址:https://www.jb51.cc/c/118585.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐