We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4273

Summary: Android: nativeRunMain(): Couldn’t find function SDL_main in library
Product: SDL Reporter: ZZ <registrirayme>
Component: mainAssignee: Sam Lantinga <slouken>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: blocker    
Priority: P1 CC: sylvain.becker
Version: 2.0.8   
Hardware: ARM   
OS: Android (All)   

Description ZZ 2018-09-26 06:20:23 UTC
I have an issue similar to the one described here on Android:
https://github.com/urho3d/Urho3D/issues/2267

I have main() in a static library which I link to the main .so. I tried different variants but whenever the static library (with main()) is linked to a dynamic library I get the error message:
nativeRunMain(): Couldn’t find function SDL_main in library

The proposed solution in that engine in the link above doesn't work for me:
extern "C" __attribute__((visibility("default"))) int SDL_main(int argc, char** argv); \
int SDL_main(int argc, char** argv) \
Comment 1 Sylvain 2018-09-27 10:34:59 UTC
Could this be the same at bug #4002 ?
I would say no, because in that case this is the library which cannot be found.

use objdump on your build libraries:

objdump -T build/intermediates/ndkBuild/release/obj/local/arm64-v8a/libmain.so | grep main

gives:

00000000000ca508 g    DF .text	0000000000000128  Base        SDL_main
Comment 2 ZZ 2018-09-28 04:57:40 UTC
I don't think that the above bug is anything like this one. From what I've read is that SDL_main get's stripped as unused during linking.
My project has the following structure:
1. executable
2. engine library (dynamic)
3. sdl library (dynamic)
4. a static library that containts int main() (thus contains SDL_main)
In that configuration compiled with MSVC and Clang on Windows, and GCC on Linux it works. On Android on the other hand I get the above error message.

The workaround I found was to compile libraries 2 and 3 as static and to link them to the executable like that (I use CMake):
target_link_libraries (${PROJECT_NAME}
  # Prevent SDL_main from being stripped when linked in a static library
  $<$<PLATFORM_ID:Android>:-Wl,--whole-archive>
  Engine_main
  $<$<PLATFORM_ID:Android>:-Wl,--no-whole-archive>
)
Comment 3 ZZ 2018-09-28 05:09:13 UTC
I missed an important bit of information. I am using NDK r18 and the target arm64-v8a.
While the above workaround helps it is not optimal because it limits flexibility.
Comment 4 Sam Lantinga 2018-09-28 08:12:08 UTC
Yes, if you have a static library, you're only guaranteed to link in the functions that are actually referenced by your executable.

One way to guarantee that you pull it in is to have a static function pointer in your executable that points to the function.