编译时编译多个源文件错误

时间:2019-02-11 20:04:40

标签: c

您好,我正在尝试对C二叉树程序进行测试,尝试编译程序时出现错误。即使添加了list.h文件并使用list.c和pqueue.c以及binary-tree.c(包括main)进行编译,我的pqueue.c也看不到其列表功能,我知道pqueue和列表代码是正确的按照其逻辑。 我正在尝试使用gcc -std = c99 -Wall -o test list.c pqueue.c binary-tree.c

进行编译

我收到错误消息

C:\ Users \ sugap \ AppData \ Local \ Temp \ ccyT174V.o:pqueue.c :(。text + 0xe8):对list_set_memhandler的未定义引用 未定义引用list_empty等 我编译错了吗?提前非常感谢。

这是我的binary-tree.c(包括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)
{

这是我的排队。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);
}

这是我的列表模块

#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};

0 个答案:

没有答案