Win10+ uses cmake to compile the latest ceres-solver library

references

Click here to visit
Thanks guys!

illustrate

I only compiled releasethe version, if I need debugthe version, cmake编译and vs编译I need to set some other things, I didn't do it.

1. Libraries to be used

  • ceres-solver-2.1.0
  • own-3.4.0
  • gflags-2.2.2
  • hawthorn-0.4.0

Everyone needs to go to the corresponding githubwebsite to download.

2. Required tools

  • visual studio 2022 community
  • cmake 3.26.3

Everyone needs to download and install by themselves.

3. Preparations

3.1 Create a folder for all decompressed source code

For example:
insert image description here

3.2 Create a folder for all build buildsb u i l d file

For example all_build, 4 folders are built inside to store buildthe files of 4 libraries
insert image description here
insert image description here

3.3 Create a folder for all install installin s t a ll file

Similar to build all_build, build a all_install:

insert image description here
insert image description here

4. Open cmake cmakec make e to start compiling.

4.1 each othere i g e n

4.1.1 c m a k e cmake c make compile _

insert image description here
Click config→ \rightarrow Select the corresponding platform→ \rightarrowfinish . It will be red.

  1. Change to the subfolder corresponding to CMAKE_INSTALL_PREFIXthe previously created all_installfoldereigen
    insert image description here

config, there should be no error reported at this time, → \rightarrowgenerate → \rightarrow open project , open the vs project.

4.1.2 v s vs vs compile _

insert image description here

Click Build → \rightarrow Batch generation

insert image description here
insert image description here
Check ALL_BUILDthe releaseversion and version INSTALLof release, click Generate , if there is no error, the description is correct.

Result: some files will be generated under the folder eigen:install
insert image description here

4.2 g f l a g s gflags gflags

4.2.1 c m a k e cmake c make compile _

insert image description here
Click config→ \rightarrow Select the corresponding platform→ \rightarrowfinish . It will be red.

  1. tickBUILD_SHARED_LIBS
  2. will CMAKE_INSTALL_PREFIXbe changed to the gflagscorresponding installfolder.
    insert image description here
    config, there should be no error reported at this time, → \rightarrowgenerate → \rightarrow open project , open the vs project.

4.2.2 v s vs vs compile _

The steps are the same as 4.1.2 4.1.2Same as 4.1.2 .
If no error is reported, some files will be generated ingflagscorrespondinginstallfolder as a result:
insert image description here

4.3 glow glowg l o g

4.3.1 c m a k e cmake c make compile _

insert image description here
Click config→ \rightarrow Select the corresponding platform→ \rightarrowfinish . It will be red.

  1. tickBUILD_SHARED_LIBS
  2. will CMAKE_INSTALL_PREFIXbe changed to the glogcorresponding installpath
  3. Set it gflags_DIRto the gflagscorresponding buildpath
    insert image description here
    config, there should be no error at this time, → \rightarrowgenerate → \rightarrow open project , open the vs project.

4.3.2 v s vs vs compile _

The steps are the same as 4.1.2 4.1.2Same as 4.1.2 .
If no error is reported, some files will be generated inglogcorrespondinginstallfolder as a result:
insert image description here

4.4 Ceres Ceresceres

4.4.1 c m a k e cmake c make compile _

insert image description here

Click config→ \rightarrow Select the corresponding platform→ \rightarrowfinish . It will be red.

  1. tickBUILD_SHARED_LIBS
  2. will CMAKE_INSTALL_PREFIXbe changed to the cerescorresponding installpath
  3. The path that will Eigen3_DIRbe set toeigenbuild
  4. The path that will gflags_DIRbe set togflagsbuild
  5. Set CMAKE_BUILD_TYPEit to Release
    insert image description here
    insert image description here
    insert image description here
    click config, there should be no error at this time, → \rightarrowgenerate → \rightarrow open project , open the vs project.

4.4.2 v s vs vs compile _

The steps are the same as 4.1.2 4.1.2Same as 4.1.2 .
If no error is reported, some files will be generated incerescorrespondinginstallfolder as a result:
insert image description here

