由于项目算法需要从Linux移植到android,原先的CGAL库也需要进行移植,先现对CGAL的移植过程做下记录,主要是其交叉编译的过程.。
前提条件:
1、主机已安装NDK编译器,版本大于19
2、主机已安装cmake 和 make
3、主机已安装gcc-aarch64 编译器
CGAL官网
The Computational Geometry Algorithms Library (cgal.org)
CGAL依赖 gmp mpfr boost 库,因此,需要先对这三个依赖进行处理,并且不同的CGAL版本对依赖库的版本要求也不相同,详见其在线文档
The GNU MP Bignum Library (gmplib.org)
1、boost库的交叉编译
1.1 aarch64-arm平台
下载安装包后,设置编译脚本build.sh
#!/bin/bash
export CC=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
export CXX=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
if [ -d "./build" ]; then
echo "log dir exist"
else
mkdir build
echo "touch new build dir"
fi
cd ./build
rm -rf *
cd -
echo $CC
echo $CXX
echo ${PWD}
file_path=project-config.jam
rm -rf b2 $file_path
./bootstrap.sh --prefix=${PWD}/build --with-toolset=gcc --without-libraries=python
# 检查文件是否存在
if [ ! -f "$file_path" ]; then
echo "error project-config.jam not exist ."
exit 1
fi
# 使用sed命令替换文件中的'using gcc'为'using g++'
sed -i "s#using gcc ;#using gcc : : ${CC} ;#g" "$file_path"
./b2 install --prefix=${PWD}/build
tree ./build
解压安装包,并将build.sh脚本放入文件夹内,执行脚本
sh build.sh
1.2 android平台
下载boost的安卓编译脚本,并确认安装的ndk版本符合其要求,编译脚本下载路径https://github.com/moritz-wundke/Boost-for-Android
下载完成后,设置编译脚本build.sh
#!/bin/bash
export NDK=/usr/android-ndk-r25c/
export PATH=${PATH}:$NDK
./build-android.sh $NDK #刚才设置的环境变量
#这个工具在编译的时候会自动下载最新版本的boost库,默认编译成静态库;
#如果需要选择特定版本,在执行的时候配置参数:
#刚才设置的环境变量
#./build-android.sh --boost=<version> $NDK
#如果需要编译成so动态库,打开脚本找到:link = static;static改成shared
编译结束在build/out目录下可获得android平台的boost库,其头文件标准路径为,
xxxxxxxx/arm64-v8a/include/boost-1_84/ 注意多了一层boost-1_84
库文件路径 xxxxxxxx/arm64-v8a/lib
参考:Android ndk 方式交叉编译boost库_android boost 验证-CSDN博客
2、gmp库的交叉编译
1.1 aarch64-arm平台
aarch64平台下交叉编译器自带了gmp的库和头文件,无需编译,
若需要编译,下载安装包后,设置编译脚本build.sh
#!/bin/bash
export CC=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
export CXX=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
export CXXFLAGS=${CXXFLAGS}:-malign-double
if [ -d "./build" ]; then
echo "log dir exist"
else
mkdir build
echo "touch new build dir"
fi
cd ./build
rm -rf *
cd -
echo $CC
echo $CXX
echo ${PWD}
./configure --host=aarch64-linux-gnu --enable-cxx --prefix=${PWD}/build
make
make install
tree ./build
解压安装包,并将build.sh脚本放入文件夹内,执行脚本
sh build.sh
编译完成后
1.2 android平台
安卓平台下比较简单,将编译器更换即可,参考如下
#!/bin/bash
#ndk version must later than r19
ANDROID_NDK=/usr/android-ndk-r25c/
ANDROID_API_LEVEL=28
export CC=${ANDROID_NDK}toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_API_LEVEL}-clang
export CXX=${ANDROID_NDK}toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_API_LEVEL}-clang++
#set aligned,otherwise link static lib wiil occur ld: error:not aligned to 8 bytes
export CXXFLAGS=${CXXFLAGS}:-malign-double
if [ -d "./build" ]; then
echo "log dir exist"
else
mkdir build
echo "touch new build dir"
fi
cd ./build
rm -rf *
cd -
echo $CC
echo $CXX
echo ${PWD}
./configure --host=aarch64-linux-android --enable-cxx --prefix=${PWD}/build
make
make install
tree ./build
3、mpfr库的交叉编译
1.1 aarch64-arm平台
aarch64平台下交叉编译器自带了gmp的库和头文件,无需编译,
若需要编译,下载安装包后,设置编译脚本build.sh
#!/bin/bash
export CC=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
export CXX=/usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
#指定gmp的路径,在第二步安装的目录下
GMP_DIR=/home/ccl/Downloads/boost/gmp-6.3.0/build
if [ -d "./build" ]; then
echo "log dir exist"
else
mkdir build
echo "touch new build dir"
fi
cd ./build
rm -rf *
cd -
echo $CC
echo $CXX
echo ${PWD}
./configure --host=aarch64-linux-gnu --enable-cxx --prefix=${PWD}/build --with-gmp=$GMP_DIR
make
make install
tree ./build
解压安装包,并将build.sh脚本放入文件夹内,执行脚本
sh build.sh
编译完成后,可得到如下
1.2 android平台
安卓平台下比较简单,将编译器更换即可,参考如下
#!/bin/bash
#ndk version must later than r19
ANDROID_NDK=/usr/android-ndk-r25c/
ANDROID_API_LEVEL=28
GMP_DIR=/home/ccl/Downloads/cgal/gmp-6.1.2/build
export CC=${ANDROID_NDK}toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_API_LEVEL}-clang
export CXX=${ANDROID_NDK}toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_API_LEVEL}-clang++
if [ -d "./build" ]; then
echo "log dir exist"
else
mkdir build
echo "touch new build dir"
fi
cd ./build
rm -rf *
cd -
echo $CC
echo $CXX
echo ${PWD}
./configure --host=aarch64-linux-android --enable-cxx --prefix=${PWD}/build --with-gmp=$GMP_DIR
make
make install
tree ./build
4、CGAL的交叉编译
1.1 aarch64-arm平台
在CGAL的文件目录下新建build和install文件夹
mkdir build install
切换至build目录下,使用cmake-gui进行配置
/usr/cmake-3.23.0-rc2-linux-x86_64/bin/cmake-gui
进行编译工具配置于 所依赖的 libgmp、libmpfr 、libgmpxx路径设置
勾选以上组件后,设置库的头文件路径于库文件路径
编译器头文件路径于文件安装路径
点击configure按键,观察配置打印log
6,选择gererate,生成的cmake文件就在build目录下
7、进入build目录下执行 sudo make -j4
8、执行 sudo make install
1.2 android平台
在CGAL的文件目录下新建build和install文件夹
mkdir build install
找到CGAL的文件目录下的CMakeLists,在其头部增加以下设置,设置内容分别是android ndk的编译器、gmp gmpxx的头文件和库文件路径、 mpfr的头文件库文件路径、 boost的头文件和库文件路径以及CGAL的编译输出设置等等。
#ndk version must later than r19
SET(ANDROID_NDK /usr/android-ndk-r25c)
SET(CMAKE_TOOLCHAIN_FILE ${ANDROID_NDK}/build/cmake/android.toolchain.cmake)
SET(ANDROID_ABI arm64-v8a)
SET(API_LEVEL 28)
SET(ANDROID_PLATFORM ${API_LEVEL})
SET(CMAKE_ANDROID_NDK ${ANDROID_NDK})
SET(ANDROID_LD default)
SET(ANDROID_STL c++_static)
#ndk version must later than r19
#set GMP PATH
SET(GMP_INCLUDE_DIR /home/ccl/Downloads/boost/gmp-6.3.0/build/include)
SET(GMP_LIBRARIES_DIR /home/ccl/Downloads/boost/gmp-6.3.0/build/lib)
SET(GMP_LIBRARIES /home/ccl/Downloads/boost/gmp-6.3.0/build/lib/libgmp.a)
#set GMPXX PATH
SET(GMPXX_INCLUDE_DIR ${GMP_INCLUDE_DIR})
SET(GMPXX_LIBRARIES /home/ccl/Downloads/boost/gmp-6.3.0/build/lib/libgmpxx.a)
#set MPFR PATH
SET(MPFR_INCLUDE_DIR /home/ccl/Downloads/boost/mpfr-4.0.1/build/include)
SET(MPFR_LIBRARIES_DIR /home/ccl/Downloads/boost/mpfr-4.0.1/build/lib)
SET(MPFR_LIBRARIES /home/ccl/Downloads/boost/mpfr-4.0.1/build/lib/libmpfr.a)
#set BOOST PATH
SET(BOOST_ROOT /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a)
SET(Boost_INCLUDE_DIR /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/include/boost-1_84)
SET(Boost_LIBRARY_DIRS /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/lib/)
#when WITH_CGAL_ImageIO is on, need boost library, here not need
SET(Boost_system_LIBRARY_RELEASE /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/lib/libboost_system-clang-mt-a64-1_84.a)
SET(Boost_thread_LIBRARY_RELEASE /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/lib/libboost_thread-clang-mt-a64-1_84.a)
SET(Boost_date_time_LIBRARY_RELEASE /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/lib/libboost_date_time-clang-mt-a64-1_84.a)
SET(Boost_chrono_LIBRARY_RELEASE /home/ccl/Downloads/boost/Boost-for-Android-master/build/out/arm64-v8a/lib/libboost_chrono-clang-mt-a64-1_84.a)
#set make install path
SET(CMAKE_INSTALL_PREFIX /home/ccl/Downloads/boost/CGAL-5.2/install)
SET(WITH_CGAL_ImageIO OFF)
SET(WITH_CGAL_Qt5 OFF)
SET(CGAL_HEADER_ONLY FALSE)
SET(CMAKE_BUILD_TYPE Release)
SET(BUILD_SHARED_LIBS FALSE) #set cgal static library
cmake变量的定义请参考CGAL官方文档说明:
CGAL 5.4 - Manual: Summary of CGAL's Configuration Variables
添加结束后,在build目录下设置编译脚本build.sh
#!/bin/bash
make clean
/usr/cmake-3.23.0-rc2-linux-x86_64/bin/cmake -DCMAKE_INSTALL_PREFIX=/home/ccl/Downloads/boost/CGAL-5.2/install ../
make -j4
make install
ls ../install/lib
管理员权限运行脚本
sudo sh build.sh
注意:本人使用的ndk版本大于19,当小于19的版本时需要额外使用脚本生成android的编译器,具体可参考以下开源项目
Cross Compilation of CGAL for Android · CGAL/cgal Wiki · GitHub