一、工作准备
注:下载后的包如下图所示,解压缩所有包至合适位置,本文以“D:\Program Files (x86)\Ceres-Solver”为例
二、编译Ceres以及第三方依赖库
1.编译gflags
1)在合适位置新建gflags_build文件夹,打开Cmake,指定gflags库的源文件地址以及构建目录,点击Config,根据自己情况选择编辑器(VS 2017—Visual Studio 15 2017 x64),如图
再次点击config,没有红框,即配置正确,然后点击generate,生成解决方案
2)打开在gflags_build文件夹下生成的VS工程文件,分别在Release和Debug下,右键ALL_BUILD生成解决方案(每种模式下应各成功生成4个)
2.编译glog(参考以上内容)
1)在合适位置新建glog_build文件夹,打开Cmake,指定glog库的源文件地址以及构建目录,点击Config,根据自己情况选择编辑器,再次点击config,没有红框,即配置正确,然后点击generate,生成解决方案
2)打开在glog_build文件夹下生成的VS工程文件,分别在Release和Debug下,右键ALL_BUILD生成解决方案(每种模式下应各成功生成7个)
3.编译Ceres-solver(参考以上内容)
1)在合适位置新建Ceres_build文件夹,打开Cmake,指定Ceres库的源文件地址以及构建目录,点击Config,根据自己情况选择编辑器,点击确定
2)提示出现错误,需要进行如下更改:勾选EIGENSPARSE,填写下载并解压的Eigen库地址
3)点击Config,填写之前构件gflags和glog的目录,同时指定CMAKE_BUILD_TYPE为Release;Debug
4)点击Config,没有错误,点击Generate生成VS解决方案
5)打开在glog_build文件夹下生成的VS工程文件,分别在Release和Debug下,右键ALL_BUILD生成解决方案(每种模式下应各成功生成13个)
三、配置VS 2017
文件准备
1.新建Ceres文件夹,在其下新建include和lib文件夹,如下图
2. include文件夹下拷入:
1) gflags-build\include\下的gflags文件夹;
2) glog-build\下的glog文件夹;
3) ceres-solver-1.13.0\include\下的ceres文件夹;
4) ceres-build\config\ceres\internal文件下的config.h拷贝到上述include\ceres里的internal文件夹中;
5) eigen-3.3.9\下的Eigen文件夹;
6)glog-build\src\windows下的glog文件夹中log_severity.h复制到…\Include\glog文件夹下;
注:gflags-build、glog-build是编译后的目录,ceres-solver-1.13.0、eigen-3.3.9是解压缩目录
3.lib文件夹下拷入:
1)gflags-build\lib\Debug下的gflags_nothreads_static.lib和gflags_static.lib;
2)glog-build\Debug下的glog.lib;
3)ceres-build\lib\Debug下的ceres-debug.lib;
VS 配置
-
新建VS工程-空项目,打开项目属性(或新建项目属性表)
-
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;
}
出现如下结果说明配置成功
总结
以上就是Win10 VS2017 Ceres-Solver 1.13.0配置说明
欢迎批评指正!
他们说 青山是否妩媚 还须看青山是谁. 可我摇头 我见众生皆草木 唯你是青山 .HDarker