Reputation: 79
I've got a little problem with an exercise I have to finish. We have to implement a recurisve "ls" program which prints out the "n" biggest files. But there are some problems.
1)
I have three files, main.c, list.c and list.h. In list.h I have included string.h ,stdio.h,stdlib.h,declared a struct (char* filename,long long filesize and struct element* next) and two methods (append,printlist). In list.c I have included list.h and implemented the two methods append and printlist. In main.c I have included unistd.h,dirent.h, sys/stat.h and list.h.
When I try to compile it with "gcc main.c" I get the error "used of undeclared methods append and printlist" but if I'm using Eclipse it builds just fine. How can I solve this?
Exact errors
/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)
To implement the functionality I tried to use a self-sorting list, i.e. go through the list until the last value that is bigger than the new value, then set the pointer of the new value to the pointer of the last value and the pointer of the last value is the new value.
In theory it should work, but in practice it isn't.
The append methods looks like this
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;
}
I'm using this method from my "ftop" method, which gets a directory, adds every file in this directory to the list and for each directory it calls "ftop" again.
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);
}
}
}
}
But I don't get it why it isn't working. I know you don't want to do the homework of others, but I would be thankful for every hint you could give me.
P.s.: If you want the full code
Upvotes: 2
Views: 349
Reputation: 143219
You want
gcc -o main main.c list.c
that is — specify both files.
Upvotes: 1