pelya / commandergenius

Port of SDL library and several games to the Android OS.
http://libsdl-android.sourceforge.net/
GNU Lesser General Public License v2.1
531 stars 248 forks source link

undefined reference to 'SDL_main' #124

Closed 1000283 closed 3 years ago

1000283 commented 3 years ago

I have an application that already uses SDL, and compiles for Windows and Linux.

I pulled the latest version of this project and, after reading the readme, was able to compile one of the demo applications. I had to fix licenses and gradle's java path.

However, i cannot compile my app, i always get his linker error:

/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -Wl,-soname,libsdl_main.so -shared ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o -lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/armeabi-v7a/libsdl-1.2.so ./obj/local/armeabi-v7a/libapplication.so -target armv7-none-linux-androideabi16 -no-canonical-prefixes -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,--fatal-warnings -llog -lc -lm -o ./obj/local/armeabi-v7a/libsdl_main.so jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main' clang++: error: linker command failed with exit code 1 (use -v to see invocation) make: [/home/myuser/Android/Sdk/ndk/21.1.6352462/build/core/build-binary.mk:725: obj/local/armeabi-v7a/libsdl_main.so] Error 1 make: Waiting for unfinished jobs.... [armeabi-v7a] Install : libsdl-1.2.so => libs/armeabi-v7a/libsdl-1.2.so install -p ./obj/local/armeabi-v7a/libsdl-1.2.so ./libs/armeabi-v7a/libsdl-1.2.so /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip --strip-unneeded ./libs/armeabi-v7a/libsdl-1.2.so

I'm not sure if i'm missing some enviroment variables.

I've run ./changeAppSettings.sh ./project/jni/application/setEnvironment-x86_64.sh but i have no LD_LIBRARY_PATH set, nor $CFLAGS or $LDFLAGS.

This is my system:

$ uname -a Linux myhost-VirtualBox 4.15.0-123-generic #126~16.04.1-Ubuntu SMP Wed Oct 21 13:48:05 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.7 LTS"

$ javac -version javac 1.8.0_275

(installed from openjdk-8-* packages)

$ /home/myuser/Android/Sdk/ndk/21.1.6352462/ndk-build -version GNU Make 4.2.1 Built for x86_64-pc-linux-gnu

Help would be appreciated.

pelya commented 3 years ago

Include SDL.h in the source file with your main(), it will do

define main SDL_main

Or just rename it yourself.

On Mon, 30 Nov 2020, 13:15 V, notifications@github.com wrote:

I have an application that already uses SDL, and compiles for Windows and Linux.

I pulled the latest version of this project and, after reading the readme, was able to compile one of the demo applications. I had to fix licenses and gradle's java path.

However, i cannot compile my app, i always get his linker error:

/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -Wl,-soname,libsdl_main.so -shared ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o -lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/armeabi-v7a/libsdl-1.2.so ./obj/local/armeabi-v7a/libapplication.so -target armv7-none-linux-androideabi16 -no-canonical-prefixes -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,--fatal-warnings -llog -lc -lm -o ./obj/local/armeabi-v7a/libsdl_main.so jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main' clang++: error: linker command failed with exit code 1 (use -v to see invocation) make: [/home/myuser/Android/Sdk/ndk/21.1.6352462/build/core/ build-binary.mk:725: obj/local/armeabi-v7a/libsdl_main.so] Error 1 make: Waiting for unfinished jobs.... [armeabi-v7a] Install : libsdl-1.2.so => libs/armeabi-v7a/libsdl-1.2.so install -p ./obj/local/armeabi-v7a/libsdl-1.2.so ./libs/armeabi-v7a/ libsdl-1.2.so /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip --strip-unneeded ./libs/armeabi-v7a/libsdl-1.2.so

I'm not sure if i'm missing some enviroment variables.

I've run ./changeAppSettings.sh ./project/jni/application/setEnvironment-x86_64.sh but i have no LD_LIBRARY_PATH set, nor $CFLAGS or $LDFLAGS.

This is my system:

$ uname -a Linux myhost-VirtualBox 4.15.0-123-generic #126~16.04.1-Ubuntu SMP Wed Oct 21 13:48:05 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.7 LTS"

$ javac -version javac 1.8.0_275

(installed from openjdk-8-* packages)

