49

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!

unwind
  • 391,730
  • 64
  • 469
  • 606
Nicole
  • 1,255
  • 2
  • 12
  • 15

5 Answers5

37

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.

vharavy
  • 4,881
  • 23
  • 30
11

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
unwind
  • 391,730
  • 64
  • 469
  • 606
4

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.

Bechir
  • 987
  • 10
  • 26
2

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 <stdio.h>
    /* 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.

Kara
  • 6,115
  • 16
  • 50
  • 57
0

ld linker error: undefined reference to `main' in C++:

I know this question is about C, but if you stumble here and happen to be compiling in C++ (as was my case), it may be because you have your main() function inside of a namespace. Move it outside the namespace and it will fix this error!

Ex:

will_not_run.cpp:

#include <iostream>

// <========== comment out the namespace (this lines plus the opening and 
// closing braces) to make this program link!
namespace my_module
{

int main()
{
    std::cout << "hello world!\n\n";

    return 0;
}

} // namespace my_module

If you try to build and run with this command:

time g++ -Wall -Wextra -Werror -O3 -std=gnu++17 will_not_run.cpp \
    -o will_not_run && will_not_run

...you'll get this error:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

To fix this, simply delete the namespace, or move main() outside of it, and it works just fine again!

#include <iostream>

int main()
{
    std::cout << "hello world!\n\n";

    return 0;
}

So, if you need to access your my_module namespace inside the main() function, simply be explicit, and use it as my_module::my_func() or whatever, or call using namespace my_module; in whichever scope you'd like to have access to your namespaced content within your main() function.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265