在 Linux 中对 pthread_create 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1662909/
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
Undefined reference to pthread_create in Linux
提问by Ralph
I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/
我从https://computing.llnl.gov/tutorials/pthreads/摘取了以下演示
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
But when I compile it on my machine (running Ubuntu Linux 9.04) I get the following error:
但是当我在我的机器上编译它(运行 Ubuntu Linux 9.04)时,我收到以下错误:
corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main':
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit'
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
This doesn't make any sense to me, because the header includes pthread.h
, which should have the pthread_create
function. Any ideas what's going wrong?
这对我来说没有任何意义,因为标题包括pthread.h
,它应该具有该pthread_create
功能。任何想法出了什么问题?
回答by Employed Russian
Both answers to this question so far are incorrect.
For Linux the correct command is:
到目前为止,这个问题的两个答案都不正确。
对于 Linux,正确的命令是:
gcc -pthread -o term term.c
In general, libraries should follow sources and objects on command line, and -lpthread
is not an "option", it's a library specification. On a system with only libpthread.a
installed,
一般来说,库应该在命令行上遵循源和对象,而-lpthread
不是一个“选项”,它是一个库规范。在仅libpthread.a
安装的系统上,
gcc -lpthread ...
will fail to link.
将无法链接。
回答by xiaolong
You need to use the option -lpthread
with gcc.
您需要在-lpthread
gcc 中使用该选项。
回答by sam
you need only Add "pthread" in proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> top part "Libraries(-l)". thats it
您只需要在proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> 顶部“Libraries(-l)”中添加“pthread”。就是这样
回答by sam
in eclipse
在日食
properties->c/c++Build->setting->GCC C++ linker->libraries in top part add "pthread"
属性->c/c++Build->setting->GCC C++ 链接器->顶部的库添加“pthread”
回答by naXa
In Anjuta, go to the Build menu, then Configure Project. In the Configure Options box, add:
在 Anjuta 中,转到 Build 菜单,然后选择 Configure Project。在配置选项框中,添加:
LDFLAGS='-lpthread'
Hope it'll help somebody too...
希望它也能帮助别人...
回答by user1793997
Sometimes, if you use multiple library, check the library dependency. (e.g. -lpthread -lSDL... <==> ... -lSDL -lpthread)
有时,如果您使用多个库,请检查库依赖项。(例如 -lpthread -lSDL... <==> ... -lSDL -lpthread)
回答by dylanninin
Acutally, it gives several examples of compile commands used for pthreads codes are listed in the table below, if you continue reading the following tutorial:
实际上,如果您继续阅读以下教程,它给出了用于 pthreads 代码的几个编译命令示例,如下表所示:
https://computing.llnl.gov/tutorials/pthreads/#Compiling
https://computing.llnl.gov/tutorials/pthreads/#Compiling
回答by leesagacious
Compile it like this : gcc demo.c -o demo -pthread
像这样编译它:gcc demo.c -o demo -pthread
回答by Jayhello
If you are using cmake, you can use:
如果您使用的是 cmake,则可以使用:
add_compile_options(-pthread)
Or
或者
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
回答by Alexander Nenartovich
Running from the Linux terminal, what worked for me was compiling using the following command (suppose the c file I want to compile is called test.c):
从 Linux 终端运行,对我有用的是使用以下命令进行编译(假设我要编译的 c 文件名为 test.c):
gcc -o test test.c -pthread
Hope it helps somebody!
希望它可以帮助某人!