2015-04-04 121 views
0

我需要用C和CUnit編寫一個程序來測試一些簡單的堆棧函數,並使用「Makefile」,但是當我嘗試編譯它時,我總是會得到相同的錯誤。在Ubuntu終端顯示這個時候我寫了 「make」 命令:CUnit(undefined reference)

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

    } 

的基本功能,推,流行等寫入,但沒有與他們的問題。他們以前用於我的另一個程序。

回答

0

gcc -o編譯並鏈接您的源代碼,因此要麼添加實現函數的.c文件,要麼與gcc -c分開編譯,而不鏈接源文件。