【ubuntu】2.c:(.text+0xd2): undefined reference to `pthread_create‘ collect2: error: ld returned

简介: 【ubuntu】2.c:(.text+0xd2): undefined reference to `pthread_create‘ collect2: error: ld returned

编译错误信息:2.c:(.text+0xd2): undefined reference to `pthread_create‘ collect2: error: ld returned 1 exit status

代码部分:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
char buf[200]={0};
void *func(void *arg)
{
  while(1)
  {
    memset(buf,0,sizeof(buf));//初始化内存函数
    printf("before keyboard read.\n");
    read(0,buf,10);
    printf("read by keyboard is [%s]\n",buf);
  }
}
int main(void)
{
  pid_t pid = -1;
  int fd = -1;
  char buf[200]={0};
  int ret = -1;
  pthread_t th = -1;
  ret = pthread_create(&th,NULL,func,NULL); //创建一个新进程
  if(ret != 0)
  {
    printf("pthread_created eror.\n");
    return -1;
  }
  fd = open("/dev/input/mouse0",O_RDWR);
  if(fd < 0)
  { 
    perror("open");
    return -1;
  }
  while(1)
  {
    memset(buf,0,sizeof(buf));//初始化内存函数
    printf("before mouse read.\n");
    read(fd,buf,10);
    printf("read by mouse is [%s]\n",buf);
  }
  return 0;
}

ubuntu环境下运行

解决方案:

在编译文件命令后加上-lphread

示范:gcc 2.c -lpthread

目录
相关文章
|
1月前
|
小程序 前端开发 API
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
|
1月前
|
资源调度 前端开发
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
113 0
|
1月前
|
人工智能 Ubuntu 机器人
【ubuntu】手把手教你解决ubuntu报错openssh-server E: Sub-process /usr/bin/dpkg returned an error code (1)
【ubuntu】手把手教你解决ubuntu报错openssh-server E: Sub-process /usr/bin/dpkg returned an error code (1)
134 0
|
6月前
Uncaught runtime errors: × ERROR Cannot read properties of undefined (reading ‘ vue2&vue-router兼容性问题
Uncaught runtime errors: × ERROR Cannot read properties of undefined (reading ‘ vue2&vue-router兼容性问题
219 0
|
7月前
Fatal error: Call to undefined function openssl_pkey_get_private()
Fatal error: Call to undefined function openssl_pkey_get_private()
43 0
|
6月前
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
|
7月前
|
JavaScript
vue报错: ERROR TypeError: Cannot read property 'version' of undefined
vue报错: ERROR TypeError: Cannot read property 'version' of undefined
49 1
|
7月前
|
编译器
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
Error: undefined method `launchd_service_path‘ for xxx
使用brew命令进行启动就报错。
210 0
|
7月前
|
NoSQL MongoDB
connect-mongo无法连接到mongodb报错Error: failed to connect to [undefined:27017]
connect-mongo无法连接到mongodb报错Error: failed to connect to [undefined:27017]