准备工作:
eigen、gflags-2.2.2、glog-0.3.5、Ceres solver、cmake
遇到的问题:
编译选择X64 debug和release两个版本,注意,需要分别对gflags、glog、ceres编译出它们所对应的debug和release版本,否则可能会报错:
错误原因是生成的Ceres生成的config.h文件出错。
开始编译
编译gflas
打开cmake,在source code中选择准备的gflags,如下:
我最终需要的环境是64位,故在编译时选择Visual Studio 15 2017 Win64 。
配置完成后点击generate,在vs2017中打开,分辨在release和debug两种模式下生成解决方案。两种模式下分别成功生成4个。
编译glog
指定先前生成的gflags的位置,如下:
配置完成后点击generate,在vs2017中打开,分辨在release和debug两种模式下生成解决方案。两种模式下分别成功生成7个。
编译Ceres-solver
同理,在source code中选择刚才下载的ceres-solver,并指定编译位置,选择编译器。 点击config,这时候需要指定之前所下载的eigen库,并将EIGENSPARSE选上 。
再次点击config,指定之前生成的gflags和glog,如下:
再次点击config,指定CMAKE_BUILD_TYPE,如下:
配置完成后点击generate,在vs2017中打开,分辨在release和debug两种模式下生成解决方案。两种模式下分别成功生成13个。
vs2017使用前提工作
新建文件Ceres,将ceres、gflags、glog、eigen的源码解压后放入文件中,并在文件中新建include和lib文件,如下:
将一下目录拷贝到include文件中:
1、gflagsbuild\include\下的gflags文件夹。
2、glogbuild\下的glog文件夹。
3、ceres\include\下的ceres文件夹。
4、ceres-build\config\ceres\internal文件下的config.h拷贝到ceres里的internal文件夹中
5、eigen-eigen-c1659a20ca60\下的Eigen文件夹。
注:gflagsbuild、glogbuild是编译后的目录,另外两个是解压缩后的目录。
如下:
将以下内容拷贝到lib目录中:
1、gflagsbuild\lib\Debug下的gflags_nothreads_static.lib和gflags_static.lib。
2、glogbuild\Debug下的glog.lib。
3、ceresbuild\lib\Debug下的ceres-debug.lib。
如下:
然后新建vs控制台工程,打开项目-项目属性-VC/VC++目录,将上面的include和lib路径添加到包含目录和库目录中。
打开项目-项目属性-C/C+±常规,将上面的include路径添加到附加包含目录中。
打开项目-项目属性-连接器-输入,编辑附加依赖项,将之前lib目录下的链接库名添加到里面,最后点击确定,应用,即可。
示例代码如下:
#include <ceres\ceres.h>
#include <glog\logging.h>
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
//using ceres::Solve;
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.
double initial_x = 5.0;
double x = initial_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.linear_solver_type = ceres::DENSE_QR;
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;
}
运行结果如下: