Linux学习-线程的创建和终止-(出现错误-undefined reference to 'pthread_create')

在下面的例程中创建了5个线程,这 5 个线程和主线程并发执行,主线程创建完线程后调用 pthread_exit()函数退出线程,其它线程分别打印当前线程的序号。当主线程先于其它进程执行 pthread_exit()时,进程不会退出,而是最后一个线程完成时才会进程退出。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.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++){ /* 循环创建 5 个线程 */
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);
}
}
printf("In main: exit!\n");
pthread_exit(NULL); /* 主线程退出 */
return 0;
}


程序中主线程调用了 pthread_exit()函数并不会将整个进程终止,而是最后一个线程调用 pthread_exit()时程序才完成运行。


    用GCC进行编译:

gcc a.c -o a

    出现错误:

undefined reference to 'pthread_create';

    解决方法:这里是没有链接到库,应该添加-lpthread参数,GCC编译的语句应该为:

gcc a.c -o a -lpthread

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 当在使用gcc编译程序时,出现"undefined reference to pthread_create"的错误信息时,很可能是由于程序中使用了多线程功能,并且没有正确链接pthread库。 解决这个问题的方法是,在编译程序时添加-lpthread选项来链接pthread库。具体步骤如下: 1. 打开终端或命令提示符窗口,进入到源代码所在的目录。 2. 使用gcc编译程序,并加上-lpthread选项,例如: $ gcc source.c -lpthread -o executable 3. 执行上述命令后,重新编译程序,并生成一个可执行文件。 4. 如果编译成功,可执行文件将会生成在当前目录中。 需要注意的是,-lpthread选项必须位于源代码文件名之后,确保正确链接了pthread库。 此外,还有一些特殊情况可能导致该错误出现,例如: - 没有正确包含pthead.h头文件:需要在源代码文件中添加#include <pthread.h>以引入pthread库的函数声明和定义。 - 编译器或系统缺少pthread库:需要通过安装相应的开发包或更新编译器来解决该问题。 总之,gcc undefined reference to pthread_create错误的原因是缺少对pthread库的连接。通过添加-lpthread选项,重新编译程序可以解决该问题。 ### 回答2: "undefined reference to `pthread_create'" 是一个编译错误,它通常指的是在编译时缺少了与 `pthread_create` 函数的链接。 这种错误通常是由于以下几个原因引起的: 1. 缺少对 `pthread` 库的链接:在编译程序时,需要使用 `-pthread` 或 `-lpthread` 选项来链接 `pthread` 库。例如,在使用 `gcc` 编译时,可以使用命令 `gcc -pthread main.c -o main` 来链接 `pthread` 库。 2. 未正确包含 `pthread.h` 头文件:如果在程序中使用了 `pthread_create` 函数,那么需要在程序中包含 `pthread.h` 头文件。确保在使用 `pthread_create` 函数之前,包含了正确的头文件。 3. 缺少 POSIX thread 库:`pthread` 是 POSIX 线程库的一部分。因此,如果编译环境不支持 POSIX 线程库,就会出现错误。在这种情况下,需要检查编译环境是否支持 POSIX 线程库,并使用适当的方法安装。 综上所述,当出现 `gcc undefined reference to pthread_create` 错误时,需要确保正确地链接 `pthread` 库,并正确包含 `pthread.h` 头文件。同时,还要检查编译环境是否支持 POSIX 线程库。 希望以上回答对您有所帮助! ### 回答3: gcc undefined reference to pthread_create是一个常见的错误信息,它表示在使用gcc编译时缺少对pthread_create函数的引用或链接。 pthread_create函数属于POSIX线程库(libpthread),用于创建一个新的线程。当程序中使用了pthread_create函数但未正确引用或链接线程库时,就会出现错误。 要解决这个问题,我们需要在编译命令中添加对线程库的引用。可以使用"-lpthread"选项来告诉gcc链接线程库。 以下是一个示例的编译命令: gcc -o my_program my_program.c -lpthread 在上面的命令中,"-o my_program"指定了输出文件的名称为"my_program","my_program.c"是需要编译的源代码文件,"-lpthread"告诉gcc链接线程库。 通过在编译命令中添加"-lpthread"选项,就可以解决gcc undefined reference to pthread_create错误。另外,还需要确保系统中已经正确安装了POSIX线程库,否则可能会出现其他问题。 总结一下,要解决gcc undefined reference to pthread_create错误,需要在编译命令中添加对线程库的引用,使用"-lpthread"选项来链接线程库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值