`undefined reference to` main` in the Cpp class without main ()

I ran into this while trying to get an answer. But it looks like the poster had several files and they were not linked, and therefore an error.

But why am I getting this error when using a single file?

g++ myClass.cpp  
/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

And why is mainit necessary here at compile time ( where will he find a mention of the main thing in my code )? mainis the starting point for code execution, but why does the compiler assume what I need mainhere. Can I define it in some other file and use it gcc -oto create an executable?

Or maybe I will miss something else in the code causing the error?

#include<iostream>
class myClass
{

public:
myClass()
{
    std::cout<<"Constructor";
}

~myClass()
{
    std::cout<<"Destructor";    
}

};
+5
source share
5

main . [1], -.

, " , , ", "-c",

g++ -c myClass.cpp

myClass.o, , .

g++ -o myprog myClass.o myOtherClass.o something_that_has_main.o -lsomelib

(, , )

[1] , , . " ", , .

+6

, main. -c:

g++ -c myClass.cpp

, -Wall -Wextra, .

+8

. -c :

g++ -c myClass.cpp
+1

?! -c

g++ -c file.cpp

main.

+1

-c . .

+1

All Articles