Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Linking Error: undefined reference to 'main'

Tags:

c

linker

I have read the other answers on this topic, and unfortunately they have not helped me. I am attempting to link several c programs together, and I am getting an error in response:

$ gcc -o runexp.o scd.o data_proc.o -lm -fopenmp /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status make: * [runexp] Error 1 

I have exactly one main function and it is in runexp. The form is

int main(void) {     ...;      return 0; } 

Any thoughts on why I might get this error? Thanks!

like image 907
Nicole Avatar asked Apr 09 '13 14:04

Nicole


People also ask

How do I fix undefined reference linker error?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

What does undefined reference to main mean in C?

When I get an 'undefined reference to main', it usually means that I have a .c file that does not have int main() in the file.

How do I fix linker error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

How do I fix undefined reference error in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.


2 Answers

You should provide output file name after -o option. In your case runexp.o is treated as output file name, not input object file and thus your main function is undefined.

like image 78
vharavy Avatar answered Oct 20 '22 14:10

vharavy


You're not including the C file that contains main() when compiling, so the linker isn't seeing it.

You need to add it:

$ gcc -o runexp runexp.c scd.o data_proc.o -lm -fopenmp 
like image 29
unwind Avatar answered Oct 20 '22 13:10

unwind