Linux 链接 pthread 库问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7419163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linking pthread library issue
提问by Lipika Deka
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 等。
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
谢谢
采纳答案by nos
On linux, pthread functions live in the libpthread library. So you have to link to that.
在 linux 上,pthread 函数位于 libpthread 库中。所以你必须链接到那个。
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.
使用 pthread 时,正确的方法是使用 编译和链接-pthread
,除其他外,它将链接到 pthread 库中。您有-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 进行链接。)