当main.c不使用pthreads时,为什么必须在main.c编译中显式链接到pthreads? [英] Why do I have to explicitly link to pthreads in my main.c compilation when my main.c does not use pthreads?

查看:131
本文介绍了当main.c不使用pthreads时,为什么必须在main.c编译中显式链接到pthreads?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,我有一个共享库,该库使用pthreads,而main.c不使用.

In Linux, I have a shared library I made that uses pthreads and a main.c that does not.

libpthread.so显示在我的共享库的一小部分中,这是正确的.

libpthread.so shows up in an ldd of my shared library, which is correct.

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

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

But when I compile and link my main.c that does not use pthreads to my shared library that does, I see:

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

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

Adding -lpthread to my compile command, i.e.,

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

解析未定义的引用.

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

Why do I need to explicitly link to libpthread when my main.c does not use it and my shared library already has libpthread linked in?

推荐答案

感谢R ..和Pavan Manjunath,鼓励我继续努力.

Thank you R.. and Pavan Manjunath, for encouraging me to keep digging.

共享库libmapreduce.so的链接步骤如下:

The link step for the shared library libmapreduce.so looked like:

icc -shared -g -o libmapreduce.so.1.0 map.o map_wrp.o -openmp [...] -lpthread -ldl

不需要-openmp链接标志,并且实际上引入了对pthread_atfork的未定义引用.在我尝试将main.c与共享库libmapreduce.so链接之前,未显示对pthread_atfork的未定义引用.在不使用-openmp标志的情况下重新创建libmapreduce.so可以解决此问题.

That -openmp link flag was not needed and in fact introduced the undefined reference to pthread_atfork. The undefined reference to pthread_atfork did not show up until I tried to link a main.c with the shared library libmapreduce.so. Re-creating libmapreduce.so without the -openmp flag removed the problem.

这篇关于当main.c不使用pthreads时,为什么必须在main.c编译中显式链接到pthreads?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