references
Click here to visit
Thanks guys!
illustrate
I only compiled release
the version, if I need debug
the version, cmake编译
and vs编译
I need to set some other things, I didn't do it.
Article directory
- references
- illustrate
- 1. Libraries to be used
- 2. Required tools
- 3. Preparations
- 4. Open cmake cmakec make e to start compiling.
- 5. Dynamic library finishing
- 6. Test
-
- 6.1 Code
- 6.2 Change the mode to release releaserelease
- 6.3 Add attribute table, which can be used in the future
- 6.4 Setting up the property sheet
-
- 6.4.1 c / c + + → c/c++\rightarrow c/c++→ General→ \rightarrow→ Additional include directories
- 6.4.2 Linker → \rightarrow→ General→ \rightarrow→ Additional library directories
- 6.4.3 Linker → \rightarrow→ Input→ \rightarrow→ Additional dependencies
- 6.4.4 Generating events → \rightarrow→ Post-build events→ \rightarrow→ command line
- 6.5 Compile and run
- 7. Conclusion
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 github
website 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:
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 build
the files of 4 libraries
3.3 Create a folder for all install installin s t a ll file
Similar to build all_build
, build a all_install
:
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 _
Click config
→ \rightarrow→ Select the corresponding platform→ \rightarrow→finish
. It will be red.
- Change to the subfolder corresponding to
CMAKE_INSTALL_PREFIX
the previously createdall_install
foldereigen
config
, there should be no error reported at this time, → \rightarrow→generate
→ \rightarrow →open project
, open the vs project.
4.1.2 v s vs vs compile _
Click Build → \rightarrow→ Batch generation
Check ALL_BUILD
the release
version and version INSTALL
of release
, click Generate , if there is no error, the description is correct.
Result: some files will be generated under the folder eigen
:install
4.2 g f l a g s gflags gflags
4.2.1 c m a k e cmake c make compile _
Click config
→ \rightarrow→ Select the corresponding platform→ \rightarrow→finish
. It will be red.
- tick
BUILD_SHARED_LIBS
- will
CMAKE_INSTALL_PREFIX
be changed to thegflags
correspondinginstall
folder.
config
, there should be no error reported at this time, → \rightarrow→generate
→ \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 ingflags
correspondinginstall
folder as a result:
4.3 glow glowg l o g
4.3.1 c m a k e cmake c make compile _
Click config
→ \rightarrow→ Select the corresponding platform→ \rightarrow→finish
. It will be red.
- tick
BUILD_SHARED_LIBS
- will
CMAKE_INSTALL_PREFIX
be changed to theglog
correspondinginstall
path - Set it
gflags_DIR
to thegflags
correspondingbuild
path
config
, there should be no error at this time, → \rightarrow→generate
→ \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 inglog
correspondinginstall
folder as a result:
4.4 Ceres Ceresceres
4.4.1 c m a k e cmake c make compile _
Click config
→ \rightarrow→ Select the corresponding platform→ \rightarrow→finish
. It will be red.
- tick
BUILD_SHARED_LIBS
- will
CMAKE_INSTALL_PREFIX
be changed to theceres
correspondinginstall
path - The path that will
Eigen3_DIR
be set toeigen
build
- The path that will
gflags_DIR
be set togflags
build
- Set
CMAKE_BUILD_TYPE
it toRelease
clickconfig
, there should be no error at this time, → \rightarrow→generate
→ \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 inceres
correspondinginstall
folder as a result:
5. Dynamic library finishing
Create a new folder under the folder, and then copy all_install
all the files under the folder , and inside the folder to the folderdlls
all_install
ceres/bin
gflags/bin
glog/bin
.dll
dlls
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(10−x)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 release
pattern
6.3 Add attribute table, which can be used in the future
Property Manager → \rightarrow→release
→ \rightarrow → Right-click, add a new project property sheet, rename itceres.props
, click添加
, double-click this property sheet, open it for setting.
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_install
under 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/include
gflags/include
glog/include
eigen
eigen
应用
6.4.2 Linker → \rightarrow→ General→ \rightarrow→ Additional library directories
Add all the , , all_install
under the folder .ceres/lib
gflags/lib
glog/lib
6.4.3 Linker → \rightarrow→ Input→ \rightarrow→ Additional dependencies
Add all the files all_install
under ceres/lib
, gflags/lib
, and glog/lib
under the folder..lib
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.
OK, save the attribute table.
6.5 Compile and run
- You may encounter some
visual studio
security errors, which can be resolved by searching the Internet. As long as it is not an error reported by the library. - Result:
We expected 10, which is obviously true.
7. Conclusion
You're done!