线程异常:undefined reference to 'pthread_create' 处理
源代码:
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
void *producter_f (void *arg);
void *consumer_f (void *arg);
int buffer_has_item=0;
pthread_mutex_t mutex;
int running =1 ;
int main (void)
{
pthread_t consumer_t;
pthread_t producter_t;
pthread_mutex_init (&mutex,NULL);
pthread_create(&producter_t, NULL,(void*)producter_f, NULL );
pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);
usleep(1);
running =0;
pthread_join(consumer_t,NULL);
pthread_join(producter_t,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
void *producter_f (void *arg)
{
while(running)
{
pthread_mutex_lock (&mutex);
buffer_has_item++;
printf("生产,总数量:%d\n",buffer_has_item);
pthread_mutex_unlock(&mutex);
}
}
void *consumer_f(void *arg)
{
while(running)
{
pthread_mutex_lock(&mutex);
buffer_has_item--;
printf("消费,总数量:%d\n",buffer_has_item);
pthread_mutex_unlock(&mutex);
}
}
错误场景:
[root@luozhonghua 04]# gcc -o mutex ex04-5-mutex.c
/tmp/ccZuFiqr.o: In function `main':
ex04-5-mutex.c:(.text+0x3d): undefined reference to `pthread_create'
ex04-5-mutex.c:(.text+0x61): undefined reference to `pthread_create'
ex04-5-mutex.c:(.text+0x8b): undefined reference to `pthread_join'
ex04-5-mutex.c:(.text+0x9f): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
分析:pthread 库不是 Linux 系统默认的库,连接时须要使用静态库 libpthread.a
处理:
在编译中加 -lpthread 參数
[root@luozhonghua 04]# gcc -lpthread -o mutex ex04-5-mutex.c
线程异常:undefined reference to 'pthread_create' 处理的更多相关文章
- (笔记)Linux线程编译undefined reference to 'pthread_create'
在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include &l ...
- 在linux下编译线程程序undefined reference to `pthread_create'
由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...
- openCV中 libopencv-nonfree-dev的安装: undefined reference to `cv::initModule_nonfree()'
今天照着一起做RGB-D SLAM (3) , 程序会出现以下的错误: cv::initModule_nonfree(); /home/yhzhao/slam/src/detectFeature ...
- undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' 的修改方法
在编译DSO代码的时候会如下这样的问题: 检查DSO,在程序中没有用到pthread,但是在编译的时候却出现此类问题.仔细想了想了一下,在程序中用到了C++11中的线程std::thread,个人猜测 ...
- undefined reference to symbol' pthread_create@@GLIBC_2.2.5'
我在ubuntu16.04上迁移工程,遇到了这个错误. pthread库不是Linux系统默认的库,链接时需要添加-pthread参数. 这里注意是链接那一步添加-pthread,而不是编译选项.
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...
- Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误。
Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误. ...
- 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)
问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...
- undefined reference to `sin'问题解决
作者:zhanhailiang 日期:2014-10-25 使用gcc编译例如以下代码时报"undefined reference to `sin'": #include < ...
随机推荐
- android高仿微信UI点击头像显示大图片效果
用过微信的朋友朋友都见过微信中点击对方头像显示会加载大图,先贴两张图片说明下: 这种UI效果对用户的体验不错,今天突然有了灵感,试着去实现,结果就出来了.. 下面说说我的思路: 1.点击图片时跳转到另 ...
- JavaScripts学习日记——DOM SAX JAXP DEMO4J XPath
今日关键词: XML解析器 DOM SAX JAXP DEMO4J XPath XML解析器 1.解析器概述 什么是解析器 XML是保存数据的文件,XML中保存的数据也需要被程序读取然后使用.那么程序 ...
- javaScript模块化一
1. Module模式的基本特性 A) 模块化 可重用 B) 封装了变量和function 和全局的namespace不接触 松耦合. C) 只暴露可用public的方法 其他私有方法全部隐藏 ...
- mybatis之mapper.xml分析
select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...
- SQL Profiler工具简介
一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...
- C库专题(Day1)
<assert.h> C库宏-assert() 定义:#define assert(ignore) ((void)0) void assert(int experession); ex ...
- Swift - 43 - 继承, 多态, 析构函数
import Foundation /* 什么叫继承: 可以简单理解为一个类可以从它的父类或者基类中直接拿属性或者方法去使用 冒号":"表示两者之间的继承关系 */ class P ...
- brew 更换国内源(镜像)
cd /usr/local git remote set-url origin git://mirrors.tuna.tsinghua.edu.cn/homebrew.git brew update ...
- HTML&CSS基础学习笔记1.4-定义文档类型
Web 世界中存在许多不同的文档.只有了解文档的类型,浏览器才能正确地显示文档. HTML 也有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 HTML 页面 ...
- FastMM内存泄露
转自:http://www.2ccc.com/article.asp?articleid=4879FastMM是非常优秀的内存管理器,但是从FastMM4Options.inc中找到合适自已程序的选项 ...