# Mapping C++ Entrypoint Into Dll

This topic is 781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

Julien

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites

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().

##### Share on other sites

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

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632634
• Total Posts
3007546
• ### Who's Online (See full list)

There are no registered users currently online

×