gpt4 book ai didi

使用 GNU 工具链 (gcc/ld) 从静态库创建共享库

转载 作者:IT王子 更新时间:2023-10-28 23:58:59 25 4
gpt4 key购买 nike

我在从静态库生成共享对象时遇到问题。虽然我知道还有其他选择,但我现在对为什么它不起作用以及如何让它起作用感到困扰(而不是陷入困境)。

下面是我正在使用的非常简单的源代码。

get_zero.c

#include "get_zero.h"

int
get_zero(void)
{
return 0;
}

get_zero.h

int get_zero(void);

主.c

#include <stdio.h>
#include <string.h>

#include "get_zero.h"

int
main(void)
{
return get_zero();
}

目标是使用 libget_zero_static 和 libget_zero_shared 创建两个功能相同的应用程序。

这是我的编译/链接步骤:

gcc -c -fPIC get_zero.c
ar cr libget_zero_static.a get_zero.o
gcc -shared -o libget_zero_shared.so -L. -Wl,--whole-archive -lget_zero_static -Wl,-no--whole-archive

这是我得到的错误:

/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libc.a(init-first.o): relocation R_X86_64_32 against `_dl_starting_up' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libc.a(init-first.o): could not read symbols: Bad value
collect2: ld returned 1 exit status

这是在 64 位 Ubuntu 系统上。

我在这里阅读了有关整个存档选项的信息,看来这个问题应该已经消除了我所有的障碍。 How to create a shared object file from static library .

最佳答案

看来您需要将存档指定为参数,而不是库。所以制作 libget_zero_static.a 而不是 -lget_zero_static。至少它对我有用:

gcc -shared -o libget_zero_shared.so \
-Wl,--whole-archive \
libget_zero_static.a \
-Wl,--no-whole-archive

关于使用 GNU 工具链 (gcc/ld) 从静态库创建共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13236987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com