win10超简单的方法编译Ceres

本文方法只需要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文件所在的文件夹下也可以)
至此属性配置完毕。
生成项目,运行即可,会得到下图结果:
在这里插入图片描述

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值