C linking error: undefined reference to 'main'

your GCC command doesn't include runexp.c, it OUTPUTS to runexp.o - sure you're including the source file with the main method ?

Commented Apr 9, 2013 at 14:49

5 Answers 5

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.

answered Apr 9, 2013 at 14:51 4,961 24 24 silver badges 30 30 bronze badges Or just remove the -o option and use the conventional ./a.out , which is a beautiful name ;) Commented Aug 5, 2014 at 22:42

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 
answered Apr 9, 2013 at 14:52 398k 64 64 gold badges 482 482 silver badges 615 615 bronze badges

You are overwriting your object file runexp.o by running this command :

 gcc -o runexp.o scd.o data_proc.o -lm -fopenmp 

In fact, the -o is for the output file. You need to run :

gcc -o runexp.out runexp.o scd.o data_proc.o -lm -fopenmp 

runexp.out will be you binary file.

answered Apr 9, 2013 at 14:57 1,031 12 12 silver badges 26 26 bronze badges

Generally you compile most .c files in the following way:

gcc foo.c -o foo. It might vary depending on what #includes you used or if you have any external .h files. Generally, when you have a C file, it looks somewhat like the following:

#include /* any other includes, prototypes, struct delcarations. */ int main() < */ code */ >

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. If you first learned java, this is an understandable manner of confusion since in Java, your code usually looks like the following:

//any import statements you have public class Foo < int main()<>> 

I would advise looking to see if you have int main() at the top.