2014-10-31 3 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' 

이 함수의 이름을 디자인하는 데 사용 된 유일한 파일은 헤더 파일과 후속 구현 파일입니다. 구현이있다 동안

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을 추가했는데 문제가 발생 했습니까? 나는 또한 모든 헤더 파일에 대해 경비원을 확보하고 있으므로 여전히 혼란 스럽다. 선언과 정의에 문제가있는 것이 아닌가? zip 파일을보고

+1

세이프 가드를 사용 했습니까? – gsamaras

+0

둘 다 함수를 정의하는 두 개의 오브젝트 파일 ('ImageList.o'와'img.o')이있는 것처럼 보입니까? –

+0

헤더 파일 정의를 변경하여 정의에 "extern"을 지정하십시오. – DrC

답변

0

나는 일

1)의 몇 이미

2 ImageList.h

에 포함되어 있기 때문에) ImageList.c에서 quadtree.h를 제거 함수를 인라인을 확인 말할 수있다. 함수 정의의 quadtree.c에 있습니다.

나는 이것이 문제를 해결할 것이라고 믿습니다.

+0

응답 해 주셔서 감사합니다. 그러나 압축 해제 된 파일의 내용을 변경하지 않으면 여전히 makefile이 실행됩니까? 어느 imagelist.c 및 imagelist.h에 quadtree.h 있습니다. 나는 여전히 모든 파일을 조사하고 다중 정의를 찾지 못합니다. – sebajun

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 ...입니다. .. #endif.

+0

답변 해 주셔서 감사합니다. Zip 파일의 헤더 파일을 살펴보면 경비원이 있습니다. 어떤 헤더 파일에서 – sebajun

+0

이 함수 printList insert_at_tail 및 make_empty_list에 대해 선언되어 있습니까? –

+0

현재 작업에 대한 전체 집음을주십시오. –