5. Dynamic library finishing

Create a new folder under the folder, and then copy all_installall the files under the folder , and inside the folder to the folderdllsall_installceres/bingflags/binglog/bin.dlldlls
insert image description here
insert image description here

6. Test

6.1 Code

Let's write a piece of code to test it. Create a new project and copy and paste the code below.
This is the solution to xxThe value of x is such that 1 2 ( 10 − x ) 2 \frac{1}{2}(10−x)^221(10x)2 minimum

#include<iostream>
#include<ceres/ceres.h>
using namespace std;

//第一部分:构建代价函数
struct CostFunctor {
    
    
    //模板函数,泛化函数类型
    template <typename T>
    //重载符号(),仿函数;传入待优化变量列表和承接残差的变量列表
    bool operator()(const T* const x, T* residual) const 
    {
    
    
        //残差计算步骤
        residual[0] = T(0.5) * (T(10.0) - x[0]) * (T(10.0) - x[0]);//1/2(10−x)^2
        return true;
    }
};

//主函数
int main(int argc, char** argv)
{
    
    
    // 寻优参数x的初始值,为5
    double initial_x = 5.0;
    double x = initial_x;

    // 第二部分:构建寻优问题
    //实例化Problem
    ceres::Problem problem;
    //代价函数赋值
    //使用自动求导,将之前的代价函数结构体传入,第一个1是输出维度,即残差的维度,第二个1是输入维度,即待寻优参数x的维度。
//    ceres::CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
    //添加误差项,1、上一步实例化后的代价函数2、核函数3、待优化变量
    problem.AddResidualBlock(new ceres::AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor), nullptr, &x);

    //第三部分: 配置并运行求解器
    ceres::Solver::Options options;
    //配置增量方程的解法,此处为QR求解
    options.linear_solver_type = ceres::DENSE_QR;
    //是否输出到cout
    options.minimizer_progress_to_stdout = true;
    //优化信息
    ceres::Solver::Summary summary;
    //求解:1、求解器2、实例化problem 3、优化器
    Solve(options, &problem, &summary);
    //输出优化的简要信息,迭代次数和每次的cost
    std::cout << summary.BriefReport() << "\n";
    //最终结果
    std::cout << "初始值x : " << initial_x << " 迭代到-> " << x << "\n";
    return 0;
}

Obviously, many errors will be reported, because the environment has not been set.

6.2 Change the mode to release releaserelease

because the compiled releasepattern
insert image description here

6.3 Add attribute table, which can be used in the future

insert image description here

Property Manager → \rightarrowrelease → \rightarrow Right-click, add a new project property sheet, rename itceres.props, click添加, double-click this property sheet, open it for setting.

insert image description here

6.4 Setting up the property sheet

6.4.1 c / c + + → c/c++\rightarrow c/c++ General→ \rightarrow Additional include directories

Add all the , , all_installunder the folder . Correspondingly , add its source code directory. That is, where the 4 source codes were placed at the beginning, find them . Last click .ceres/includegflags/includeglog/includeeigeneigen
应用

insert image description here

6.4.2 Linker → \rightarrow General→ \rightarrow Additional library directories

Add all the , , all_installunder the folder .ceres/libgflags/libglog/lib

insert image description here

6.4.3 Linker → \rightarrow Input→ \rightarrow Additional dependencies

Add all the files all_installunder ceres/lib, gflags/lib, and glog/libunder the folder..lib

insert image description here

6.4.4 Generating events → \rightarrow Post-build events→ \rightarrow command line

Copy the command line below xcopy /y /d /s /e "D:\libs\ceres\all_install\dlls" "$(OutDir)", but replace the paths with your own dlls. This is 动态库整理the path created in that step.

insert image description here
OK, save the attribute table.

6.5 Compile and run

  1. You may encounter some visual studiosecurity errors, which can be resolved by searching the Internet. As long as it is not an error reported by the library.
  2. Result:
    We expected 10, which is obviously true.
    insert image description here

7. Conclusion

You're done!

Guess you like

Origin blog.csdn.net/sdhdsf132452/article/details/130646870
Recommended