Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined reference to 'SDL_main' #124

Closed
1000283 opened this issue Nov 30, 2020 · 11 comments
Closed

undefined reference to 'SDL_main' #124

1000283 opened this issue Nov 30, 2020 · 11 comments

Comments

@1000283
Copy link

1000283 commented Nov 30, 2020

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
Copy link
Owner

pelya commented Nov 30, 2020 via email

@1000283
Copy link
Author

1000283 commented Nov 30, 2020

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
Copy link
Owner

pelya commented Nov 30, 2020 via email

@1000283
Copy link
Author

1000283 commented Nov 30, 2020

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
Copy link
Owner

pelya commented Dec 1, 2020 via email

@1000283
Copy link
Author

1000283 commented Dec 4, 2020

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
Copy link
Owner

pelya commented Dec 4, 2020 via email

@1000283
Copy link
Author

1000283 commented Dec 4, 2020

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
Copy link
Owner

pelya commented Dec 4, 2020 via email

@1000283
Copy link
Author

1000283 commented Dec 4, 2020

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 1000283 closed this as completed Dec 4, 2020
@1000283
Copy link
Author

1000283 commented Dec 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants