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) # 增加这一行
相关推荐
百锦再1 小时前
Android Studio开发中Application和Activity生命周期详解
android·java·ide·app·gradle·android studio·studio
移动开发者1号2 小时前
Android现代进度条替代方案
android·app
万户猴2 小时前
【Android蓝牙开发实战-11】蓝牙BLE多连接机制全解析1
android·蓝牙
RichardLai882 小时前
[Flutter 基础] - Flutter基础组件 - Icon
android·flutter
前行的小黑炭2 小时前
Android LiveData源码分析:为什么他刷新数据比Handler好,能更节省资源,解决内存泄漏的隐患;
android·kotlin·android jetpack
清霜之辰2 小时前
安卓 Compose 相对传统 View 的优势
android·内存·性能·compose
_祝你今天愉快2 小时前
再看!NDK交叉编译动态库并在Android中调用
android
一杯凉白开3 小时前
Android View 事件的分发机制 四句口诀 先问拦截再派送,子不处理父兜底, 一旦消费无后续, 滑动冲突靠逻辑。
android
冬田里的一把火33 小时前
[Android]导航栏中插入电源菜单
android
星途码客3 小时前
SQLyog中DELIMITER执行存储过程时出现的前置缩进问题
android