floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.55k stars 469 forks source link

Add define guard around sokol-main stub #947

Closed geooot closed 7 months ago

geooot commented 7 months ago

When working on a project that targets android and ios, this stub function was giving me problems.

In my code, i both define main and sokol_main since the ios version used the main function but the android target used sokol_main. But the linker complained since there was two definitions of sokol_main.

With this guard, I can just define SOKOL_REMOVE_MAIN_STUB to not include it during build.

floooh commented 7 months ago

Update: please skip right to the PS down below!

I don't like the additional config define tbh, but I think I have a better solution:

The underlying problem is that the sokol_app.h Android backend needs to ignore the SOKOL_NO_ENTRY configuration option because the application startup procedure looks entirely different than on other platforms. So the Android backend is a bit of a special case anyway and will always require a sokol_main() function to be provided by the user.

As such, can you try this solution instead which wouldn't require an additional config define?

#if !defined(_SAPP_ANDROID)
/* this is just a stub so the linker doesn't complain */
sapp_desc sokol_main(int argc, char* argv[]) {
    _SOKOL_UNUSED(argc);
    _SOKOL_UNUSED(argv);
    sapp_desc desc;
    _sapp_clear(&desc, sizeof(desc));
    return desc;
}
#endif

...if that works for you, please change the PR accordingly :)

PS: wait a minute, that doesn't make sense :D The problem isn't the Android platform but the other platforms where SOKOL_NO_ENTRY is defined...

You'll need to conditionally remove the sokol_main() function from your code instead on all platforms that are not Android...

This is actually a case where Zig would benefit from a simple text-based preprocessor :)

I would suggest moving those two functions into two different zig source files:

pub fn main() void {
    sapp.run(app_descriptor);
}

export fn sokol_main() sapp.Desc {
    return app_descriptor;
}

...maybe called entry_android.zig and entry.zig and use one or the other as your project root source file in the build.zig.

I guess that's the only way to conditionally hide a function from the linker in Zig, but I'm not sure.