使用带有gcc的线程的POSIX程序的问题 [英] Troubles with POSIX program using threads with gcc

查看:224
本文介绍了使用带有gcc的线程的POSIX程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用带有gcc的POSIX线程来做一个程序,当我尝试对其进行编译时,终端显示如下消息:

I must do a program with POSIX threads with gcc and when i try to compile it, the terminal show a message like:

/tmp/ccw594wa.o:在函数main': POSIX.c:(.text+0xda): undefined reference to pthread_create'中 POSIX.c :(.text + 0x102):对'pthread_create'的未定义引用 collect2:萨尔瓦多州人民发展银行1

/tmp/ccw594wa.o: In function main': POSIX.c:(.text+0xda): undefined reference topthread_create' POSIX.c:(.text+0x102): undefined reference to `pthread_create' collect2: ld devolvió el estado de salida 1

有人可以帮助我确定错误原因吗??,该程序必须实现生产者-消费者算法来完成某些任务,我的代码(未完成)是下一个代码

can someone help me to identify the error cause??, the program has to implemente the producer-consumer algorithm to do some task, my code (unfinished) is the next one

  #include "stdio.h"
  #include "pthread.h"
  #define p printf
  #define s scanf

  pthread_mutex_t exmut;
  float suma=0;
  float x;
  int iteraciones;

  void *h_productor (void *arg)  /*Procedimiento del productor*/
  {
  }

  void *h_consumidor (void *arg) /*Procedimiento del consumidor*/
  {
  }

  main()  /*Hilo principal*/
  {
     int error;
     int *out, idp=1, idc=2;
     pthread_t productor;      /*Hilo que realiza el proc. del productor*/
     pthread_t consumidor;     /*Hilo que realiza el proc. del consumidor*/ 
     p ("\n\n    Cálculo de e^x, por medio de una serie infinita... =)!");
     p ("\n\n  x= ");
     s ("%f",&x);
     do
     {
        p ("  No. de iteraciones: ");
        s ("%d",&iteraciones);
        if (iteraciones<1 || iteraciones>10)
           p("\n Número no válido, debes introducir un valor de entre 1 y 10\n");
     }
     while (iteraciones<1 || iteraciones>10);

     pthread_mutex_init (&exmut, NULL);
     /*Creamos ambos hilos, tanto el productor como el consumidor*/
     error = pthread_create (&productor,NULL,h_productor,&idp);
     error = pthread_create (&consumidor,NULL,h_consumidor,&idc);
     /*
     /*Esperamos a que ambos hilos terminen/
     error = pthread_join (h_productor,(void **)&out);
     error = pthread_join (h_consumidor,(void **)&out);
   */
     p ("\n  La aproximación para %d iteraciones del valor de e^%f= %f",iteraciones,x,suma);
  }  

推荐答案

您可以尝试使用-pthread选项进行编译,该选项告诉gcc链接到pthread库.

You could try compiling with the -pthread option, which tells gcc to link against the pthread library.

这篇关于使用带有gcc的线程的POSIX程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