Troubles with POSIX program using threads with gcc

ⅰ亾dé卋堺 提交于 2019-12-13 07:07:35

问题


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: 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);
  }  

回答1:


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




回答2:


try:
gcc -pthread -o test test.c



来源:https://stackoverflow.com/questions/9919724/troubles-with-posix-program-using-threads-with-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!