android ndk交叉编译pcl 1.9.1

目录

  • android ndk交叉编译pcl 1.9.1
    • 依赖库
    • android ndk 下载
    • 使用cmake交叉编译lib库
    • 编译pcl
    • 完整的android studio cmake配置代码


android ndk交叉编译pcl 1.9.1

依赖库

  • boost: boost -for-android(github)
  • flann
  • qhull
  • eigen
  • openni2(暂未编译)

android ndk 下载

  • 一般使用android studio集成环境下载,也可单独下载,在此不赘述。
  • 编写交叉编译toolchain.cmake脚本
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21) # API level
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_NDK /home/yourhome/ndk) # 到你的ndk根目录

使用cmake交叉编译lib库

  • 注意:要选用较新版本的cmake,本文之前选用ubuntu apt安装的cmake版本为3.10.2,始终有各种问题,后来换成了3.17.5配置成功。
  • configure时选择第3项——导入上面编写的交叉编译脚本,之后就是正常的cmake配置使用,在此不赘述。使用此方法编译flann、qhull、pcl等。
  • pcl中的qhull库依赖:QHULL_INCLUDE_DIRS和QHULL_LIBRARIES,需设置这两个变量,cmake中add entry添加。
  • pcl中的flann库依赖:FLANN_INCLUDE_DIRS和FLANN_LIBRARIES,需设置这两个变量,cmake中add entry添加。
  • 取消PCL_SHARED_LIBS的勾选,编译pcl静态库。

编译pcl

  • 本人编译pcl版本是1.9.1,在执行编译时报了错,后来分析在pcl_config.h中添加的如下代码解决了该问题:
#define HAVE_QHULL 1
#define HAVE_QHULL_2011 1
#define ANDROID 1		// 此为添加的代码,因没有定义ANDROID宏,所以在用户配置文件中做了添加
  • 在android studio链接上述编译的静态库时出现openmp链接不上的错误,此时需添加以下cmake配置代码:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

完整的android studio cmake配置代码

cmake_minimum_required(VERSION 3.4.1)
project(example)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

# 设置库路径
set(3RD_ROOT "/home/xxx/3rdParty/arm64-v8a")
set(PCL_INCLUDE "${3RD_ROOT}/pcl")
set(BOOST_INCLUDE "${3RD_ROOT}/boost")
set(FLANN_INCLUDE "${3RD_ROOT}/flann")
set(EIGEN_INCLUDE "${3RD_ROOT}/eigen")
set(QHULL_INCLUDE "${3RD_ROOT}/qhull")

set(PCL_STATIC_LIB_DIR "${PCL_INCLUDE}/lib")
set(BOOST_STATIC_LIB_DIR "${BOOST_INCLUDE}/lib")
set(FLANN_STATIC_LIB_DIR "${FLANN_INCLUDE}/lib")
set(QHULL_STATIC_LIB_DIR "${QHULL_INCLUDE}/lib")

include_directories(${PCL_INCLUDE}/include)
include_directories(${BOOST_INCLUDE}/include)
include_directories(${FLANN_INCLUDE}/include)
include_directories(${QHULL_INCLUDE}/include)
include_directories(${EIGEN_INCLUDE}/include/eigen3)

function(importLib dirName libName)
  add_library(${libName} SHARED IMPORTED)
  set_target_properties(${libName}
          PROPERTIES
          IMPORTED_LOCATION
          "${dirName}/${libName}.a")
endfunction(importLib)

set(libFlannNames
        libflann_s
        libflann_cpp_s
        )

set(libBoostNames
        libboost_thread-clang-mt-a64-1_69
        libboost_program_options-clang-mt-a64-1_69
        libboost_filesystem-clang-mt-a64-1_69
        libboost_system-clang-mt-a64-1_69
        libboost_regex-clang-mt-a64-1_69
        libboost_iostreams-clang-mt-a64-1_69
        libboost_date_time-clang-mt-a64-1_69
        )

set(libQhullNames
        libqhullstatic
        )

set(libPclNames
        libpcl_stereo
        libpcl_segmentation
        libpcl_recognition
        libpcl_ml
        libpcl_io
        libpcl_io_ply
        libpcl_filters
        libpcl_tracking
        libpcl_search
        libpcl_registration
        libpcl_keypoints
        libpcl_features
        libpcl_surface
        libpcl_sample_consensus
        libpcl_octree
        libpcl_kdtree
        libpcl_common
        )

foreach(lib ${libPclNames})
  importLib(${PCL_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libFlannNames})
  importLib(${FLANN_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libBoostNames})
  importLib(${BOOST_STATIC_LIB_DIR} ${lib})
endforeach(lib)

foreach(lib ${libQhullNames})
  importLib(${QHULL_STATIC_LIB_DIR} ${lib})
endforeach(lib)

set(OpenCV_DIR "/home/xxx/Dev/OpenCV-android-sdk/sdk/native/jni")
find_package(OpenCV REQUIRED)

add_library(meas3d SHARED meas3dlib.cpp Meas3D.cpp)

find_library(log-lib log)

#if(EIGEN3_FOUND AND NOT TARGET Eigen3::Eigen3)
#  add_library(Eigen3::Eigen3 INTERFACE IMPORTED)
#  set_target_properties(Eigen3::Eigen3 PROPERTIES
#          INTERFACE_INCLUDE_DIRECTORIES "${EIGEN3_INCLUDE_DIR}")
#endif()

target_link_libraries(meas3d
    PUBLIC
    ${log-lib}
    ${libPclNames}
    ${libFlannNames}
    ${libBoostNames}
    ${libQhullNames}
    ${OpenCV_LIBS}
    )

你可能感兴趣的:(日常记录,cmake,c++,android,studio,ndk,linux)