When I try to add android_native_app_glue.c to my Android CMake project I get the following error when building.
ld: error: undefined symbol: __android_log_print
>>> referenced by android_native_app_glue.c:51 (C:/dev/android/sdk/ndk/22.1.7171670/sources/android/native_app_glue\android_native_app_glue.c:51)
>>> CMakeFiles/app-glue.dir/C_/dev/android/sdk/ndk/22.1.7171670/sources/android/native_app_glue/android_native_app_glue.c.o:(android_app_read_cmd)
Here is my CMakeLists.txt file.
cmake_minimum_required(VERSION 3.22.1)
project(MyGame)
set (APP_GLUE_DIR ${ANDROID_NDK}/sources/android/native_app_glue)
add_library(app-glue SHARED ${APP_GLUE_DIR}/android_native_app_glue.c)
target_include_directories(app-glue PUBLIC ${APP_GLUE_DIR})
add_library(native-lib SHARED ./native-lib.cpp)
target_include_directories(native-lib PUBLIC ${APP_GLUE_DIR})
add_library(libMyGame SHARED IMPORTED )
find_library(log-lib log)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
target_link_libraries(native-lib app-glue android ${log-lib} libMyGame)
Why is this linker error occurring and how can I fix it? Note that native-lib also uses __android_log_print but there are no similar errors for that.
asked Oct 23, 2024 at 1:40
Bungles
2,3293 gold badges34 silver badges63 bronze badges
1 Answer 1
Because you didn't link liblog when building app-glue. I'd also recommend making app-glue a static library rather than shared. Generally speaking the fewer shared libraries your app has the better.
add_library(app-glue STATIC ${APP_GLUE_DIR}/android_native_app_glue.c)
target_include_directories(app-glue PUBLIC ${APP_GLUE_DIR})
# I've got no idea why the `find_library(log-lib log)` thing became popular, but it's
# just an overly complicated way of writing this:
target_link_libraries(app-glue PUBLIC log)
# You don't need to (and shouldn't) add this to `CMAKE_SHARED_LINKER_FLAGS` because
# then it will apply to every library in your project, not just the one that needs it.
target_link_options(app-glue PUBLIC -u ANativeActivity_onCreate")
You should probably also consider migrating to GameActivity.
answered Oct 23, 2024 at 19:51
Dan Albert
10.6k3 gold badges44 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Bungles
Thanks! Unfortunately this didn't work for me. I still see the run-time link error. I think it may have something to do with the fact that my android_main() is in libMyGame but I'm trying to link android_native_app_glue.c with my native_lib module, but I'm not sure. Also I tried your method but with app-glue as SHARED and it wouldn't compile. It said undefined symbol android_main(), which again is in the game library. Seems like the same issue and I may need to just give up and copy android_native_app_glue.c into my game project.
default