2011-12-01 47 views
2

我必须完成一个练习,但有一点问题。我们必须实施一个recussve“ls”程序,它打印出“n”个最大的文件。但是有一些问题。使用gcc和自排序列表编译.h文件

1)

我有三个文件,main.c,list.c和list.h.在list.h中,我包含了string.h,stdio.h,stdlib.h,声明了一个struct(char * filename,long long filesize和struct element * next)和两个方法(append,printlist)。在list.c中我包含了list.h并实现了append和printlist两个方法。在main.c中我包含了unistd.h,dirent.h,sys/stat.h和list.h.

当我尝试使用“gcc main.c”进行编译时,出现“使用未声明的方法append和printlist”的错误,但是如果使用Eclipse,它的构建会很好。我该如何解决这个问题?

确切的错误,

/tmp/ccLbHnqR.o: In function `main': 
main.c:(.text+0x189): undefined reference to `printliste' 
/tmp/ccLbHnqR.o: In function `ftop': 
main.c:(.text+0x1f6): undefined reference to `append' 

2)

为了实现我试图用一个自排序列表的功能,即通过列表去,直到比新的值越大最后一个值,然后将新值的指针设置为最后一个值的指针,最后一个值的指针就是新值。

理论上它应该工作,但在实践中它不是。

追加方法看起来像这样

void append(struct element **lst, char* filename, long long filesize){ 
    struct element *newElement; 
    struct element *lst_iter = *lst; 

    newElement = malloc(sizeof(*newElement)); // create new element 
    newElement->filename = filename; 
    newElement->filesize = filesize; 
    newElement->next = NULL; // important to find the end of the list 

    if (lst_iter != NULL) { // if elements are existing 
     //if our element is bigger than the first element 
     if(lst_iter->filesize < newElement->filesize){ 
      newElement->next = lst_iter; 
      *lst = newElement; 
     } else { 
      while(lst_iter->next != NULL){ 
       if(lst_iter->filesize > newElement->filesize) lst_iter = lst_iter->next; 
       else break; 
      } 
      newElement->next = lst_iter->next; 
      lst_iter->next = newElement; 
     }   
    } 
    else // if the list is empty our value is the new value 
    *lst=newElement; 
} 

我使用的是从我的“最高频率”的方法,它得到一个目录这种方法,增加了每个文件这个目录添加到列表中,并为每个目录调用“ftop”。

void ftop(char* path){ 
    DIR *dir; 
    struct dirent *ent; 

    //open the directory 
    dir = opendir(path); 
    if (dir != NULL) { 
     //for each file/directory in it 
     while ((ent = readdir(dir)) != NULL) { 
      struct stat st; 
      //if it is a file, append it to the list 
      if(S_ISREG(st.st_mode)){ 
       append(&list, ent->d_name, st.st_size); 
      } else { 
       //if it is a directory, use recursion 
       ftop(ent->d_name); 
      } 
     } 
    } 
} 

但为什么它不工作,我不明白这一点。我知道你不想做别人的作业,但我会感谢你能给我的每一个提示。

P.S:如果你想的完整代码

main.c list.c

+0

我敢打赌,我知道这个作业来自哪里;) – Beginner

+0

请单独发布问题作为单独的问题。 – ninjalj

回答

1

你想

gcc -o main main.c list.c 

那就是 - 同时指定文件。

+0

谢谢,这解决了第一个问题(以及我有几个错误包括也必须包括在list.h文件STDLIB和标准输入输出) – Sturmi12

+0

哦,我只注意到你有两个问题:)我还以为你会成不必要的解释:)你应该发布两个问题。我会尝试阅读第二本,尽管我觉得很安心:) –

+0

“理论上它应该起作用,但实际上它不是。” - 你能详细说明一下吗,所以我不必阅读整个代码? :) –