Ceres库的安装
一、添加源
sudo gedit /etc/apt/sources.list
将下面的源粘贴到source.list的最上方 ,保存退出
deb http://cz.archive.ubuntu.com/ubuntu trusty main universe
更新
sudo apt-get update
二、安装依赖库
sudo apt-get install liblapack-dev libsuitesparse-dev libcxsparse3.1.2 libgflags-dev
sudo apt-get install libgoogle-glog-dev libgtest-dev
三、下载Ceres1.14版本并解压
wget ceres-solver.org/ceres-solver-1.14.0.tar.gz
tar -zxvf ceres-solver-1.14.0.tar.gz
四、编译安装
cd ceres-solver-1.14.0
mkdir build
cd build
cmake ..
make -j4
sudo make install
测试Ceres库是否安装成功
编写CMakeLists.txt
cmake_minimum_required(VERSION 3.8.0)
project(ceres_example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Ceres REQUIRED)
include_directories(
${CERES_INCLUDE_DIRS}
)
add_executable(ceres_example
ceres_example.cpp)
target_link_libraries(ceres_example
${CERES_LIBRARIES}
)
ceres官网一个例子
#include <ceres/ceres.h>
class CostFunctor {
public:
template <typename T>
bool operator()(const T* const x, T* residual) const
{
residual[0] = 10.0 - x[0];
return true;
}
};
int main(int argc, char const* argv[])
{
double initial_x = 5.0;
double x = initial_x;
// Build the problem.
ceres::Problem problem;
// Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
ceres::CostFunction* cost_function = new ceres::AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, &x);
// Run the solver!
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
终端输入
mkdir build
cd build
cmake ..
make
build中生成可执行文件
终端输入
./ceres_example
输出如下:
则ceres库安装成功。
Ceres库的卸载
Ceres只有一个库文件在"/usr/local/lib/“中,并且所有的头文件都在”/usr/local/include/ceres/"中,所以卸载Ceres库只需要将这两个文件删除即可。
sudo rm -r /usr/local/lib/cmake/Ceres
sudo rm -rf /usr/local/include/ceres /usr/local/lib/libceres.a