在Linux中未定义的对pthread_create的引用。

时间:2021-08-24 05:28:40

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/

我在web上从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函数。有什么问题吗?

9 个解决方案

#1


522  

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的系统上。安装一个,

gcc -lpthread ...

will fail to link.

将无法链接。

#2


66  

For Linux use:

Linux使用:

gcc -pthread -o term term.c

-pthread tells the compiler to link in the pthread library as well as configure the compilation for threads.

-pthread告诉编译器在pthread库中链接,并为线程配置编译。

Using the -lpthread option only causes the pthread library to be linked - the pre-defined macros don't get defined.

使用-lpthread选项只会导致pthread库被链接——预定义的宏不被定义。

Bottom line: you should use the -pthread option.

底线:您应该使用-pthread选项。

#3


29  

in eclipse

在eclipse中

properties->c/c++Build->setting->GCC C++ linker->libraries in top part add "pthread"

属性->c/c++构建->设置->GCC c++ linker->库在顶部添加“pthread”

#4


14  

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/编译

在Linux中未定义的对pthread_create的引用。

#5


5  

Compile it like this : gcc demo.c -o demo -pthread

像这样编译:gcc演示。c - o演示pthread

#6


4  

You need to use the option -lpthread with gcc.

您需要使用gcc的选项-lpthread。

#7


3  

you need only Add "pthread" in proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> top part "Libraries(-l)". thats it

你只需要添加“pthread”就可以了,比如>C/ c++ build=>GCC c++ Linker=>库=> top部分“库(-l)”。这是它

#8


0  

In Anjuta, go to the Build menu, then Configure Project. In the Configure Options box, add:

在Anjuta中,进入构建菜单,然后配置项目。在Configure选项框中,添加:

LDFLAGS='-lpthread'

Hope it'll help somebody too...

希望它也能帮助别人……

#9


0  

Sometimes, if you use multiple library, check the library dependency. (e.g. -lpthread -lSDL... <==> ... -lSDL -lpthread)

有时,如果您使用多个库,请检查库依赖项。(例如-lpthread -lSDL……< = = >…-lSDL -lpthread)

#1


522  

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的系统上。安装一个,

gcc -lpthread ...

will fail to link.

将无法链接。

#2


66  

For Linux use:

Linux使用:

gcc -pthread -o term term.c

-pthread tells the compiler to link in the pthread library as well as configure the compilation for threads.

-pthread告诉编译器在pthread库中链接,并为线程配置编译。

Using the -lpthread option only causes the pthread library to be linked - the pre-defined macros don't get defined.

使用-lpthread选项只会导致pthread库被链接——预定义的宏不被定义。

Bottom line: you should use the -pthread option.

底线:您应该使用-pthread选项。

#3


29  

in eclipse

在eclipse中

properties->c/c++Build->setting->GCC C++ linker->libraries in top part add "pthread"

属性->c/c++构建->设置->GCC c++ linker->库在顶部添加“pthread”

#4


14  

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/编译

在Linux中未定义的对pthread_create的引用。

#5


5  

Compile it like this : gcc demo.c -o demo -pthread

像这样编译:gcc演示。c - o演示pthread

#6


4  

You need to use the option -lpthread with gcc.

您需要使用gcc的选项-lpthread。

#7


3  

you need only Add "pthread" in proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> top part "Libraries(-l)". thats it

你只需要添加“pthread”就可以了,比如>C/ c++ build=>GCC c++ Linker=>库=> top部分“库(-l)”。这是它

#8


0  

In Anjuta, go to the Build menu, then Configure Project. In the Configure Options box, add:

在Anjuta中,进入构建菜单,然后配置项目。在Configure选项框中,添加:

LDFLAGS='-lpthread'

Hope it'll help somebody too...

希望它也能帮助别人……

#9


0  

Sometimes, if you use multiple library, check the library dependency. (e.g. -lpthread -lSDL... <==> ... -lSDL -lpthread)

有时,如果您使用多个库,请检查库依赖项。(例如-lpthread -lSDL……< = = >…-lSDL -lpthread)