源代码:

#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. C编程风格.

    C语言编程风格. 关于编程风格,不同书上有不同规范,不同公司都有自己的一套定义.根据自己的编程习惯做个简要说明. 1.变量定义 在定义变量时,前缀使用变量的类型,之后使用表现变量用途的英文单词或单词缩 ...

  2. Linux ssh安全设置

    本文摘要 SSH服务器配置文件是/etc/ssh/sshd_conf.在你对它进行每一次改动后都需要重新启动SSH服务,以便让改动生效.       SSH服务器配置文件是/etc/ssh/sshd_ ...

  3. Linux命令之用户与组管理

    介绍 Linux操作系统中,任何文件都归属某一特定的用户,而任何用户都隶属至少一个用户组.用户是否有权限对某文件进行访问.读写以及执行,受到系统严格约束的正式这种清晰.严谨的用户与用户组管理系统.在很 ...

  4. jQuery中DOM操作

    1 定义:jquery中对DOM的操作就是对DOM元素进行增删查改操作 2 分类:      1)DOM Core(核心):用途广泛 支持多种编程语言 2)HTML DOM:代码简短 只用于处理web ...

  5. 编写自己的javascript功能库之Ajax(仿jquery方式)

    本人学习的是php,所以就用php跟js来演示代码了,主要是锻炼自己写js的能力,练练手而已. 下面这是我编写的操作ajax的代码功能,勉强让我称之为库吧.. js代码实例(tool.ajax.js) ...

  6. jQuery读取JSON总结

    1.jQuery 部分 <script src="js/jquery.js" type="text/javascript"></script& ...

  7. Hdu1097(计算a的b次幂最后一位数值)

    #include <stdio.h> #include <math.h> int main() { int Num1,Num2; while(scanf("%d %d ...

  8. Spring学习笔记--依赖注入

    依赖注入和控制反转:http://baitai.iteye.com/blog/792980出自李刚<轻量级 Java EE 企业应用实战> Java应用是一种典型的依赖型应用,它就是由一些 ...

  9. awk的日志模块追加日期时间字段的方案

    function test() { i = ) { system("sleep 1") "date +'%Y-%m-%d %H:%M:%S'" | getlin ...

  10. pcduino连接OTG登录远程桌面

    由于没有HDMI的显示屏,为了方便起见,使用了pcduino的OTG来连接到虚拟桌面,可是发现连接上虚拟桌面后,电脑的外网就断了.下面这个方法让你既可以连接到pcduino,又可以让电脑能上外网. 打 ...