问题描述

Android项目NDK编译时报错:ld: error: undefined symbol: std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >::~basic_string()

具体报错信息如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
make: Leaving directory 'D:/MyWork/android/libAPI/src/main'
make: *** Waiting for unfinished jobs....
ld: error: undefined symbol: std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >::~basic_string()
>>> referenced by API.cpp:0 (jni/API.cpp:0)
>>> ./obj/local/x86/objs-debug/API/API.o:(_GLOBAL__sub_I_API.cpp)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [D:/MyDevEnv/Android-SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:657: obj/local/x86/libAPI.so] Error 1
make: Entering directory 'D:/MyWork/android/libAPI/src/main'
[x86] SharedLibrary : libAPI.so
make: Leaving directory 'D:/MyWork/android/libAPI/src/main'
make: Entering directory 'D:/MyWork/android/libAPI/src/main'
ld: error: undefined symbol: std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >::~basic_string()
>>> referenced by API.cpp:0 (jni/API.cpp:0)
>>> ./obj/local/arm64-v8a/objs-debug/API/API.o:(_GLOBAL__sub_I_API.cpp)
>>> referenced by API.cpp:0 (jni/API.cpp:0)
>>> ./obj/local/arm64-v8a/objs-debug/API/API.o:(_GLOBAL__sub_I_API.cpp)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [D:/MyDevEnv/Android-SDK/ndk/25.1.8937393/build/../build/core/build-binary.mk:657: obj/local/arm64-v8a/libAPI.so] Error 1
[arm64-v8a] SharedLibrary : libAPI.so
make: Leaving directory 'D:/MyWork/android/libAPI/src/main'

> Task :libAPI:ndkBuild FAILED

Execution failed for task ':libAPI:ndkBuild'.
> Process 'command 'D:\MyDevEnv\Android-SDK\ndk\25.1.8937393/ndk-build.cmd'' finished with non-zero exit value 2

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

问题分析

引入so动态库的时候,会出现上图的错误,所以编译库的时候需要指定:APP_STL := c++_shared

解决方法

在Application.mk中增加如下所示的配置:

1
APP_STL := c++_shared

编译完成后会生成两个文件,其中一个为libc++_shared.so,另外一个为业务库文件,两个文件都需要拷贝。