$ /home/myuser/Android/Sdk/ndk/21.1.6352462/ndk-build -version GNU Make 4.2.1 Built for x86_64-pc-linux-gnu

Help would be appreciated.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pelya/commandergenius/issues/124, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABF5QG722OR2GB4JXGQN43SSN5ETANCNFSM4UHNPF5A .

1000283 commented 3 years ago

I had already done those, to no avail.

At the moment i'm using

#include <SDL/SDL.h>
#define main SDL_main

and also had

int SDL_main(int argc, char* argv[])
{
    main(argc,argv);
}

My main is int main(int argc, char* argv[]), the liking error is not thrown by my app's code, but that may or may not be misleading.

jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main'
jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main'
jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main'
pelya commented 3 years ago

Maybe it's because of C++ name mangling? Please try to add this line before your SDL_main function:

extern "C" int SDL_main(int argc, char* argv[]);

On Mon, 30 Nov 2020, 17:25 V, notifications@github.com wrote:

I had already done those, to no avail.

At the moment i'm using

include <SDL/SDL.h>

define main SDL_main

and also had

int SDL_main(int argc, char* argv[]) { main(argc,argv); }

My main is int main(int argc, char* argv[]), the liking error is not thrown by my app's code, but that may or may not be misleading.

jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main'

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pelya/commandergenius/issues/124#issuecomment-735853945, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABF5QE62WJY2GVVDVBPLXDSSO2PJANCNFSM4UHNPF5A .

1000283 commented 3 years ago

No, didn't help.

This is the offending line:

/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -v -Wl,-soname,libsdl_main.so -shared 
    ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o 
    ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o 
    -lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -latomic -Wl,--exclude-libs,libatomic.a 
    ./obj/local/armeabi-v7a/libsdl-1.2.so 
    ./obj/local/armeabi-v7a/libapplication.so 
    -target armv7-none-linux-androideabi16 -no-canonical-prefixes    
    -Wl,--build-id   -nostdlib++ -Wl,--no-undefined -Wl,--fatal-warnings -llog -lc -lm 
    -o ./obj/local/armeabi-v7a/libsdl_main.so
Android (6317467 based on r365631c1) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project e0caee08e5f09b374a27a676d04978c81fcb1928) (based on LLVM 9.0.8svn)
Target: armv7-none-linux-android16
Thread model: posix
InstalledDir: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@mthumb
Candidate multilib: armv7-a;@march=armv7-a
Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
Candidate multilib: .;
Selected multilib: armv7-a;@march=armv7-a
    "/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" 
    -z noexecstack -EL --warn-shared-textrel -z now -z relro -X --hash-style=gnu --hash-style=both --enable-new-dtags --eh-frame-hdr -m armelf_linux_eabi -shared 
    -o ./obj/local/armeabi-v7a/libsdl_main.so 
    /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16/crtbegin_so.o 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/lib/linux/arm 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/armv7-a 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/../lib 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/../../lib 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a 
    -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib 
    -soname libsdl_main.so 
    ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o 
    ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o 
    -lgcc --exclude-libs libgcc.a --exclude-libs libgcc_real.a -latomic --exclude-libs libatomic.a 
    ./obj/local/armeabi-v7a/libsdl-1.2.so 
    ./obj/local/armeabi-v7a/libapplication.so 
    --build-id --no-undefined --fatal-warnings -llog -lc -lm -lm -lgcc -ldl -lc -lgcc -ldl 
    /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16/crtend_so.o

And the resulting error:

jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main'
jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main'
jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Looking at sdl_main.c it does #include "SDL_main.h" and i assume "" will look into -I paths (although i thought it meant "same dir as file"), and i see a lot of -Ijni/../jni/sdl-1.2/include so it should be ok.

I made the following changes to project/jni/application/myuser/AndroidAppSettings.cfg

    # Specify architectures to compile, 'all' or 'y' to compile for all architectures.
    # Available architectures: armeabi-v7a arm64-v8a x86 x86_64
    MultiABI='all'

    # Aditional CFLAGS for application
    AppCflags='-Dmain=SDL_main -DBYPASS_PROTECTION'

    # Aditional C++-specific compiler flags for application, added after AppCflags
    AppCppflags=''

    # Additional LDFLAGS for application
    AppLdflags='-z defs'

