Search code examples
c++visual-c++mingw-w64entry-point

Overriding C++ entry point to be a STATIC method of a class


Preface and the problem

I'm currently studying C++ programming language and game programming. At the moment, I'm working on a simple game engine just to practice 'consistency' and architecture of the API, and due to this reason the idea of mimicing C# 'Program' class appeared.

C# Entry point:

class Program
{
    static void Main(string[] args)
    {
        // Do stuff.
    }
}

C++ analogue required:

class Program
{
public:
    static void Main()
    {
        // Do stuff. 'args' analogue can be ignored, if necessary.
    }
};  

Is it possible to somehow, using linker options, redefine entry point to be a static class method?

Related experience and my theories on this topic

  1. The main reason, why I think, this should be possible is described in the following piece of code (that was successfully compiled using mingw-w64).
#include <iostream>

class Main
{
public:
    static void Foo() { std::cout << "Main::Foo\n"; }
};

void localFoo() { std::cout << "localFoo\n"; }

void callFunc(void(*funcToCall)())
{
    funcToCall();
}

int main()
{
    callFunc(localFoo);
    callFunc(Main::Foo); // Proves that Main::Foo has the same interface as localFoo.

    return 0;
}
  1. (Refers to Win32 API) I abstracted Win32 API into classes and used window procedure as a static member of class. It was absolutely correct to Win32 WNDCLASS and I could even use static members of my class inside this procedure.

Conslusion I made: static fields and methods technically have no differences between global variables and functions, and, since that, they can replace some code, that dates back to C (default entry point, for example).

Notes

  • Both MinGW and MSVC (Visual Studio or cmd) solutions are acceptable.
  • The author of the post is extremely grateful for any information provided :3

Solution

  • Is it possible to somehow, using linker options, redefine entry point to be a static class method?

    No. Not if you want to use the C++ runtime library, at any rate. main (or WinMain) is called by the runtime library once it has completed initialising itself, and that call is hard-coded in the runtime library itself.

    The MSVC linker lets you specify an alternative entry point with the /ENTRY switch (see here), but if you do that you will bypass the runtime library initialisation code and that will break things.