有时我们使用第三方提供的静态库加上我们自己的代码需要重新编译出库文件或可执行文件,如果生成可执行文件直接参考https://blog.csdn.net/u013463707/article/details/90754571 即可。
但是如果还想生成库文件,直接修改Android.mk的include $(BUILD_EXECUTABLE) 为include $(BUILD_STATIC_LIBRARY)
是不行的,因为gcc编译时是找不到你编译出的库文件内所包含的库文件里的符号的(例如1.a 由2.o 和3.a组成,使用1.a时只能找到2.o里定义的变量和函数,是找不到3.a里定义的函数和变量的)
那如何处理呢?我们可以先将原库文件解压,例如aarch64-linux-android-ar x 3.a 可以得到3.o 4.o.....,然后使用aarch64-linux-android-ar rc 1.a *.o (*.o 包含原3.a 内的.o文件和自己新增的C文件生成的.o 例如例子中2.o)。这样1.a在被使用时所用 C文件和库文件定义的函数、变量都可以使用了,不会再报未定义错误。
当然还有一种方法就是在自己的.c文件中对3.a所定义的函数变量都进行一次封装,这样可以间接调用到原.a内的函数,显然这种方法并不好,当然当函数接口较少时,也不失为一种解决办法。
那如何将多个库文件转换为一个动态库呢?
多个动态库无法合并为一个动态库,*.so文件不能直接合并,因为其中已经没有重定向信息。
但多个静态库可以合并为一个动态库,方法就是在上面的基础上,将解压出的.o文件进行编译链接即可
aarch64-linux-android-gcc -shared -o libalpu_fa_000.so *.o
但是在操作时遇到找不到一些依赖的动态库和.o文件
cannot find crtbegin_so.o: No such file or directory
cannot find -lc
cannot find -ldl
cannot find crtend_so.o
两个.o文件可以在prebuilts\ndk\current\platforms\android-24\arch-arm64\usr\lib或out\target\product\rk3399_f1\obj\lib中找到
动态库路径:out\target\product\rk3399_f1\obj\SHARED_LIBRARIES\libc_intermediates
out\target\product\rk3399_f1\obj\SHARED_LIBRARIES\libdl_intermediates
将上述.o ,.so拷贝到编译目录
aarch64-linux-android-gcc -shared -Xlinker -zmuldefs -L ./ -o libalpu_fa_000.so *.o
-Xlinker -zmuldefs 是为了解决重复定义的问题
crtbegin_so.o: In function `__atexit_handler_wrapper':
crtbegin_so.c:(.text+0x1c): multiple definition of `__atexit_handler_wrapper'
crtbegin_so.o:crtbegin_so.c:(.text+0x1c): first defined here
crtbegin_so.o:(.data+0x0): multiple definition of `__dso_handle'
crtbegin_so.o:(.data+0x0): first defined here
crtbegin_so.o: In function `atexit':
crtbegin_so.c:(.text+0x44): multiple definition of `atexit'
crtbegin_so.o:crtbegin_so.c:(.text+0x44): first defined here
crtbegin_so.o: In function `__on_dlclose':
crtbegin_so.c:(.text+0x0): multiple definition of `__on_dlclose'
crtbegin_so.o:crtbegin_so.c:(.text+0x0): first defined here
crtbegin_so.o: In function `pthread_atfork':
crtbegin_so.c:(.text+0x78): multiple definition of `pthread_atfork'
crtbegin_so.o:crtbegin_so.c:(.text+0x78): first defined here
参考:https://www.cnblogs.com/fnlingnzb-learner/p/8127456.html