【爬坑记录】ncnn移植到Android上出现的编译问题

【爬坑记录】ncnn移植到Android上出现的编译问题

背景:因为项目中使用的ncnn版本过早,希望编译一下最新版本,观察速度是否有提升,编译时出现如下报错:

报错1:
*/ncnn-20191113/src/allocator.h:63: error: undefined reference to 'posix_memalign'

*/ncnn-20191113/src/allocator.h:63: error: undefined reference to 'posix_memalign'

*/ncnn-20191113/src/allocator.h:63: error: undefined reference to 'posix_memalign'

*/ncnn-20191113/src/allocator.h:63: error: undefined reference to 'posix_memalign'

clang++: error: linker command failed with exit code 1 (use -v to see invocation)
examples/CMakeFiles/faceid_example.dir/build.make:171: recipe for target 'examples/faceid_example' failed
  • 问题成因:使用的编译选项Android_PLATFORM过低,Android_PLATFORM = android-14,修改到 Android_PLATFORM = android-19,问题得以解决;
报错2:
/home/sbb/opensource/ncnn/ncnn-20191113/ncnn-20191113/src/datareader.cpp:93: error: undefined reference to 'AAsset_seek'
/home/sbb/opensource/ncnn/ncnn-20191113/ncnn-20191113/src/datareader.cpp:94: error: undefined reference to 'AAsset_getBuffer'
/home/sbb/opensource/ncnn/ncnn-20191113/ncnn-20191113/src/datareader.cpp:112: error: undefined reference to 'AAsset_seek'
/home/sbb/opensource/ncnn/ncnn-20191113/ncnn-20191113/src/datareader.cpp:120: error: undefined reference to 'AAsset_read'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
examples/CMakeFiles/faceid_example.dir/build.make:171: recipe for target 'examples/faceid_example' failed

  • 解决思路:因为本来项目是好的,现在因为更新ncnn导致了这个问题,第一反应是ncnn的编译选项是否有变化,重新编译了一下,貌似还是不行;使用了如下命令:
readelf -a ncnn.a |grep AAsset

发现:

readelf -a libncnn.a |grep AAsset
Relocation section '.rel.text._ZN4ncnn26DataReaderFromAndroidAssetC2EP6AAsset' at offset 0x37d4 contains 1 entries:
Relocation section '.rel.ARM.exidx.text._ZN4ncnn26DataReaderFromAndroidAssetC2EP6AAsset' at offset 0x37dc contains 2 entries:
00000024  00008e0a R_ARM_THM_CALL    00000000   AAsset_seek
0000002c  00008f0a R_ARM_THM_CALL    00000000   AAsset_getBuffer
00000080  00008e0a R_ARM_THM_CALL    00000000   AAsset_seek
00000008  0000910a R_ARM_THM_CALL    00000000   AAsset_read

ncnn该有的库都是有的,最后定位到,是编译的可执行文件需要依赖Android 库;即这样:

target_link_libraries(example  ufacedet ${OpenCV_LIBS} android)# 需要加上最后一项的Android依赖库;

至此,问题得以解决;
参考链接:

  • 针对问题1:https://www.cnblogs.com/cmd10/p/4915360.html
  • 针对问题2:
    https://stackoverflow.com/questions/12552208/undefined-reference-to-aassetmanager-fromjava

你可能感兴趣的:(爬坑记录)