tags:

views:

1136

answers:

6

Hi
I'm new to C and i can't compile a program i downloaded. The Errormessage looks like this :

    ********@*******:~/Desktop/GRAPPA20$ gcc all_sorting_reversals.c
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o: In function `_start':
/build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
/tmp/ccwl1p7v.o: In function `find_all_sorting_reversals':
all_sorting_reversals.c:(.text+0x536): undefined reference to `clear_list'
all_sorting_reversals.c:(.text+0x55c): undefined reference to `clear_list'
all_sorting_reversals.c:(.text+0x5c5): undefined reference to `push'
all_sorting_reversals.c:(.text+0x5fe): undefined reference to `clear_list'
all_sorting_reversals.c:(.text+0x61f): undefined reference to `clear_list'
all_sorting_reversals.c:(.text+0x71d): undefined reference to `push'
all_sorting_reversals.c:(.text+0x767): undefined reference to `list_size'
all_sorting_reversals.c:(.text+0x791): undefined reference to `list_size'
all_sorting_reversals.c:(.text+0x7fe): undefined reference to `list_size'
all_sorting_reversals.c:(.text+0x830): undefined reference to `list_get'

The Code can be seen in : http://pastebin.com/d749ec13a

A: 

Looks like you need to forward declare everything.

In C, the compiler reads everything from top-to-bottom, so if you call a method, and the method is defined further down in your code, you need to forward declare it.

For example, this wont work:

int main()
{
   doStuff();
   return 0;
}
void doStuff()
{
   int foo = 3;
}

.. but this will:

void doStuff()
{
   int foo = 3;
}
int main()
{
   doStuff();
   return 0;
}

Another possibility is that you are trying to compile C++ code with a C compiler. Lists are ussually created as classes, so if you got any class decleration in your code, it's C++ :)

Again, you need to post the code (or link to it), cause from those messages, we can't give you a definite answer.

[EDIT] Nvm, this clearly ain't your answer after seeing the source code :)

cwap
Your two code snippets are identical...
ChrisF
Fixed it for him. I assume he meant to switch the order of main and doStuff in his 2nd snippet
Visage
I wasn't sure which is why I left it.
ChrisF
Ye, damn that copy-pasting :) Sorry. Visage got it right
cwap
+5  A: 

This is a linker error. It is occurring because the linker can not find the implementation of certain functions. In this case, the functions don't look like they're from a library. So the most likely cause is you are not compiling in all the C source files required.

Did you check to see whether the program has a make file?

EDIT: It's easy to see this from your posted code. The missing functions (clear_list, push, etc.) simply aren't defined in that file.

Matthew Flaschen
+6  A: 

Seems like all_sorting_reversals.c does not contain main() method and it expects to be linked with other objects/libraries that provide the other missing methods (list_get, list_size and so on).

stefanB
+2  A: 

seems like you don't compile all the files needed, you only compile a single file—which in turn doesn't have the main function

knittl
A: 

Looks like you might be missing some libraries.

OJ
A: 

It seems to be that the definition of functions such as "clear_list", "push" etc, cannot be located. Look for the libraries/objects/files containing these definition and then verify if they are linked properly to you application.

Afridi