源代码:

#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. jquery之多重判断

    var appPath = getAppPath(); $(function(){ $('#addTeskDlg').window('close'); teskGrid(); }); function ...

  2. “=”号和“:”的区别,Html.Raw()的使用

    “=”号,将原封不动输出字符串到页面 “:”号:将字符串进行编码后输出到页面 public ActionResult HtmlEncodeDemo() { ViewData["strScri ...

  3. c - 逆序/正序输出每位.

    #include <stdio.h> #include <math.h> /* 判断一个正整数的位数,并按正序,逆序输出他们的位. */ int invert(int); vo ...

  4. Altium Designer极坐标布局方法

    1.键盘快捷组合 O+G,打开栅格管理器,点击左下角的“菜单”,在对话框中的选择“添加极坐标栅格”. 2.双击新添加的优先等级为1的栅格,在弹出的polar  grid editor 对话框中,对里边 ...

  5. mysql快速入门

    一.下载并解压 $ wget http://cdn.mysql.com/Downloads/MySQL-5.5/MySQL-5.5.42-1.el6.x86_64.rpm-bundle.tar 解压后 ...

  6. PHP扩展开发(3)-config.m4

    1. 宏命令 1.1. dnl 注释      1.2. 扩展的工作方式           1.2.1) PHP_ARG_WITH不需要第三方库           1.2.2) PHP_ARG_E ...

  7. iOS 9的 Universal Links 通用链接使用

    前段时间和朋友(@品味生活)一起搞 iOS9的通用链接,我主要做了前面官方文档翻译工作,后面的一些东西都是他在搞,整理也是他整理的. 他的博客原文地址:http://pinwei.blog.51cto ...

  8. Effective Java实作Comparator - 就是爱Java

    如果集合或数组内的对象,有1个以上不同的排序逻辑时,那该如何处理呢?尤其是当已经实现了Comparable,又不能变动原本的逻辑时,Mix会采用Comparator来处理. 阅读全文>>

  9. 通过select选项动态异步加载内容

    通过监听select的change事件来异步加载数据. 1:效果图: 选择Good: 选择 Bad: 2:index.html <!DOCTYPE html> <html lang= ...

  10. cf B. Vasya and Public Transport

    http://codeforces.com/contest/355/problem/B #include <cstdio> #include <cstring> #includ ...