为什么 __android_log_print 未定义?

问题描述 投票:0回答:1

当我尝试将 android_native_app_glue.c 添加到我的 Android CMake 项目时,在构建时出现以下错误。

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)

这是我的 CMakeLists.txt 文件。

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)

为什么会发生此链接器错误以及如何修复它?请注意,native-lib 也使用 __android_log_print 但没有类似的错误。

android android-ndk logcat
1个回答
0
投票

因为你在构建时没有链接liblog

app-glue
。我还建议将
app-glue
制作为静态库而不是共享库。一般来说,您的应用程序拥有的共享库越少越好。

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")

您可能还应该考虑迁移到

GameActivity

© www.soinside.com 2019 - 2024. All rights reserved.