本文方法只需要VS、Ceres1.12.0源码、eigen源码、glog0.3.5源码。
根据本文方法编译好的x64debug和release文件:
链接:https://pan.baidu.com/s/1ieJrFfgLohkDG_FHdYJF2w
提取码:6nx3
写在前面的话:
win10需要单独下载并使用cmake编译依赖库(这时候就体现了Linux的好处)
win10编译Ceres网上一大堆各种各样的教程,过程杂乱不一,还容易出各种问题,因此首选官方教程。
打开官网意外的发现一句话:If you find the following CMake difficult to set up, then you may be interested in a Microsoft Visual Studio wrapper for Ceres Solver by Tal Ben-Nun.
然后发现一个超简单的方法,看这里,工程文件已经建立好,直接利用VS编译就好。废话说完,说一下具体操作。
编译正文:
1下载上面的项目ceres-windows-master,解压后如下图:
2官网下载eigen库(不限版本,我用的3.4)、GitHub下载glog0.3.5(一定要选0.3.5)、GitHub下载Ceres1.12.0版本(不要下载最新版)
3把你上述下载的源码解压,分别放在项目ceres-windows-master中的ceres-solver、Eigen、glog文件夹中
4用VS2015及以上版本打开ceres-2015.sln文件,我这里选择debug,x64,你可以根据自己的需要选择release或者debug,x64或者win32都可以
5点击ceres项目,右键,生成。等待完成即完成ceres动态链接库的编译。
在debug文件中即为编译好的动态链接库文件。
使用Ceres:
用vs新建个项目,命名为ceres_test,选择 debug x64
复制官方代码到cpp文件:
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
// A templated cost functor that implements the residual r = 10 -
// x. The method operator() is templated so that we can then use an
// automatic differentiation wrapper around it to generate its
// derivatives.
struct CostFunctor {
template <typename T> bool operator()(const T* const x, T* residual) const {
residual[0] = T(10.0) - x[0];
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
// The variable to solve for with its initial value. It will be
// mutated in place by the solver.
double x = 0.5;
const double initial_x = x;
// Build the problem.
Problem problem;
// Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);
// Run the solver!
Solver::Options options;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
点击工程右键->属性:
在c/c+±->附件包含目录,添加:
D:\c++\ceres-windows-master\Eigen
D:\c++\ceres-windows-master\ceres-solver\include
D:\c++\ceres-windows-master\glog\src\windows
D:\c++\ceres-windows-master\win\include
注意:换成自己的ceres-windows-master的路径(这一步的目的是为了找到头文件)
在预处理器–>预处理定义中添加:
GOOGLE_GLOG_DLL_DECL=
_MBCS
_CRT_NONSTDC_NO_DEPRECATE
_CRT_SECURE_NO_WARNINGS
GLOG_NO_ABBREVIATED_SEVERITIES
链接器–>附加库目录,添加:
D:\c++\ceres-windows-master\x64\Debug
也就是你编译好的debug文件夹(因为我这里选择的是debug模式)
链接器–>输入,添加:
ceres.lib
libglog_static.lib
配置属性–>调试–>工作目录,添加:
D:\c++\ceres-windows-master\x64\Debug
也就是编译好的dll文件路径。(这一步也可以不在工作目录中添加路径,直接把编译好的ceres.dll复制到exe文件所在的文件夹下也可以)
至此属性配置完毕。
生成项目,运行即可,会得到下图结果: