1.采用C++20.
vcpkg中设置:
2.增加预处理宏:
GLOG_USE_GLOG_EXPORT
3.屏蔽sdl错误
在 项目-属性-C/C++ -命令行中添加
/sdl /w34996
#include "ceres/ceres.h"
//#include <iostream>
//#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 = x - 10.
struct CostFunctor {template <typename T> bool operator()(const T* const x, T* residual) const {residual[0] = 10.0 - x[0];return true;}
};int main()
{// The variable to solve for with its initial valuedouble x = 0.5;const double initial_x = x;// Build the problem.Problem problem;problem.AddResidualBlock(new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor), nullptr, &x);// The options structure, which controls how the solver operatesSolver::Options options;options.linear_solver_type = ceres::DENSE_QR;options.minimizer_progress_to_stdout = true;// Run solverSolver::Summary summary;Solve(options, &problem, &summary);std::cout << summary.BriefReport() << "\n";std::cout << "x : " << initial_x << " -> " << x << "\n"<<std::endl;
}
运行结果: