undefined reference to `__android_log_print‘

报错描述

在 Android NDK 相关的工程构建中,出现报错:

undefined reference to `__android_log_print'

翻译成 QM 能理解的话: 在链接阶段, 遇到一个需要被链接的符号 __android_log_print, 但是没有在给出的依赖库里面找到 __android_log_print 符号。

复制代码
[2/2] Linking CXX executable testbed
FAILED: testbed
cmd.exe /C "cd . && D:\soft\android-ndk\r26-beta1\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android24 --sysroot=D:/soft/android-ndk/r26-beta1/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -fno-limit-debug-info -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--no-undefined-version -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments CMakeFiles/testbed.dir/main.cpp.o -o testbed  -latomic -lm && cd ."
ld.lld: error: undefined symbol: __android_log_print
>>> referenced by main.cpp:10 (D:/github/min-repros/test_android_log/r21e/main.cpp:10)
>>>               CMakeFiles/testbed.dir/main.cpp.o:(main)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

为什么要链接 __android_log_print

QM 的初始诉求是, 不准开发的代码依赖 log 库。那么谁用了 __android_log_print 符号, 目的又是什么?

__android_log_print 符号, 对应到 C/C++ 函数, 用于打印日志并显示到 Android LogCat。啥意思? 我们开发者提供的 .a/.so 库,最终是放到 Android App 里使用的,而 Android App 是一个 GUI 界面程序, 它是没有控制台的 log 输出给用户看的。 也就是说,如果用户的一些交互操作, 导致 Android App 崩溃了, 并且打算从 Android Studio 的 LogCat 去检查报错问题(此时很可能是 Android Java/Kotlin开发者),那么 LogCat 的报错信息是非常有用出的: 快速定位问题。

当然, 你也可以让 Android NDK 开发者完全不使用 __android_log_print. 那意味着: 如果 Android App 出现 Crash,Android App 的 Java/Kotlin 开发者无从查起, Android NDK 开发者也拿不到 log 内容, 无法定位 bug。

复现代码

cpp 复制代码
#include <string.h>
#include <jni.h>
#include <android/log.h>

#define  LOG_TAG    "foo"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

int main()
{
    LOGI("test");
}

链接 log 库

cmake 复制代码
add_executable(testbed main.cpp)
target_link_libraries(testbed PRIVATE log) # 增加这一行
相关推荐
StarShip19 分钟前
SystemServer类 与 system_server进程
android
画个太阳作晴天1 小时前
Android App 跟随系统自动切换白天/黑夜模式:车机项目实战经验分享
android·android studo
成都大菠萝1 小时前
2-2-2 快速掌握Kotlin-语言的接口默认实现
android
代码s贝多芬的音符1 小时前
android webview 打开相机 相册 图片上传。
android·webview·webview打开相机相册
游戏开发爱好者82 小时前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具
android·ios·小程序·https·uni-app·iphone·webview
StarShip2 小时前
Android system_server进程介绍
android
StarShip2 小时前
Android Context 的 “上下文”
android
成都大菠萝2 小时前
2-6-1 快速掌握Kotlin-语言的接口定义
android
李小轰_Rex2 小时前
纯算法AEC:播录并行场景的回声消除实战笔记
android·音视频开发
ok406lhq3 小时前
unity游戏调用SDK支付返回游戏会出现画面移位的问题
android·游戏·unity·游戏引擎·sdk