linking pthread library issue

两盒软妹~` 提交于 2020-04-13 06:01:11

问题


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:

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.

Threads are created by simulator.c and theses threads access functions written in he other files.

On doing make...Errors like the following appear.

/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

Would be grateful for anypointers . I understand information is very less...

Thanks


回答1:


On linux, pthread functions live in the libpthread library. 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. 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.

Also, remember, when you are creating a shared library, you should compile the source files with the -fPIC flag.

(And, seems you are calling ld directly to produce the client.so library, you really should use gcc to do the linking.)



来源:https://stackoverflow.com/questions/7419163/linking-pthread-library-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!