But from what i can see, the LD flags only apply to libapplication.so. The remaining .so files seem to have a lot of undefined symbols:

$ nm -C -u project/obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o 
         U __aeabi_memclr
         U __aeabi_unwind_cpp_pr0
         U __aeabi_unwind_cpp_pr1
         U __android_log_print
         U chdir
         U exit
         U malloc
         U SDL_ANDROID_JavaVM
         U SDL_ANDROID_MultiThreadedVideoLoop
         U SDL_ANDROID_MultiThreadedVideoLoopInit
         U SDL_CreateThread
         U SDL_main
         U setenv
         U __stack_chk_fail
         U __stack_chk_guard
         U strchr
         U strcpy
         U strdup
         U strlen
$ nm -C -u project/obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o
         U __aeabi_unwind_cpp_pr0
         U __aeabi_unwind_cpp_pr1
         U __android_log_print
         U SDL_Android_Init
         U SDL_main
         U __stack_chk_fail
         U __stack_chk_guard
         U strdup
$ nm -C -u project/obj/local/armeabi-v7a/libsdl-1.2.so
         U abort
         U __aeabi_memclr
         U __aeabi_memclr4
         U __aeabi_memclr8
         U __aeabi_memcpy
         U __aeabi_memcpy4
         U __aeabi_memmove4
         U __aeabi_memset
         U __android_log_print
         U atoi
         U calloc
         U clock_gettime
         U __cxa_atexit
         U __cxa_finalize
         U dladdr
         U dlclose
         U dlerror
         U dlopen
         U dlsym
         U __errno
         U fclose
         U ferror
         U fflush
         U fmaxf
         U fminf
         U fopen
         U fprintf
         U fread
         U free
         U fseek
         U ftell
         U fwrite
         U getenv
         U gettimeofday
         U glActiveTexture
         U glBindTexture
         U glBlendFunc
         U glColor4f
         U glDeleteTextures
         U glDisable
         U glDisableClientState
         U glDrawArrays
         U glDrawTexiOES
         U glEnable
         U glEnableClientState
         U glGenTextures
         U glGetError
         U glGetIntegerv
         U glGetString
         U glLoadIdentity
         U glMatrixMode
         U glOrthof
         U glPixelStorei
         U glPopMatrix
         U glPushMatrix
         U glReadPixels
         U glTexCoordPointer
         U glTexEnvf
         U glTexImage2D
         U glTexParameteri
         U glTexParameteriv
         U glTexSubImage2D
         U glVertexPointer
         U glViewport
         U __gnu_Unwind_Find_exidx
         U log
         U malloc
         U memcmp
         U nanosleep
         U pow
         U pthread_attr_destroy
         U pthread_attr_init
         U pthread_attr_setdetachstate
         U pthread_cond_broadcast
         U pthread_cond_destroy
         U pthread_cond_init
         U pthread_cond_signal
         U pthread_cond_timedwait
         U pthread_cond_wait
         U pthread_create
         U pthread_exit
         U pthread_join
         U pthread_kill
         U pthread_mutexattr_init
         U pthread_mutexattr_settype
         U pthread_mutex_destroy
         U pthread_mutex_init
         U pthread_mutex_lock
         U pthread_mutex_unlock
         U pthread_self
         U pthread_sigmask
         U qsort
         U raise
         U realloc
         U sem_destroy
         U sem_getvalue
         U sem_init
         U sem_post
         U sem_timedwait
         U sem_trywait
         U sem_wait
         U __sF
         U snprintf
         U __stack_chk_fail
         U __stack_chk_guard
         U strcasecmp
         U strchr
         U strcmp
         U strdup
         U strlcpy
         U strlen
         U strncpy
         U strstr
         U tolower
         U toupper
         U truncf
$ nm -C -u project/obj/local/armeabi-v7a/libapplication.so
         U __cxa_atexit
         U __cxa_finalize

Also, i have these libs installed system-wide:

