如何在Mac下通过NDK来编译Ceres Android库
前言
网上关于Ceres的安装教程很多,但是Ceres 安卓库真的是很少,Ceres官网对于Android版也更是一句话略过了。即使一些博客有涉及android库如何编译,但编译出的库也大多都是阉割版,很多功能(例如Suite Sparse)无法用。这就意味着你无法求解很复杂的计算,或者即使能够求解也将非常耗时。
所以本文会逐步讲述如何编译一个功能齐全并且可以在安卓手机上调用的Ceres安卓库。
准备工作
在此特别鸣谢一些大佬们的之前工作
https://blog.csdn.net/u010203716/article/details/72955880 这篇博客提供了一个编译好的Ceres库,亲测可用,不过不支持Suite Sparse 多线程求解。
https://www.jianshu.com/p/2e7399a18673
https://github.com/kiutao/suitesparse_android_lib SuiteSparse官网支持很多平台,但是缺没有提供Android平台的…一大堆makefile,如果自己整理会很麻烦。好在这位github作者已经整理好了一份安卓版的SuiteSparse库,而且这个github中附带了suitesparse依赖的lapack第三方库,不用我们自己再去关联!我们只需要根据他的脚本直接编译就好,很良心!
修改Android.mk内容
1.首先进入到刚刚在博客一下载好的ceres-solver-1.12.0/jni
目录下,将里面的Android.mk
文件替换成下面内容。
# Ceres Solver - A fast non-linear least squares minimizer
# Copyright 2015 Google Inc. All rights reserved.
# http://ceres-solver.org/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: settinger@google.com (Scott Ettinger)
# keir@google.com (Keir Mierle)
#
# Builds Ceres for Android, using the standard toolchain (not
# standalone). It uses LLVM's libc++ as the standard library. It is a
# modern BSD licensed implementation of the standard c++ library. We
# do this to avoid any licensing issues that may arise from using
# GCC's libstdc++ which is licensed under GPL3.
#
# Building
# --------
#
# You will have to specify the environment EIGEN_PATH to point to the
# Eigen sources when building. For example:
#
# EIGEN_PATH=/home/keir/src/eigen-3.0.5 ndk-build -j
#
# It is also possible to specify CERES_EXTRA_DEFINES, in case you need
# to pass more definitions to the C compiler.
#
# Using the library
# -----------------
# Copy the static library:
#
# ../obj/local/armeabi-v7a/libceres.a
#
# into your own project, then link it into your binary in your
# Android.mk file.
#
# Reducing binary size
# --------------------
# This build includes the Schur specializations, which increase the
# size of the binary. If you don't need them for your application,
# consider adding:
#
# -DCERES_RESTRICT_SCHUR_SPECIALIZATION
#
# to the LOCAL_CFLAGS variable below.
#
# Changing the logging library
# ----------------------------
# Ceres Solver ships with a replacement for glog that provides a
# simple and small implementation that builds on Android. However, if
# you wish to supply a header only version yourself, then you may
# define CERES_GLOG_DIR to point to it.
LOCAL_PATH := $(call my-dir)
# MY_DD := $(shell $(LOCAL_PATH)/assert_ndk_version.sh)
#
# MY_VAR := $(shell $(LOCAL_PATH)/assert_ndk_version.sh "r9d" $(NDK_ROOT))
#
# # Ceres requires at least NDK version r9d to compile.
# ifneq ($(MY_VAR), true)
# ##$(error Ceres requires NDK version r9d or greater dd=$(MY_DD))
# endif
# Ceres requires Eigen to build.
ifndef EIGEN_PATH
$(error Ceres requires Eigen; please invoke via EIGEN_PATH=... ndk-build)
endif
EIGEN_PATH := $(EIGEN_PATH)
SUITESPARSE_PATH := /usr/local/Cellar/suite-sparse/5.3.0_1/include
CERES_INCLUDE_PATHS := $(CERES_EXTRA_INCLUDES)
CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../internal
CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../internal/ceres
CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../include
CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../config
# Use the alternate glog implementation if provided by the user.
ifdef CERES_GLOG_DIR
CERES_INCLUDE_PATHS += $(CERES_GLOG_DIR)
else
CERES_INCLUDE_PATHS += $(LOCAL_PATH)/../internal/ceres/miniglog
endif
CERES_SRC_PATH := ../internal/ceres
include $(CLEAR_VARS)
LOCAL_CFLAGS := -DCERES_SUITESPARSE_VERSION=\"5.3.0\"
LOCAL_C_INCLUDES := $(CERES_INCLUDE_PATHS)
LOCAL_C_INCLUDES += $(EIGEN_PATH)
LOCAL_C_INCLUDES += $(SUITESPARSE_PATH)
LOCAL_CPP_EXTENSION := .cc
LOCAL_CFLAGS += $(CERES_EXTRA_DEFINES) \
-DCERES_NO_LAPACK \
-DCERES_USE_SUITESPARSE \
-DCERES_NO_CXSPARSE \
-DCERES_HAVE_PTHREAD \
-DCERES_STD_UNORDERED_MAP
# If the user did not enable threads in CERES_EXTRA_DEFINES, then add
# CERES_NO_THREADS.
#
# TODO(sameeragarwal): Update comments here and in the docs to
# demonstrate how OpenMP can be used by the user.
ifeq (,$(findstring CERES_HAVE_PTHREAD, $(LOCAL_CFLAGS)))
LOCAL_CFLAGS += -DCERES_NO_THREADS
endif
# LOCAL_STATIC_LIBRARIES := static_libamd static_libcamd static_libbtf static_libcholmod static_libcsparse
# static_libcxsparse static_libgraphblas static_libspqr static_libsuitesparseconfig static_libumfpack
LOCAL_SRC_FILES := $(CERES_SRC_PATH)/single_linkage_clustering.cc \
$(CERES_SRC_PATH)/compressed_col_sparse_matrix_utils.cc \
$(CERES_SRC_PATH)/solver.cc \
$(CERES_SRC_PATH)/sparse_matrix.cc \
$(CERES_SRC_PATH)/sparse_normal_cholesky_solver.cc \
$(CERES_SRC_PATH)/split.cc \
$(CERES_SRC_PATH)/stringprintf.cc \
$(CERES_SRC_PATH)/suitesparse.cc \
$(CERES_SRC_PATH)/triplet_sparse_matrix.cc \
$(CERES_SRC_PATH)/trust_region_minimizer.cc \
$(CERES_SRC_PATH)/trust_region_preprocessor.cc \
$(CERES_SRC_PATH)/trust_region_step_evaluator.cc \
$(CERES_SRC_PATH)/trust_region_strategy.cc \
$(CERES_SRC_PATH)/types.cc \
$(CERES_SRC_PATH)/visibility_based_preconditioner.cc \
$(CERES_SRC_PATH)/visibility.cc \
$(CERES_SRC_PATH)/wall_time.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_d_d_d.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_2_2.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_2_3.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_2_4.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_2_d.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_3_3.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_3_4.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_3_6.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_3_9.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_3_d.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_4_3.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_4_4.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_4_8.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_4_9.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_4_d.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_2_d_d.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_4_4_2.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_4_4_3.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_4_4_4.cc \
$(CERES_SRC_PATH)/generated/schur_eliminator_4_4_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_d_d_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_2_2.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_2_3.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_2_4.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_2_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_3_3.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_3_4.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_3_6.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_3_9.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_3_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_4_3.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_4_4.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_4_8.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_4_9.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_4_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_2_d_d.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_4_4_2.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_4_4_3.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_4_4_4.cc \
$(CERES_SRC_PATH)/generated/partitioned_matrix_view_4_4_d.cc \
$(CERES_SRC_PATH)/solver_utils.cc \
$(CERES_SRC_PATH)/array_utils.cc \
$(CERES_SRC_PATH)/blas.cc \
$(CERES_SRC_PATH)/block_evaluate_preparer.cc \
$(CERES_SRC_PATH)/block_jacobian_writer.cc \
$(CERES_SRC_PATH)/block_jacobi_preconditioner.cc \
$(CERES_SRC_PATH)/block_random_access_dense_matrix.cc \
$(CERES_SRC_PATH)/block_random_access_diagonal_matrix.cc \
$(CERES_SRC_PATH)/block_random_access_matrix.cc \
$(CERES_SRC_PATH)/block_random_access_sparse_matrix.cc \
$(CERES_SRC_PATH)/block_sparse_matrix.cc \
$(CERES_SRC_PATH)/block_structure.cc \
$(CERES_SRC_PATH)/callbacks.cc \
$(CERES_SRC_PATH)/canonical_views_clustering.cc \
$(CERES_SRC_PATH)/cgnr_solver.cc \
$(CERES_SRC_PATH)/compressed_row_jacobian_writer.cc \
$(CERES_SRC_PATH)/compressed_row_sparse_matrix.cc \
$(CERES_SRC_PATH)/conditioned_cost_function.cc \
$(CERES_SRC_PATH)/conjugate_gradients_solver.cc \
$(CERES_SRC_PATH)/coordinate_descent_minimizer.cc \
$(CERES_SRC_PATH)/corrector.cc \
$(CERES_SRC_PATH)/covariance.cc \
$(CERES_SRC_PATH)/covariance_impl.cc \
$(CERES_SRC_PATH)/dense_normal_cholesky_solver.cc \
$(CERES_SRC_PATH)/dense_qr_solver.cc \
$(CERES_SRC_PATH)/dense_sparse_matrix.cc \
$(CERES_SRC_PATH)/detect_structure.cc \
$(CERES_SRC_PATH)/dogleg_strategy.cc \
$(CERES_SRC_PATH)/dynamic_compressed_row_jacobian_writer.cc \
$(CERES_SRC_PATH)/dynamic_compressed_row_sparse_matrix.cc \
$(CERES_SRC_PATH)/evaluator.cc \
$(CERES_SRC_PATH)/file.cc \
$(CERES_SRC_PATH)/gradient_checker.cc \
$(CERES_SRC_PATH)/gradient_checking_cost_function.cc \
$(CERES_SRC_PATH)/gradient_problem.cc \
$(CERES_SRC_PATH)/gradient_problem_solver.cc \
$(CERES_SRC_PATH)/is_close.cc \
$(CERES_SRC_PATH)/implicit_schur_complement.cc \
$(CERES_SRC_PATH)/iterative_schur_complement_solver.cc \
$(CERES_SRC_PATH)/lapack.cc \
$(CERES_SRC_PATH)/levenberg_marquardt_strategy.cc \
$(CERES_SRC_PATH)/line_search.cc \
$(CERES_SRC_PATH)/line_search_direction.cc \
$(CERES_SRC_PATH)/line_search_minimizer.cc \
$(CERES_SRC_PATH)/linear_least_squares_problems.cc \
$(CERES_SRC_PATH)/linear_operator.cc \
$(CERES_SRC_PATH)/line_search_preprocessor.cc \
$(CERES_SRC_PATH)/linear_solver.cc \
$(CERES_SRC_PATH)/local_parameterization.cc \
$(CERES_SRC_PATH)/loss_function.cc \
$(CERES_SRC_PATH)/low_rank_inverse_hessian.cc \
$(CERES_SRC_PATH)/minimizer.cc \
$(CERES_SRC_PATH)/normal_prior.cc \
$(CERES_SRC_PATH)/parameter_block_ordering.cc \
$(CERES_SRC_PATH)/partitioned_matrix_view.cc \
$(CERES_SRC_PATH)/polynomial.cc \
$(CERES_SRC_PATH)/preconditioner.cc \
$(CERES_SRC_PATH)/preprocessor.cc \
$(CERES_SRC_PATH)/problem.cc \
$(CERES_SRC_PATH)/problem_impl.cc \
$(CERES_SRC_PATH)/program.cc \
$(CERES_SRC_PATH)/reorder_program.cc \
$(CERES_SRC_PATH)/residual_block.cc \
$(CERES_SRC_PATH)/residual_block_utils.cc \
$(CERES_SRC_PATH)/schur_complement_solver.cc \
$(CERES_SRC_PATH)/schur_eliminator.cc \
$(CERES_SRC_PATH)/schur_jacobi_preconditioner.cc \
$(CERES_SRC_PATH)/scratch_evaluate_preparer.cc
ifndef CERES_GLOG_DIR
LOCAL_SRC_FILES += $(CERES_SRC_PATH)/miniglog/glog/logging.cc
endif
LOCAL_MODULE := ceres
include $(BUILD_STATIC_LIBRARY)
2.将jni目录下的Application.mk替换成如下内容
# Ceres Solver - A fast non-linear least squares minimizer
# Copyright 2015 Google Inc. All rights reserved.
# http://ceres-solver.org/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
APP_BUILD_SCRIPT := $(call my-dir)/Android.mk
APP_PROJECT_PATH := $(call my-dir)
APP_CPPFLAGS := -frtti -fexceptions -std=c++0x
APP_OPTIM := release
# Use libc++ from LLVM. It is a modern BSD licensed implementation of
# the standard C++ library.
APP_STL := gnustl_static
APP_ABI := armeabi-v7a
3.打开终端进入到your/path/to/ceres-solver-1.12.0/jni
路径下
输入EIGEN_PATH=/your/path/to/eigen /your/path/to/ndk-build
不知道ndk-build位置,可以在终端输入which ndk-build
,这里以我的Eigen和NDK路径为例,EIGEN_PATH=/Users/subowen/WorkDir/SDKDir/eigen /Users/subowen/WorkDir/SDKDir/NDKDir/android-ndk-r16b/ndk-build
如果一切顺利的话会出现上面的编译各个文件的log。同时在/your/path/to/ceres-solver-1.12.0/obj/local/armeabi-v7a/
目录下会生成编译成功的libceres.a静态库。