4

I was wondering if its possible to define main() inside a class, something like:

struct runtime_entry_point
{
    friend int main()
    {

    }
};

I have tested that and it doesn't work (Almost in GCC 4.8.2):

g++ -o dist/Release/GNU-Linux-x86/turbo build/Release/GNU-Linux-x86/main.o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o: In function `_start': collect2: error: ld exited with status 1

This sounds to me like a no definition of main() error.

Later, I have written main in the classic way:

struct runtime_entry_point
{
    friend int main()
    {

    }
};

int main(){}

Now the compilation fails because int main() was already defined inside the struct runtime_entry_point! Whats happening here?

Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • 13
    Whatever you're doing: don't do it. – Miles Rout May 09 '14 at 07:41
  • @MilesRout I was just out of curiosity. But if you wan't to kow the context, I'm doing template metaprogramming, I'm searching a way to automatice the generation of `main()`, which commonly does nothing in tmp metaprograms. – Manu343726 May 09 '14 at 07:43
  • Note for all people: I'm not asking if definning `main()` in a struct is a good practice or not, I'm asking about the reason of the above situation (`main()` is not defined vs previous definition inside the struct) – Manu343726 May 09 '14 at 07:46
  • 1
    There's a way, immigrate to Java. – 101010 May 09 '14 at 10:11
  • 1
    You can find [here](http://stackoverflow.com/q/23553723/2567683) yesterday's discussion on templatizing `main`, that made it to isocpp's selected recent questions. If you read through the comments of Jefffrey's answer there a thorough explanation of the c++ intrinsic's related to main. But I suppose you're already aware of the post – Nikos Athanasiou May 09 '14 at 10:16

3 Answers3

4

Trivially it is not possible to write main as a part of a class/struct. The linker by default searches for a free main method and links against it, makes it the entry point. You may alter this behavior by at the linker level, in which case main must be a static method in class/struct. But this is linker implementation dependent, not-portable and dangerous.

However, in the second situation you mentioned is a result of violation of One Definition Rule. You are defining a name (main()) more than once in a single translation unit.

Rakib
  • 7,435
  • 7
  • 29
  • 45
2

Аccording to the article on cppreference.com, the following construction:

struct runtime_entry_point
{
    friend int main()
    {
    }
};

Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline.

Linker couldn't find main() in object file (inline function), and you can't declare another main() in the same translation unit (as it already declared).

alexander
  • 2,703
  • 18
  • 16
0

You can not define a function twice, it works when you change the definition into a declaration inside the class/struct:

#include <iostream>

struct runtime_entry_point
{
    friend int main();
};

int main()
{
    std::cout << "Hello, world!" << std::endl;
}

This works just fine with GCC.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180