# sudo apt list --installed | grep sdl

    libsdl-gfx1.2-5/xenial,now 2.0.25-5 amd64 [installed,automatic]
    libsdl-gfx1.2-dev/xenial,now 2.0.25-5 amd64 [installed]
    libsdl-image1.2/xenial-updates,xenial-security,now 1.2.12-5+deb9u1ubuntu0.16.04.1 amd64 [installed,automatic]
    libsdl-image1.2-dev/xenial-updates,xenial-security,now 1.2.12-5+deb9u1ubuntu0.16.04.1 amd64 [installed]
    libsdl-ttf2.0-0/xenial,now 2.0.11-3 amd64 [installed,automatic]
    libsdl-ttf2.0-dev/xenial,now 2.0.11-3 amd64 [installed]
    libsdl1.2-dev/xenial-updates,xenial-security,now 1.2.15+dfsg1-3ubuntu0.1 amd64 [installed]
    libsdl1.2debian/xenial-updates,xenial-security,now 1.2.15+dfsg1-3ubuntu0.1 amd64 [installed,automatic]
pelya commented 3 years ago

Your libapplication.so is empty, it should contain SDL_main and the rest of your app compiled code.

Did you enable custom build script option in AndroidAppSettings.cfg? Are you using AndroidBuild.sh script to compile your code? Build system builds an empty libapplication.so then replaces it with your actual library before linking, but it looks like you have it in the wrong place. It should be libapplication-armeabi-v7a.so in the same directory as AndroidBuild.sh.

On Mon, 30 Nov 2020, 19:24 V, notifications@github.com wrote:

No, didn't help.

This is the offending line:

/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -v -Wl,-soname,libsdl_main.so -shared ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o -lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/armeabi-v7a/libsdl-1.2.so ./obj/local/armeabi-v7a/libapplication.so -target armv7-none-linux-androideabi16 -no-canonical-prefixes -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,--fatal-warnings -llog -lc -lm -o ./obj/local/armeabi-v7a/libsdl_main.so Android (6317467 based on r365631c1) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project e0caee08e5f09b374a27a676d04978c81fcb1928) (based on LLVM 9.0.8svn) Target: armv7-none-linux-android16 Thread model: posix InstalledDir: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin Found candidate GCC installation: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x Selected GCC installation: /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x Candidate multilib: thumb;@mthumb Candidate multilib: armv7-a;@march=armv7-a Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb Candidate multilib: .; Selected multilib: armv7-a;@march=armv7-a "/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" -z noexecstack -EL --warn-shared-textrel -z now -z relro -X --hash-style=gnu --hash-style=both --enable-new-dtags --eh-frame-hdr -m armelf_linux_eabi -shared -o ./obj/local/armeabi-v7a/libsdl_main.so /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16/crtbegin_so.o -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/lib/linux/arm -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/armv7-a -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16 -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/../lib -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/../../lib -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a -L/home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib -soname libsdl_main.so ./obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o ./obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o -lgcc --exclude-libs libgcc.a --exclude-libs libgcc_real.a -latomic --exclude-libs libatomic.a ./obj/local/armeabi-v7a/libsdl-1.2.so ./obj/local/armeabi-v7a/libapplication.so --build-id --no-undefined --fatal-warnings -llog -lc -lm -lm -lgcc -ldl -lc -lgcc -ldl /home/myuser/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/arm-linux-androideabi/16/crtend_so.o

And the resulting error:

jni/../jni/sdl_main/sdl_main.c:138: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/sdl_main.c:58: error: undefined reference to 'SDL_main' jni/../jni/sdl_main/SDL_android_main.cpp:37: error: undefined reference to 'SDL_main' clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Looking at sdl_main.c it does #include "SDL_main.h" and i assume "" will look into -I paths (although i thought it meant "same dir as file"), and i see a lot of -Ijni/../jni/sdl-1.2/include so it should be ok.

I made the following changes to project/jni/application/myuser/AndroidAppSettings.cfg

Specify architectures to compile, 'all' or 'y' to compile for all architectures.

Available architectures: armeabi-v7a arm64-v8a x86 x86_64

MultiABI='all'

Aditional CFLAGS for application

AppCflags='-Dmain=SDL_main -DBYPASS_PROTECTION'

Aditional C++-specific compiler flags for application, added after AppCflags

AppCppflags=''

Additional LDFLAGS for application

AppLdflags='-z defs'

But from what i can see, the LD flags only apply to libapplication.so. The remaining .so files seem to have a lot of undefined symbols:

