源代码:

#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 &#39;pthread_create&#39; 处理的更多相关文章

  1. (笔记)Linux线程编译undefined reference to 'pthread_create'

    在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include &l ...

  2. 在linux下编译线程程序undefined reference to `pthread_create'

    由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...

  3. openCV中 libopencv-nonfree-dev的安装: undefined reference to `cv::initModule_nonfree()&#39;

    今天照着一起做RGB-D SLAM (3)    , 程序会出现以下的错误: cv::initModule_nonfree(); /home/yhzhao/slam/src/detectFeature ...

  4. undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' 的修改方法

    在编译DSO代码的时候会如下这样的问题: 检查DSO,在程序中没有用到pthread,但是在编译的时候却出现此类问题.仔细想了想了一下,在程序中用到了C++11中的线程std::thread,个人猜测 ...

  5. undefined reference to symbol' pthread_create@@GLIBC_2.2.5'

    我在ubuntu16.04上迁移工程,遇到了这个错误. pthread库不是Linux系统默认的库,链接时需要添加-pthread参数. 这里注意是链接那一步添加-pthread,而不是编译选项.

  6. linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol &#39;pthread_create@@GLIBC_2.2.5&#39;

    解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...

  7. Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误。

    Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误. ...

  8. 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)

    问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...

  9. undefined reference to `sin&#39;问题解决

    作者:zhanhailiang 日期:2014-10-25 使用gcc编译例如以下代码时报"undefined reference to `sin'": #include < ...

随机推荐

  1. error C2143: 语法错误 : 缺少“;”(在“using”的前面)

    class JJMenuScene : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos ...

  2. gcc总结【基本用法】【选项】【动静态库】(转)

    1.////////////////////////////////////////////////////////////////////////////////////////////////// ...

  3. view动画库

    Android View Animations这个项目收集了各种有趣的动画效果. 所有效果: Attension Flash, Pulse, RubberBand, Shake, Swing, Wob ...

  4. Java 动态代理机制详解(JDK 和CGLIB,Javassist,ASM)

    class文件简介及加载 Java编译器编译好Java文件之后,产生.class 文件在磁盘中.这种class文件是二进制文件,内容是只有JVM虚拟机能够识别的机器码.JVM虚拟机读取字节码文件,取出 ...

  5. CentOS7--64安装python的psutil模块

    1.以root身份登陆CentOS依次 执行以下命令: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.g ...

  6. box-shadow全面解析

    一.box-shadow语法: box-shadow: none | inset(可选值,不设置,为外投影,设置,为内投影) x-offset(阴影水平偏移量,正方向为right) y-offset( ...

  7. 2:url有规律的多页面爬取

    举例网站:http://www.luoo.net/music/期刊号 e.g:http://www.luoo.net/music/760 打算爬取其title:Hello World:pic:desc ...

  8. Intent 能传递的数据类型

    1. Serializable,将对象序列化成二进制数据传递 2. charsequence: 主要用来传递String,char等 3. parcelable: 这个android提供的一种新的类型 ...

  9. oracle开机自启动-超简单

    1. 在/etc/oratab中作如下修改$ORACLE_SID:$ORACLE_HOME:Y例如vi /etc/orataborcl:/u01/app/oracle/product/10.2.0/d ...

  10. js 获取mac地址

    js 获取mac地址 function MacInfo(){ var locator =new ActiveXObject ("WbemScripting.SWbemLocator" ...