【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

目录
相关文章
|
Ubuntu Linux
Win10 Ubuntu子系统(内嵌ubuntu18.04)运行32bit Linux原生程序 解决Exec format error错误
Win10 Ubuntu子系统(内嵌ubuntu18.04)运行32bit Linux原生程序 解决Exec format error错误
225 0
|
16天前
|
Ubuntu 开发工具 git
Ubuntu编译ffmpeg解决错误:ERROR: avisynth/avisynth_c.h not found
通过本文的详细指导,您可以顺利地在Ubuntu系统上配置和编译FFmpeg,并解决Avisynth头文件缺失的问题。
74 27
|
9月前
|
小程序 前端开发 API
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
|
4月前
|
Ubuntu Linux Windows
wsl重装Ubuntu遇到的一些问题( WslRegisterDistribution failed with error: 0x80041002 Error: 0x80041002 (null)、重置网络后WLAN图标消失)
wsl重装Ubuntu遇到的一些问题( WslRegisterDistribution failed with error: 0x80041002 Error: 0x80041002 (null)、重置网络后WLAN图标消失)
|
5月前
|
存储 API Windows
MASM32连接程序时error A2006: undefined symbol : u
MASM32连接程序时error A2006: undefined symbol : u
|
9月前
|
资源调度 前端开发
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
556 0
|
9月前
|
人工智能 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)
913 0
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兼容性问题
548 0
Fatal error: Call to undefined function openssl_pkey_get_private()
Fatal error: Call to undefined function openssl_pkey_get_private()
94 0
Error: undefined method `launchd_service_path‘ for xxx
使用brew命令进行启动就报错。
251 0