$ nm -C -u project/obj/local/armeabi-v7a/objs/sdl_main/sdl_main.o U aeabi_memclr U __aeabi_unwind_cpp_pr0 U aeabi_unwind_cpp_pr1 U android_log_print U chdir U exit U malloc U SDL_ANDROID_JavaVM U SDL_ANDROID_MultiThreadedVideoLoop U SDL_ANDROID_MultiThreadedVideoLoopInit U SDL_CreateThread U SDL_main U setenv U stack_chk_fail U stack_chk_guard U strchr U strcpy U strdup U strlen $ nm -C -u project/obj/local/armeabi-v7a/objs/sdl_main/SDL_android_main.o U __aeabi_unwind_cpp_pr0 U aeabi_unwind_cpp_pr1 U android_log_print U SDL_Android_Init U SDL_main U stack_chk_fail U stack_chk_guard U strdup $ nm -C -u project/obj/local/armeabi-v7a/libsdl-1.2.so U abort U __aeabi_memclr U aeabi_memclr4 U aeabi_memclr8 U __aeabi_memcpy U aeabi_memcpy4 U aeabi_memmove4 U __aeabi_memset U android_log_print U atoi U calloc U clock_gettime U cxa_atexit U __cxa_finalize U dladdr U dlclose U dlerror U dlopen U dlsym U errno U fclose U ferror U fflush U fmaxf U fminf U fopen U fprintf U fread U free U fseek U ftell U fwrite U getenv U gettimeofday U glActiveTexture U glBindTexture U glBlendFunc U glColor4f U glDeleteTextures U glDisable U glDisableClientState U glDrawArrays U glDrawTexiOES U glEnable U glEnableClientState U glGenTextures U glGetError U glGetIntegerv U glGetString U glLoadIdentity U glMatrixMode U glOrthof U glPixelStorei U glPopMatrix U glPushMatrix U glReadPixels U glTexCoordPointer U glTexEnvf U glTexImage2D U glTexParameteri U glTexParameteriv U glTexSubImage2D U glVertexPointer U glViewport U gnu_Unwind_Find_exidx U log U malloc U memcmp U nanosleep U pow U pthread_attr_destroy U pthread_attr_init U pthread_attr_setdetachstate U pthread_cond_broadcast U pthread_cond_destroy U pthread_cond_init U pthread_cond_signal U pthread_cond_timedwait U pthread_cond_wait U pthread_create U pthread_exit U pthread_join U pthread_kill U pthread_mutexattr_init U pthread_mutexattr_settype U pthread_mutex_destroy U pthread_mutex_init U pthread_mutex_lock U pthread_mutex_unlock U pthread_self U pthread_sigmask U qsort U raise U realloc U sem_destroy U sem_getvalue U sem_init U sem_post U sem_timedwait U sem_trywait U sem_wait U sF U snprintf U stack_chk_fail U stack_chk_guard U strcasecmp U strchr U strcmp U strdup U strlcpy U strlen U strncpy U strstr U tolower U toupper U truncf $ nm -C -u project/obj/local/armeabi-v7a/libapplication.so U __cxa_atexit U __cxa_finalize

Also, i have these libs installed system-wide:

sudo apt list --installed | grep sdl

libsdl-gfx1.2-5/xenial,now 2.0.25-5 amd64 [installed,automatic] libsdl-gfx1.2-dev/xenial,now 2.0.25-5 amd64 [installed] libsdl-image1.2/xenial-updates,xenial-security,now 1.2.12-5+deb9u1ubuntu0.16.04.1 amd64 [installed,automatic] libsdl-image1.2-dev/xenial-updates,xenial-security,now 1.2.12-5+deb9u1ubuntu0.16.04.1 amd64 [installed] libsdl-ttf2.0-0/xenial,now 2.0.11-3 amd64 [installed,automatic] libsdl-ttf2.0-dev/xenial,now 2.0.11-3 amd64 [installed] libsdl1.2-dev/xenial-updates,xenial-security,now 1.2.15+dfsg1-3ubuntu0.1 amd64 [installed] libsdl1.2debian/xenial-updates,xenial-security,now 1.2.15+dfsg1-3ubuntu0.1 amd64 [installed,automatic]

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pelya/commandergenius/issues/124#issuecomment-735926783, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABF5QCF2A5OP352NHK7CPTSSPIORANCNFSM4UHNPF5A .

1000283 commented 3 years ago

