0

我需要使用 C 和 CUnit 编写一个程序来测试一些简单的堆栈函数并使用“Makefile”但是当我尝试编译它时,我总是会遇到同样的错误。当我编写“make”命令时,ubuntu 上的终端会显示:

gcc -o Pilhaa_teste.o Pilhaa_teste.c -lcunit
/tmp/ccLqNqAx.o: In function `main':
Pilhaa_teste.c:(.text+0x21): undefined reference to `clean_suite1'
Pilhaa_teste.c:(.text+0x26): undefined reference to `init_suite1'
Pilhaa_teste.c:(.text+0x50): undefined reference to `testaTOP'

我写的 .h 是:

typedef struct No {
    struct No *prox;
    int info;
}no;

typedef struct pilha {
    no *topo;
}Pilha;

int init_suite1(void);

int clean_suite1(void);

void testaTOP(void);

/*create empty stack*/
Pilha *cria_pilha(void);

/*add one element to the stack*/
void push (Pilha *p, int valor);

/*free first element of stack*/
void pop (Pilha *p);

/*print and find first element*/
int top (Pilha *p);

/*free stack*/
void libera(Pilha *p);

/*print stack*/
void imprime(Pilha *p);

带有主要代码的.c:

    #include <stdlib.h>
    #include <stdio.h>
    #include "pilha.h"
    #include "CUnit/Basic.h"


int main(){
    CU_pSuite pSuite = NULL; 

    if (CUE_SUCCESS != CU_initialize_registry())
      return CU_get_error();

    pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
        if(NULL == pSuite){
        CU_cleanup_registry();
        return CU_get_error();
    }

    if(NULL == CU_add_test(pSuite, "test of top()", testaTOP)){
        CU_cleanup_registry();
        return CU_get_error();
    }


    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();
    return CU_get_error();
}

以及 clean_suite1、init_suite1 和 testaTOP 函数:

    static Pilha *p = NULL;

    int init_suite1(void){
        push(p, 6);
        if(p!=NULL)
            return 0;
        else
            return 1;
    }

    int clean_suite1(void){
        pop(p);
        if (p == NULL)
            return 0;
        else
            return 1;
    }

    void testaTOP(void){
        Pilha *p = NULL;
         push (p, 6);
        if (p != NULL){
            CU_ASSERT(top(p) == 6);
            push (p, 7);
            if (p != NULL)
                CU_ASSERT(top(p) ==7 );
        }

        no *aux = p->topo->prox;
        free(p);
        free(aux);

    }

基本功能,push,pop等都写好了,但是没有问题。它们以前在我的另一个程序中使用过。

4

1 回答 1

0

gcc -o编译和链接你的源代码,所以要么添加 .c 实现函数的地方,要么单独编译gcc -c,它不链接源文件。

于 2015-04-04T17:36:35.370 回答