Linked Questions

Popular Questions

compile multiple source files error when compiling

Asked by At

Hello im trying to bugtest a C binary-tree program and i get an error when i try to compile my program. My pqueue.c does not see its list functions even though i have added the list.h files and compiled with list.c and pqueue.c and binary-tree.c (which includes main)i know the pqueue and list code is sound in its logic. Im trying to compile with gcc -std=c99 -Wall -o test list.c pqueue.c binary-tree.c

and i get error messages

C:\Users\sugap\AppData\Local\Temp\ccyT174V.o:pqueue.c:(.text+0xe8): undefined reference to list_set_memhandler' undefined reference to list_empty etc etc am i compiling it wrong?. Thanks so much in advance.

this is part of my binary-tree.c(That includes main)

#include<stdio.h>
#include<stdlib.h>
#include"pqueue.h"
#include "list.h"

typedef struct
{
    int frequency;
    char letter;
} queue_item;

int lessthan(void *elem1, void * elem2)
{
    return ((queue_item*)elem1)->frequency 
- ((queue_item *)elem2)->frequency;
}

void create_frequency(FILE * fp, int * 
freqtable)
{

This is my pqueue.c

#include <stdlib.h>
#include "list.h"
#include "pqueue.h"
#include <assert.h>

/* A structure used to represent an 
element in a priority queue.
 *
 * @elem the_list  A pointer to the 
internally used list.
 * @elem cmp_func  The function used to 
decide priority between
 *                 elements in the 
priority queue.
 */
struct pqueue {
    list *the_list;
    pqueue_cmp_func cmp_func;
};   


pqueue* pqueue_empty(pqueue_cmp_func 
cmp_func)
    {
        pqueue* pq = malloc(sizeof *pq);
        assert(pq);

        pq->the_list = list_empty();
        assert(pq->the_list);
        pq->cmp_func = cmp_func;

        return pq;
    }


void pqueue_set_memhandler(const pqueue 
*const pq,
                           pqueue_mem_func 
mfunc)
{
    assert(pq);
    assert(pq->the_list);

    list_set_memhandler(pq->the_list, 
mfunc);
}


void pqueue_delete_first(pqueue *const pq)
{
    assert(pq);
    assert(pq->the_list);

    if(!list_is_empty(pq->the_list)) {
        list_remove(pq->the_list, 
list_first(pq->the_list));
    }
}


void pqueue_insert(pqueue *const pq, void 
*value)
{
    assert(pq);
    assert(pq->the_list);

    if(list_is_empty(pq->the_list)) {
        list_insert(pq->the_list, 
list_first(pq->the_list), value);
    } else {
        list_position pos = list_first(pq- 
   >the_list);
                while(!list_is_end(pq- 
   >the_list, 
        pos)) {
                 if(pq->cmp_func != NULL) 
 {
                if(pq->cmp_func(value, 
list_inspect(pq->the_list,

    pos)) < 0) {
                    list_insert(pq- 
   >the_list, pos, value);
                        return;
                    }
                }
            pos = list_next(pq->the_list, 
pos);
        }
        list_insert(pq->the_list, pos, 
value);
    }
}


void* pqueue_inspect_first(const pqueue 
*const pq)
{
    assert(pq);
    assert(pq->the_list);


return list_inspect(pq->the_list, 
list_first(pq->the_list));
}


bool pqueue_is_empty(const pqueue *const 
pq)
{
    assert(pq);
    assert(pq->the_list);

     return list_is_empty(pq->the_list);
}  


void pqueue_kill(pqueue *pq)
{
    assert(pq);
    assert(pq->the_list);

    list_kill(pq->the_list);
    free(pq);
}

this is my list module

#include     <stdlib.h>
#include     <assert.h>
#include "list.h"


/* A structure used to represent a node in 
a list.
 *
 * @elem next       A pointer to the next 
node or NULL.
 * @elem prev       A pointer to the 
previous node or NULL.
 * @elem value      A pointer to the value 
 of the node or NULL.
 */
struct node
{
    struct node *next;
    struct node *prev;
    void *value;
};


    /* A structure used to represent a 
    list.
         *
     * @elem first      A pointer to the 
first 
     node in the list or NULL.
     * @elem end        A pointer to the 
 last 
    node in the list or NULL.
      * @elem mfunc      The function for 
    handling dynamically allocated
     *                  memory.
     */
struct list
{
     struct node *first;
    struct node *end;
    list_mem_func mfunc;
};


/* Declaration of internal functions */
static struct node *make_node(void 
*value);


/* ---------------------- External 
functions ---------------------- */

void list_set_memhandler(list *const l, 
const list_mem_func mfunc)
{
    l->mfunc = mfunc;
}


list *list_empty(void)
{
    list *l = malloc(sizeof *l);
    assert(l != NULL);

    l->first = NULL;
    l->end = NULL;
    l->mfunc = NULL;

    return l;
}


bool list_is_empty(const list *const l)
{
    return l->first == NULL;
}


list_position list_first(list *const l)
{
    if (l->first == NULL) 
  {
        return (list_position)
    { 
      &l->first, &l->end 
    };
    } else 
  {   
        return (list_position) 
    { 
      &l->first, &l->first->prev 
    };
    }
 }


list_position list_end(list *const l)
{
    if (l->first == NULL) {
        return (list_position) { &l- 
>first, &l->end };
     } else {
        return (list_position) { &l->end- 
   >next, &l->end };
    }
}


list_position list_next(list *const l, 
const 
list_position pos)
{
    if ((*pos.forward)->next == NULL) {
        return (list_position) { & 
(*pos.forward)->next, &l->end };
    } else {
        return (list_position) { & 
(*pos.forward)->next,
                               & 
   (*pos.forward)->next->prev };
        }
     }


list_position list_previous(list *const l, 
const list_position pos)
{
     if ((*pos.backward)->prev == NULL) {
        return (list_position) { &l- 
>first, &(*pos.backward)->prev };
    } else {
        return (list_position) { & 
(*pos.backward)->prev->next,
                               & 
(*pos.backward)->prev };
    }
}


bool list_is_first(const list *const l, 
 const list_position pos)
{
    assert(l != NULL);

     return *pos.backward == NULL;
}


bool list_is_end(const list *const l, 
const list_position pos)
{
     assert(l != NULL);

    return *pos.forward == NULL;
}


void* list_inspect(const list *const l, 
const list_position pos)
{
    assert(l != NULL);

    return (*pos.forward)->value;
}


 list_position list_insert(const list 
 *const l,
                          const 
list_position pos,
                           void *value)
{
    assert(l != NULL);

    struct node *n = make_node(value);
     n->next = *pos.forward;
    n->prev = *pos.backward;
    *pos.forward = n;
    *pos.backward = n;

     return (list_position){pos.forward, 
&n->prev};

Related Questions