链接pthread库问题

链接pthread库问题

本文介绍了链接pthread库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面临的问题可能难以解释和理解,因为给出整个图片会太大而又困难.

Am facing a problem that may be slightly complicated to explain and understand as giving the entire picture would be too big and difficult.

请原谅我.

考虑以下Makefile:

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

我们从libclient.c中调用用client.c编写的函数,而client.c中的这些函数调用pthread_key_create(),pthread_setspecific..etc.

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.

线程是由Simulator.c创建的,这些线程访问其他文件中编写的函数.

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已包含在client.c和libclient.c

pthread.h has been included in both client.c and libclient.c

将感谢任何指针.我了解信息很少...

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

谢谢

推荐答案

在linux上,pthread函数位于libpthread库中.因此,您必须链接到该页面.

On linux, pthread functions live in the libpthread library. So you have to link to that.

使用pthreads时,正确的方法是使用-pthread进行编译和链接,而cc将链接到pthread库中.对于某些可执行文件,您具有-pthread标志,但对于其他可执行文件却没有,对于clients.so库,没有-pthread标志,因此请在需要的位置添加该标志.

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.

另外,请记住,在创建共享库时,应使用-fPIC标志编译源文件.

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

(而且,似乎您正在直接调用ld来生成client.so库,您确实应该使用gcc进行链接.)

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

这篇关于链接pthread库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:35