gpt4 book ai didi

c - 当我的 main.c 不使用 pthreads 时,为什么我必须在 main.c 编译中显式链接到 pthreads?

转载 作者:行者123 更新时间:2023-12-02 08:49:21 25 4
gpt4 key购买 nike

在 Linux 中,我创建了一个使用 pthreads 的共享库和一个不使用 pthreads 的 main.c。

libpthread.so 出现在我的共享库的 ldd 中,这是正确的。

$ ldd libmapreduce.so.1.0 
linux-gate.so.1 => (0x0067d000)
libpthread.so.0 => /lib/libpthread.so.0 (0x0058c000)
[...]

但是当我编译并链接不使用 pthreads 的 main.c 到使用 pthreads 的共享库时,我看到:

$ icc -Wall -o main main.c -lmapreduce
/opt/intel/Compiler/11.1/046/lib/ia32/libiomp5.so: undefined reference to `pthread_atfork'

将 -lpthread 添加到我的编译命令中,即

$ icc -Wall -o main main.c -lmapreduce -lpthread

解析 undefined reference 。

当我的 main.c 不使用它并且我的共享库已经链接了 libpthread 时,为什么我需要显式链接到 libpthread?

最佳答案

为了创建可执行文件或 DLL,您需要链接程序中所有依赖项的传递闭包。因为 main.c 链接在 sharedlib 中,所以您还必须链接 sharedlib 的所有依赖项,其中包括 pthreads。

关于c - 当我的 main.c 不使用 pthreads 时,为什么我必须在 main.c 编译中显式链接到 pthreads?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9689710/

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