undefined reference to ...



  • Hallo an alle. Kann mir vielleicht jemand beim Lösen des folgenden Problems helfen?

    Hier der Quellcode:

    queue.h

    #ifndef __INC_QUEUE_H
    #define __INC_QUEUE_H
    
    #include <stdlib.h>
    
    /*
     * Definition des Datentyps bool
     */
    typedef enum {
      true = 1,
      false = 0
    } bool;
    
    /*
     * Definition des internen Queue-Elements
     */
    typedef struct _queue_element {
      void* element;
      struct _queue_element* next;
      struct _queue_element* prev;
    } queue_element;
    
    /*
     * Definition des Datentyps Queue
     */
    typedef struct _queue {
      queue_element* head;
      queue_element* tail;
      size_t size;
    } queue;
    
    /*
     * Methoden des Datentyps Queue
     */
    
    /*
     * Initioalisierungsmethode
     * @param queue* q - Pointer auf den zu initialisierenden
     *                   queue Objekt
     * Sollte bei jedem neu angelegtem queue Objekt als erste
     * aufgerufen werden
     */
    void init_queue(queue* q);
    
    /*
     * Methode zum Hinzufgen von Elementen in die queue
     * @param queue* q - Pointer auf den queue Objekt in das
     *                   das Element hinzugefgt werden soll
     * @param void* e - Pointer auf das Element, das in die
     *                  queue hinzugefgt werden soll
     */
    void push(queue* q, void* e);
    
    /*
     * Methode zum lesen und entfernen des obersen Elementes
     * der Queue
     * @param queue* q - Pointer auf den queue Objekt aus dem
     *                   das Element ausgelesen werden soll
     */
    void* pop(queue* q);
    
    /*
     * Methode zum berprfen ob das Queue Objekt elemente
     * enthlt
     * @param queue* q - Pointer auf den queue Objekt das auf
     *                   diese Eigenschaft berprft werden soll
     */
    bool empty(queue* q);
    
    #endif
    

    task_pool.h

    #ifndef __INC_TASK_POOL_H
    #define __INC_TASK_POOL_H
    
    #include <pthread.h>
    #include "queue.h"
    
    #define MAX_THREADS 100
    
    typedef struct _pool {
      pthread_t* threads;
      queue* mp_data_queue;
      int mi_max_threads;
      int mi_active_threads;
      pthread_t m_manager_thread;
      pthread_mutex_t m_lock;
    } pool;
    
    typedef struct _task {
      void* mp_task;
      void* mp_params;
    } task;
    
    void init_pool(pool*, int);
    
    void insert_task(pool*, void*, void*);
    
    void close_pool(pool*);
    
    void* start_manager_thread(void*);
    
    #endif
    

    und die task_pool.c

    #include "task_pool.h"
    
    /*
     *
     */
    void init_pool(pool* p, int i_num_threads) {
      p->threads = (pthread_t*)malloc(sizeof(pthread_t) * i_num_threads);
      p->mi_max_threads = i_num_threads;
      p->mi_active_threads = 0;
      p->mp_data_queue = (queue*)malloc(sizeof(queue));
      init_queue(p->mp_data_queue);
      pthread_mutex_init(&(p->m_lock), NULL);
      pthread_create(&(p->m_manager_thread), NULL, start_manager_thread, p);
    }
    
    void insert_task(pool* p, void* task_to_run, void* params) {
      task t;
      t.mp_task = task_to_run;
      t.mp_params = params;
      pthread_mutex_lock(&(p->m_lock));
      push(p->mp_data_queue, &t);
      pthread_mutex_unlock(&(p->m_lock));
    }
    
    void close_pool(pool* p) {
      pthread_mutex_destroy(&(p->m_lock));
      int i;
      /* Wait for all threads to complete */
      for (i = 0; i <= p->mi_active_threads; i++) {
        pthread_join(p->threads[i], NULL);
      }
      pthread_exit(NULL);
      free(p->mp_data_queue);
      free(p->threads);
    }
    
    void* start_manager_thread(void* params) {
      pool* p = (pool*)params;
      while(true) {
        if(p->mi_active_threads <= p->mi_max_threads && p->mp_data_queue->size != 0) {
          task* p_t = (task*)pop(p->mp_data_queue);
          pthread_create(p->threads[p->mi_active_threads], NULL, p_t->mp_task, p_t->mp_params);
          p->mi_active_threads++;
        }
      }
    }
    

    in der task_pool.c befindet sich zu testzweicken einen main (hier nicht aufgelistet)

    wenn ich jetzt task_pool.c mit gcc -Wall -o test task_pool.c -lpthread kompilliere bekomme ich folgende Fehlermeldung:

    dimon@duke-m:~/work/PV/uebung3$ gcc -Wall -o test task_pool.c -lpthread
    task_pool.c: In Funktion »start_manager_thread«:
    task_pool.c:42: Warnung: Verarbeiten des Argumentes 1 von »pthread_create« erzeugt Zeiger von Ganzzahl ohne Typkonvertierung
    /tmp/cccse6Oy.o(.text+0x49): In function `init_pool':
    : undefined reference to `init_queue'
    /tmp/cccse6Oy.o(.text+0xbf): In function `insert_task':
    : undefined reference to `push'
    /tmp/cccse6Oy.o(.text+0x160): In function `start_manager_thread':
    : undefined reference to `pop'
    collect2: ld returned 1 exit status
    

    queue.h ist in queue.c implementiert und liegt in demselben verzeichnis, wo alle andere files liegen. Wie man siehet meckert der Compiler nur bei den fuktionen der queue. Woran kann es liegen?



  • Du hast entweder vergessen queue.c zu implementieren oder auch nur queue.c zu compilieren
    der korrekte compiler-aufruf wäre dann
    gcc main.c task_pool.c queue.c -o test -lpthread
    Kurt

    edit: habe das erst jetzt gesehen

    queue.h ist in queue.c implementiert und liegt in demselben verzeichnis, wo alle andere files liegen. Wie man siehet meckert der Compiler nur bei den fuktionen der queue. Woran kann es liegen?

    also ist es der compiler-aufruf.



  • :))
    So einfach kanns gehen:)

    Danke!!!


Anmelden zum Antworten