2014-10-31 36 views
0

只是想弄清楚爲什麼我得到這個錯誤?C中的多個定義

/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:21: multiple definition of `insert_at_tail' 
img.o:/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:21: first defined here 
ImageList.o: In function `printList': 
/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:43: multiple definition of `printList' 
img.o:/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:43: first defined here 
ImageList.o: In function `make_empty_list': 
/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:57: multiple definition of `make_empty_list' 

我用來設計這些函數名稱的頭文件和後續實現c文件的唯一文件。

頭文件包括以下聲明:

void printList(ImageList *list); 
ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element); 
ImageList *make_empty_list(void); 

雖然實現了這一點:

ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element){ 
    node_t *new; 
    new = malloc(sizeof(*new)); 
    assert(list!=NULL && new!=NULL); 
    new->data.dim = dimen; 
    new->data.num = element; 
    new->data.filename = malloc(strlen(name)*sizeof(char)); 
    strcpy(new->data.filename, name); 
    new->data.QuadTree = qtree; 
    new->next = NULL; 
    if(list->tail==NULL){ 
     list->head = list->tail = new; 
    } else { 
     list->tail->next = new; 
     list->tail = new; 
    } 
    return list; 
} 

// print a list (space-separated, on one line) 

void printList(ImageList *list) 
{ 
     node_t *cur; 

     for (cur = list->head; cur != NULL; cur = cur->next) { 
       printf("%d",cur->data.num); 
       printf(" [%2d]",cur->data.dim); 
       printf(" %s",cur->data.filename); 
     } 
     putchar('\n'); 
} 

// Make an empty list of images 

ImageList *make_empty_list(void) 
{ 
    ImageList *list; 
    list = malloc(sizeof(*list)); 
    assert(list!=NULL); 
    list->head = list->tail = NULL; 
    return list; 
} 

我知道,這樣做的原因通常是在頭文件中定義函數以及但似乎我沒有。我已經瀏覽了實際使用這些函數的文件,但沒有額外的函數定義。兩個文件的參數和返回值也是相同的,所以我有點失落。任何幫助表示讚賞。

CFLAGS=-Wall -g 

img : img.o QuadTree.o ImageList.o 
     gcc -o img img.o QuadTree.o ImageList.o 

img.o : img.c QuadTree.h ImageList.h 
     gcc $(CFLAGS) -c img.c 

QuadTree.o : QuadTree.c QuadTree.h 
     gcc $(CFLAGS) -c QuadTree.c 

ImageList.o : ImageList.c ImageList.h 
     gcc $(CFLAGS) -c ImageList.c 

clean : 
     rm -f img img.o QuadTree.o ImageList.o core 

我加了我的Makefile,是這個問題出現嗎?我也對所有頭文件都有保護,所以我仍然非常困惑,聲明和定義有什麼問題嗎?

+1

你使用了安全衛士嗎? – gsamaras 2014-10-31 04:21:00

+0

看起來你有兩個目標文件('ImageList.o'和'img.o'),它們都定義了這些函數? – 2014-10-31 04:21:56

+0

嘗試更改頭文件定義以指定「extern」的定義 – DrC 2014-10-31 04:23:03

回答

0

通過查看zip文件,我可以說幾件事情

1)ImageList.c刪除quadtree.h既然你已經列入ImageList.h

2)使函數內聯。在函數定義中的quadtree.c中。

我相信這會解決問題。

+0

感謝您的回覆。但是,當你不更改解壓縮文件的內容時,makefile仍然運行嗎?在imagelist.c和imagelist.h中都有quadtree.h。我仍然瀏覽我的所有文件,沒有找到多個定義。 – sebajun 2014-10-31 06:28:48

0

擴展註釋:使用保障在包括不定義兩次同樣的事情,如果文件被多次包括:對於

#ifndef HEADER_IMG_H 
#define HEADER_IMG_H 

void printList(ImageList *list); 
ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element); 
ImageList *make_empty_list(void); 

#endif 

模式的#ifndef HEADER_FILE_H的#define HEADER_FILE_H ...這裏聲明。 .. #萬一。

+0

謝謝你的迴應!如果你看看Zip文件中的頭文件,那麼有警衛。 – sebajun 2014-10-31 07:41:37

+0

在哪些頭文件中聲明瞭那些函數printList insert_at_tail和make_empty_list? – 2014-10-31 09:07:15

+0

請給出您當前工作的完整zip文件,這個文件比您的更改版本更老。 – 2014-10-31 09:13:15