NDK编译X265库到ARM,遇到问题

之前两篇博客介绍了,如何用arm-none-linux-gnueabi-gcc编译器交叉编译x265的库,但用这个编译出的库必须要把动态库加上。我为了要在android项目上应用x265的静态库,还是要用NDK进行编译,这又出现了很多问题,我把我解决了的贴出来,供大家分享,我还在编译中。x265往android上编译,最主要的问题是,android基于linux内核开发,但却对一些库做了修改,有一些库android并不支持,所以我们要针对问题,再解决。
问题一:利用cmake在linux下编译,往cmakelist.txt下添加交叉编译器等信息后,设置如下:

set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_C_COMPILER "ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc ")
set( CMAKE_CXX_COMPILER "ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++")

但却出现问题:

-- cmake version 2.8.9
-- The C compiler identification is GNU 4.8.0
-- The CXX compiler identification is GNU 4.8.0
-- Check for working C compiler: /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
-- Check for working C compiler: /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -- broken
CMake Error at /usr/cmake-2.8.9-Linux-i386/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler
  "/home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc"
  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: /home/heling/x265_1.6/source/CMakeFiles/CMakeTmp



  Run Build Command:/usr/bin/make "cmTryCompileExec4150547758/fast"

  /usr/bin/make -f CMakeFiles/cmTryCompileExec4150547758.dir/build.make
  CMakeFiles/cmTryCompileExec4150547758.dir/build

  make[1]: 正在进入目录
  `/home/heling/x265_1.6/source/CMakeFiles/CMakeTmp'

  /usr/cmake-2.8.9-Linux-i386/bin/cmake -E cmake_progress_report
  /home/heling/x265_1.6/source/CMakeFiles/CMakeTmp/CMakeFiles 1

  Building C object
  CMakeFiles/cmTryCompileExec4150547758.dir/testCCompiler.c.o


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
  -o CMakeFiles/cmTryCompileExec4150547758.dir/testCCompiler.c.o -c
  /home/heling/x265_1.6/source/CMakeFiles/CMakeTmp/testCCompiler.c

  Linking C executable cmTryCompileExec4150547758

  /usr/cmake-2.8.9-Linux-i386/bin/cmake -E cmake_link_script
  CMakeFiles/cmTryCompileExec4150547758.dir/link.txt --verbose=1


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
  CMakeFiles/cmTryCompileExec4150547758.dir/testCCompiler.c.o -o
  cmTryCompileExec4150547758 -rdynamic


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtbegin_dynamic.o: 没有那个文件或目录


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtend_android.o: 没有那个文件或目录


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -lc


  /home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -ldl

  collect2: error: ld returned 1 exit status

  make[1]: *** [cmTryCompileExec4150547758] 错误 1

  make[1]:正在离开目录
  `/home/heling/x265_1.6/source/CMakeFiles/CMakeTmp'

  make: *** [cmTryCompileExec4150547758/fast] 错误 2

在谷歌上搜索,发现很多人都出现这个问题,但没有一个解决方法,我利用NDK,做了一个交叉工具链,再把链接给cmake,如下进行设置:

cd  /ndk/build/tools  //进入ndk目录下的/build/tools
./make-standalone-toolchain.sh --platform=android-14 --ndk-dir=/home/heling/webrtc/WebRTC20140723_bak/trunk/third_party/android_tools/ndk/ --install-dir=/home/heling/android-14-toolchain/ --system=linux-x86_64  //对工具链进行设置    --install-dir工具链安装位置
//然后在cmakelist中做如下设置
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_C_COMPILER "/home/heling/android-14-toolchain/arm-linux-androideabi/bin/gcc")
set( CMAKE_CXX_COMPILER "/home/heling/android-14-toolchain/arm-linux-androideabi/bin/g++")//工具链的绝对地址信息

然后执行 cmake ../source这个问题解决了,但还出现其他的问题。


问题二:
上面一个问题解决后,在make,出现了问题:

[ 30%] Building CXX object common/CMakeFiles/common.dir/picyuv.cpp.o
[ 32%] Building CXX object common/CMakeFiles/common.dir/common.cpp.o
/home/heling/桌面/x265_1.6/source/common/common.cpp: In function 'void* x265_malloc(size_t)':
/home/heling/桌面/x265_1.6/source/common/common.cpp:79:59: error: 'posix_memalign' was not declared in this scope
/home/heling/桌面/x265_1.6/source/common/common.cpp: In function 'double x265_qScale2qp(double)':
/home/heling/桌面/x265_1.6/source/common/common.cpp:151:33: error: 'log2' was not declared in this scope
make[2]: *** [common/CMakeFiles/common.dir/common.cpp.o] 错误 1
make[1]: *** [common/CMakeFiles/common.dir/all] 错误 2
make: *** [all] 错误 2

可以看到这些报错都是因为android不支持这些函数的原因,android不支持log2,所以用log来进行替换。
posix_memalign 这个函数android也不识别,换为memalign。
解决完这个问题后,接着make ,发现又出现了问题。


问题三:

[ 85%] Building CXX object encoder/CMakeFiles/encoder.dir/weightPrediction.cpp.o
[ 85%] Built target encoder
Scanning dependencies of target x265-shared
Linking CXX shared library libx265.so
/home/heling/android-14-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lpthread
collect2: ld returned 1 exit status
make[2]: *** [libx265.so.51] 错误 1
make[1]: *** [CMakeFiles/x265-shared.dir/all] 错误 2
make: *** [all] 错误 2

因为android的ndk虽然有pthread.h,但是没有libpthread.a,集成到libc.a里了,
所以在编译x265时,不需要去链接-lpthread,解决办法就是在source目录下的cmakelist.txt中将

 list(APPEND PLATFORM_LIBS pthread)
 更改为list(APPEND PLATFORM_LIBS)
list(REMOVE_ITEM PLIBLIST "-lc" "-lpthread")
更改为list(REMOVE_ITEM PLIBLIST "-lc")保存后,再cmake../source,最后make 就成功了!!!
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页