简体   繁体   English

链接pthread库问题

[英]linking pthread library issue

Am facing a problem that may be slightly complicated to explain and understand as giving the entire picture would be too big and difficult. 面对一个可能稍微复杂的问题,解释和理解,因为给出整个画面会太大而且困难。

Please excuse me for it. 请原谅我。

Consider the following Makefile: 考虑以下Makefile:

all: clients.so simulator backup
    LD_PRELOAD=/home/Juggler/client/clients.so ./simulator

backup: backup.c libclient.a
    gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl
simulator: simulator.c libclient.a
    gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread
libclient.a: libclient.o client.o
    ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c  
    gcc -c libclient.c -o libclient.o -pthread

clients.so: client.o client_invoke.o
    ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
    gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
    gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread

We call function written in client.c from libclient.c and these functions in client.c make call to pthread_key_create(), pthread_setspecific..etc. 我们从libclient.c调用client.c中编写的函数,并在client.c中调用这些函数调用pthread_key_create(),pthread_setspecific..etc。

Threads are created by simulator.c and theses threads access functions written in he other files. 线程由simulator.c创建,这些线程访问其他文件中写入的函数。

On doing make...Errors like the following appear. 在做make ...出现如下错误。

/home/Juggler/client/libclient.a(client.o):In function 'setup_connection':
/home/Juggler/client/client.c:35: undefined reference to 'pthread_setspecific'

pthread.h has been included in both client.c and libclient.c pthread.h已包含在client.c和libclient.c中

Would be grateful for anypointers . 对任何指针都会感激不尽。 I understand information is very less... 我理解的信息非常少......

Thanks 谢谢

On linux, pthread functions live in the libpthread library. 在linux上,pthread函数存在于libpthread库中。 So you have to link to that. 所以你必须链接到那个。

The proper way, when using pthreads, is to compile and link using the -pthread , which, among other things, will link in the pthread library. 使用pthread时,正确的方法是使用-pthread进行编译和链接,其中, -pthread将链接到pthread库中。 You have the -pthread flag for some of your executables, but not for others, and not for your clients.so library, so add the flag where required. 您有一些可执行文件的-pthread标志,但不适用于其他可执行文件,而不是您的clients.so库,因此请在需要的地方添加标记。

Also, remember, when you are creating a shared library, you should compile the source files with the -fPIC flag. 另外,请记住,在创建共享库时,应使用-fPIC标志编译源文件。

(And, seems you are calling ld directly to produce the client.so library, you really should use gcc to do the linking.) (并且,似乎你直接调用ld来生成client.so库,你真的应该使用gcc来进行链接。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM