FurtiveFelon
FurtiveFelon

Reputation: 15166

Compile error: Undefined symbols: "_main", referenced from: start in crt1.10.5.o

I have the following code:

#include <iostream>

using namespace std;

class testing{
   int test() const;
   int test1(const testing& test2);
};

int testing::test() const{
   return 1;
}

int testing::test1(const testing& test2){
   test2.test();
   return 1;
}

after compilation, it gives me the following error:

Undefined symbols:
  "_main", referenced from:
      start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Why is it complaining about main? Can't i declare main in another file and include this one?

Thanks a lot!

Upvotes: 21

Views: 77664

Answers (3)

Sudarshan Sinha
Sudarshan Sinha

Reputation: 94

Try these (they worked for me):

  • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • brew install mingw-w64

Please save your code before running.

For reference refer to this video

Upvotes: 0

Himadri Choudhury
Himadri Choudhury

Reputation: 10353

If you declare the main function in another file, then you must compile the two files separately, and then link them into 1 executable.

Unless you include the entire contents of the file from the file with the main function, that will work too, though a bit odd. But, if you do this then you have to make sure that you compile the file which has the main() function.

Upvotes: 2

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507005

You have tried to link it already:

g++ file.cpp

That will not only compile it, but try to already create the executable. The linker then is unable to find the main function that it needs. Well, do it like this:

g++ -c file.cpp
g++ -c hasmain.cpp

That will create two files file.o and hasmain.o, both only compiled so far. Now you can link them together with g++:

g++ -omy_program hasmain.o file.o

It will automatically figure out that those are files already compiled, and invoke the linker on them to create a file "my_program" which is your executable.

Upvotes: 28

Related Questions