多线程编程时用到函数库 pthread.h ,但是该函数库不是linux默认的函数库,所以编译c文件时,需要在目标代码文件后加上 -lpthread参数。

1.未加上 -lpthread 编译时,报错如下:

lyr@ubuntu:~/Desktop/lyr/test$ gcc multiTread.c
/tmp/cc2opFxw.o:在函数‘thread_create’中:
multiTread.c:(.text+0x153):对‘pthread_create’未定义的引用
multiTread.c:(.text+0x18b):对‘pthread_create’未定义的引用
/tmp/cc2opFxw.o:在函数‘thread_wait’中:
multiTread.c:(.text+0x1d5):对‘pthread_join’未定义的引用
multiTread.c:(.text+0x1ff):对‘pthread_join’未定义的引用
collect2: error: ld returned 1 exit status

2.正确命令:

gcc multiTread.c -lpthread

其中 multiTread.c是自己的代码文件的名字。

编译成功!!!

相关文章:

  • 2021-11-11
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-02-24
  • 2021-08-07
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2021-05-21
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案