线程异常: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 < ...
随机推荐
- springmvc 基础
在最简单的springmvc应用程序中,控制器是唯一需要在java web部署描述文件(web.xml)中配置的servlete(springmvc的控制器是Dispatcher Servlet).每 ...
- 内容提供者 DocumentProvider Uri工具类
Activity /**详见http://blog.csdn.net/coder_pig/article/details/47905881 Calendar Provider:日历提供者,就是针对针对 ...
- CSS投影实现方式
备用素材: 1.png shadow.png 第一种方式: 利用负边距实现 最终效果图: <!DOCTYPE html> <html lang="en"&g ...
- Android Log日志文件的分析、查看
Log 在android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 什么时候会产生log文件呢 ?一般 ...
- zookeeper端口号冲突:8080冲突
端口号冲突,在conf/zoo2181.cfg文件里面添加一个端口号的指定(具体的端口号可以自己定义一个): admin.serverPort=8081
- Swift 流程控制
import Foundation ...{ == { print(index) } } // 可选变量 类型后面加? var myName:String?="jikexueyuan&quo ...
- 循序渐进Socket网络编程(多客户端、信息共享、文件传输)
循序渐进Socket网络编程(多客户端.信息共享.文件传输) 前言:在最近一个即将结束的项目中使用到了Socket编程,用于调用另一系统进行处理并返回数据.故把Socket的基础知识总结梳理一遍. 1 ...
- n行m列的网格中含有的矩形数
给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形 公式:[ n(n+1)*m(m+1)]/4 直接想问题比较复杂,可以先考虑矩形的长,再考虑矩形的高,由对称性可知最后的结果中m和n对称 ...
- 深入了解session
session在web开发中是一个非常重要的概念,这个概念很抽象,很难定义,也是最让人迷惑的一个名词,也是最多被滥用的名字之一,在不同的场合,session一次的含义也很不相同.这里只探讨HTTP S ...
- RF学习过程中遇到的问题
1.写了一个打开浏览器的用例,执行用例时报需要对应的浏览器驱动,此时应下载对应的浏览器驱动,把该执行文件放到python的安装根目录下,再执行用例就成功了 2.日志不见的问题:需要杀进程