2012-02-21 2 views
0

존재를 거부 :내 기능 나는이 오류가, 내 테스트 파일을 실행하려고 할 때마다

코드는 것으로 의미 icons_destroy 및 irest_destroy 같은 C. 기능의 목록 구조를 구현하기위한 것입니다
/tmp/ccCazDOe.o: In function `main': 
/home/cs136/cs136Assignments/a06/testing.c:8: undefined reference to `icopy' 
collect2: ld returned 1 exit status 

파괴적인 기능.

내 ilength 기능을 사용해도 똑같은 일이 발생합니다.

함수 이름을 바꾸고 헤더에 여러 번 정의하여 새 테스트 파일을 작성했습니다. 나는 무엇이 잘못되었는지를 알 수없는 것 같습니다. 그것은 단지 숫자를 반환하는 ilength라는 함수를 만들기로 결정했을 때 작동하는 것으로 보입니다. 그래서 함수가 작동하는 방식에 문제가있을 수 있습니다.

여기에 어떤 도움이 필요합니까?

내 테스트 파일의 코드 :

#include <stdio.h> 
#include "ilist_destructive.h" 

int main(void){ 
ilist x = iempty(); 
x = icons_destroy(1, x); 
x = icons_destroy(2, x); 
ilist y = icopy(x); 
idelete(y); 
idelete(x); 
} 

내 헤더 파일 :

// Destructive Abstract data type ilist 

struct ilist_ADT; 
typedef struct ilist_ADT *ilist; 
ilist iempty(); 
int iempty_huh(ilist il); 
int ifirst(ilist il); 
ilist icons_destroy(int in, ilist il); 
ilist irest_destroy(ilist il); 
ilist icopy(ilist il); 
int ilength(ilist il); 
void idelete(ilist il); 
int ilength(ilist il); 
ilist icopy(ilist il); 

내 .c 파일 : 당신은 아마 단지 컴파일하는 것처럼

#include "ilist_destructive.h" 
#include <stdlib.h> 

// The ilist ADT is a pointer to this secret struct 
struct ilist_ADT{ 
    struct ilist_ADT *rest; 
    int first;  
}; 

ilist icons_destroy(int in, ilist il){ 
    if (il == NULL) { 
     ilist anewlist = malloc(sizeof(struct ilist_ADT)); 
     anewlist->first = in; 
     anewlist->rest = NULL; 
     return (anewlist); 
    } else { 
     ilist previous = malloc(sizeof(struct ilist_ADT)); 
     previous->first = il->first; 
     previous->rest = il->rest; 
     il->first = in; 
     il->rest = previous; 
     return il; 
    } 
} 

// ifirst returns the first element of il 
int ifirst(ilist il){ 
    if(il == NULL){ 
     exit(1); 
    } 
    return il->first; 
} 

ilist irest(ilist il){ 
    if(il == NULL){ 
     exit(1); 
    } 
    return il->rest; 
} 

ilist irest_destroy(ilist il){ 
    if(il == NULL){ 
     exit(1); 
    }else if(il->rest == NULL){ 
     free(il); 
     return NULL; 
    }else{ 
     ilist original = il->rest; 
     il->first = original->first; 
     il->rest = original->rest; 
     free(original); 
     return il; 
    } 
} 

ilist iempty(){ 
    return NULL; 
} 

// test for empty ilist 
int iempty_huh(ilist il){ 
    return il == NULL; 
} 

// free memory for entire ilist 
void idelete(ilist il){ 
    while (il != NULL) { 
     ilist next = il->rest; 
     free(il); 
     il = next; 
    } 

int ilength(ilist il){ 
    int counter = 0; 
    while (iempty_huh(il) != 1){ 
     counter = counter + 1; 
     il = irest(il); 
    } 
    return counter; 
} 

ilist icopy(ilist il){ 
    ilist copy = malloc(sizeof(struct ilist_ADT)); 
    copy->first = il->first; 
    copy->rest = il->rest; 
    return copy; 
} 

} 
+0

테스트 파일과 ilist_destructive.c 파일을 모두 컴파일하고 있습니까? –

답변

2

같습니다 testing.c가 아니라 ilist_destructive.c입니다. 이 같은 명령으로, 둘 다 컴파일해야합니다

(컴파일하고 두 가지를 동시에 연결)이 같은 일련의 명령과 다른 사람 또는
gcc -Wall testing.c ilist_destructive.c -o testing 

:

gcc -Wall testing.c -c testing.o 
gcc -Wall ilist_destructive.c -c ilist_destructive.o 
gcc testing.o ilist_destructive.o -o testing 

(각각을 하나의 오브젝트 파일로 컴파일 한 후 나중에 링크 함), 관련 소스 파일이 변경되지 않으면 처음 두 단계 중 하나를 수행 할 수 없기 때문에 좀 더 유연합니다.

+0

그 기능이 무엇인지 모르겠습니다. 그들은 상당히 높은 수준으로 보입니다. 나는 그것을 어떻게 사용하는지 모르겠습니다. testing.c에서 main()의 맨 앞에 넣었습니까? – user1222282

+0

@ user1222282 : 어, 아니, 죄송합니다. 위의 내용은'gcc' (Gnu 컴파일러 클러스터)를 사용하여 프로그램을 컴파일하는 명령입니다. 그러나 나는 당신이 이미'gcc '를 사용하고 있지 않기 때문에 약간의 백업을 해봅시다. 프로그램을 컴파일하는 데 사용하는 명령 *은 무엇입니까? – ruakh

+0

C 프로그램을 실행하기 위해 VirtualBox의 Ubuntu에서 RunC를 사용하고 있습니다. – user1222282

관련 문제