My project builds an executable, not a shared library. I'm not using a custom build script or AndroidBuild.sh, i'll give that a try.

pelya commented 3 years ago

On Android every app is a shared librar wrapped in Java code. You build executablea only when you build Android ROM.

On Fri, 4 Dec 2020, 12:42 V, notifications@github.com wrote:

My project builds an executable, not a shared library. I'm not using a custom build script or AndroidBuild.sh, i'll give that a try.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pelya/commandergenius/issues/124#issuecomment-738711056, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABF5QEFHZ56ZPSIWA436JLSTC4IDANCNFSM4UHNPF5A .

1000283 commented 3 years ago

I see a lot of mv appbinary libapplication.so in many AndroidBuild.sh files. I don't think an ELF binary is equivalent to a .so, is it? So i assume there's a lot of magic surrounding the wrapping process. Or should my appbinary be first compiled to arm instead of x86?

Anyway i compiled the executable to x86 linux (using the usual procedure for it) and used AndroidBuild.sh to rename it to .so. I set the CustomBuildScript=y and it compiled (a lot of SDL stuff) and proceded to create an apk. Which crashes.

The executable needs extra libs and i've seen in the readme i'll have to add them to program/jni and tinker with the Android.mk files, but can these libs be system libs (x86) or must they be arm(*)? I'm asking 'cos i've seen errors like "can't find file" or "unrecognized format" on libs that exist on my system, which leads me to think this is an architecture issue.

Or does the NDK abstract away the architecture?

pelya commented 3 years ago

Yes, you cannot use an executable in place of .so file and expect it to work, and you cannot run x86 executable on ARM CPU.

The way an app works on Android is start Java virtual machine first (with ART inside, but it's still Java) → run Java wrapper code → load your libapplication.so

The 'executable' here is Java virtual machine, not your application main(). You can compile your own executable, but without Java support code, you can only use command line, you cannot create video output or get touchscreen input.

There is some support to build C++ apps without Java code inside Android SDK, but my SDL port does not use it, because it was added long after I've got my code to work.

On Fri, 4 Dec 2020, 16:46 V, notifications@github.com wrote:

I see a lot of mv appbinary libapplication.so in many AndroidBuild.sh files. I don't think an ELF binary is equivalent to a .so, is it? So i assume there's a lot of magic surrounding the wrapping process. Or should my appbinary be first compiled to arm instead of x86?

Anyway i compiled the executable to x86 linux (using the usual procedure for it) and used AndroidBuild.sh to rename it to .so. I set the CustomBuildScript=y and it compiled (a lot of SDL stuff) and proceded to create an apk. Which crashes.

The executable needs extra libs and i've seen in the readme i'll have to add them to program/jni and tinker with the Android.mk files, but can these libs be system libs (x86) or must they be arm(*)? I'm asking 'cos i've seen errors like "can't find file" or "unrecognized format" on libs that exist on my system, which leads me to think this is an architecture issue.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pelya/commandergenius/issues/124#issuecomment-738823247, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABF5QDGIAPYB272XKQKQULSTDY3PANCNFSM4UHNPF5A .

1000283 commented 3 years ago

Yes, it's a PEBKAC. I was confusing the concept of "native".

My app compiles for x86 linux and windows. There are a lot of #ifdef _ANDROID_ in the code, but i haven't gotten it to compile to arm yet.

I think the error: undefined reference to 'SDL_main' is the linker trying to link different architectures (my app's x86 and the to-be-generated armeabi-v7a).

A previous apk does have all libs and libapplication.so as arm, so... back to digital archaeology. :)

1000283 commented 3 years ago

The way i ended up fixing my issues was by making sure my app compiles with clang (had to fix some bugs 'cos clang seems to be pickier than gcc), change some flags and have the app be built by ./build.sh and custom build script.

export ANDROID_NDK_HOME=${ANDROID_HOME}/ndk/21.1.6352462
export PATH=${ANDROID_NDK_HOME}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools:${JAVA_HOME}/bin:${PATH}
export PATH=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}
export CXX=clang++
export CXXFLAGS="-DNDEBUG -g2 -O3 -fstack-protector-all"

Also some changes to the Makefile 'cos of clang:

CXXFLAGS = std=gnu++11
CFLAGS = -stdlib=libc++ -D_GLIBCXX_USE_CXX11_ABI=0