Mapping C++ Entrypoint Into Dll

Started by
4 comments, last by Shaarigan 7 years, 8 months ago

Hi,

my current project, is a small library for game developers. The library consists of classes, I used in earlier game projects,

e.g. window creation or JSON parsing.

What's bothering me currently, is that I have different 'main'-signatures across different platforms,

i.e. 'WinMain' on Windows or 'ANativeActivity_onCreate' on Android.

I'd like to map these into a DLL/SO-file, so that the user has only to write a 'main'-entrypoint once, which works across

multiple platforms.

Is it possible to move the entrypoint into a 'external module' or do I have to link statically?

Thanks in advance,

Julien

It's not a shame to make mistakes. As a programmer I am doing mistakes on a daily basis. - JonathanKlein

Advertisement
DLLs do have an "entry point" but this is something totally different, it is called whenever the library is loaded or unloaded or when something related to thread creation/exit happens.

I don't think you can move the main executable's entry point into a DLL at all. But if you could, it wouldn't be meaningful or portable either (because Windows calls the DLL main function a lot more often than e.g. Unices will, which only do that at load time).

Which means you will have to link statically, yes. Although why bother... the compilers that I use will wrap plain normal int main(argc, argv) into whatever the native thingie is without me even knowing.

As far as I know, it doesn't for Android, since it is compiling it to a .SO. That's the problem I am aiming to solve.

It's not a shame to make mistakes. As a programmer I am doing mistakes on a daily basis. - JonathanKlein

If understanding "entry point" of shared library as a function that is invoked when the library is loaded, then on Android it's JNI_OnLoad(). However, I'm not sure what you want to achieve by that. Do you want to hijack the main executable's entry point with your library or something like that :)? If you want to make a user creating single 'main()' function and regardless a platform. Then you're overcomplicating things. You must deliver platform dependent framework that will invoke this function. On Android, you would create simple framework project that loads your library and invokes your "custom" entry point. The REAL entry point on Android starts with JVM, not with the code that runs on it. But as I say, I don't really understand what you want to achieve.

Every platform defines and implements the notion of "entry point" slightly differently. The C and C++ languages do not specify how the platform entry point works - only that user-executed code begins in main(), modulo calls to static initializers.

There is therefore no cross-platform way to modify your entry point at a lower level; main is the cross-platform entry point.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

The SDL api uses a #define:


#define main SDL_main

So the user's int main() becomes int SDL_main(), and SDL's library has int WinMain() vs int main(), and etc, based on the OS...

SFML does it without macroes (except on iOS), using this method:

Most of SFML offers the choice of DLLs or static libs, but for one library sfml-main is static linked, and contains the entrypoints and then calls a user-provided standard entrypoint (int main()).


#ifdef SFML_SYSTEM_WINDOWS

#include <windows.h>

extern int main(int argc, char* argv[]);

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
    return main(__argc, __argv);
}

#endif // SFML_SYSTEM_WINDOWS

So if one part of your library could be statically linked (or perhaps the whole library, if you don't have any licensing kerfuffles), you could define WinMain() there, and have WinMain call main().

Taking a look into Urho3D they use a macro for the entry point of your program


DEFINE_APPLICATION_MAIN(MyApp)

that is internaly ifdef-ed into the various entry point signatures

This topic is closed to new replies.

Advertisement