c#中调用CERES,win10+vs2017+ceres编译踩坑记录

本文介绍了如何在Windows环境下,使用C++编译Ceres库并封装成DLL,以便于C#程序调用。首先详细讲述了Ceres库在VS2017下的配置步骤,包括解决预处理器错误。接着展示了C++代码,创建了一个DLL,包含一个使用Ceres求解的函数。最后,给出了C#代码示例,演示如何调用这个DLL并获取计算结果。
摘要由CSDN通过智能技术生成

c#中调用CERES

因项目中需要使用Ceres进行求解计算,所以有了本文,主要思路就是用C++编写好Ceres求解的函数,然后封装成DLL,最后由C#执行调用从而得到计算结果。

Ceres编译

参考的文章:
Win10 + VS2017 + Ceres配置
Ceres-Solver库使用(一)–windows下安装配置
Ceres Solver 在Windows下安装配置笔记
参考以上几位大佬的文章,应该可以实现Ceres在windows下的配置,本人的环境为Win10 64bit,VS2017,在配置过程中也遇到了一些问题,踩坑记录如下:
在这里插入图片描述
在这里插入图片描述
在配置管理器中的预处理器中添加如下两项:
_CRT_NONSTDC_NO_DEPRECATE
_CRT_SECURE_NO_WARNINGS


在预处理器中添加
GOOGLE_GLOG_DLL_DECL=

在这里插入图片描述
在这里插入图片描述
以上两个错误是在C++编写dll库中会遇到的,在预处理器中添加
GLOG_NO_ABBREVIATED_SEVERITIES
NOMINMAX
按照参考文章中,在vs2017中新建了一个项目编译并运行成功。有些文章中说用CMake编译后,可以用命令行执行helloworld.exe,但本人按这种方法没有成功,没有找到原因。
在这里插入图片描述

C++编写调用Ceres的DLL

.h文件代码,主要是添加extern “C”定义导出函数

class CERESDLL_API CCeresDll {
public:
	CCeresDll(void);
	// TODO: 在此处添加方法。
};

extern "C" CERESDLL_API int nCeresDll;
extern "C" CERESDLL_API int fnCeresDll(void);
extern "C" CERESDLL_API double testCeresDll(void);

.cpp文件

#include "pch.h"
#include "framework.h"
#include "Ceres_Dll.h"
#include <ceres\ceres.h>
#include <glog\logging.h>

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;

struct CostFunctor {
	template <typename T>
	bool operator()(const T* const x, T* residual) const {
		residual[0] = T(10.0) - x[0];
		return true;
	}
};
// 这是导出变量的一个示例
CERESDLL_API int nCeresDll=0;

// 这是导出函数的一个示例。
CERESDLL_API int fnCeresDll(void)
{
    return 0;
}

CERESDLL_API double testCeresDll(void)
{
	google::InitGoogleLogging(new char(123));
	// 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);
	return x;
}

// 这是已导出类的构造函数。
CCeresDll::CCeresDll()
{
    return;
}

C#调用DLL

新建一个控制台程序,要添加命名空间System.Runtime.InteropServices,然后定义DLL中的函数,代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace CeresDllTest_CSharp
{
    class Program
    {
        [DllImport("CERESDLL.dll")]
        private static extern int fnCeresDll();
        [DllImport("CERESDLL.dll")]
        private static extern double testCeresDll();
        static void Main(string[] args)
        {
            int n = fnCeresDll();
            double value = testCeresDll();
            Console.WriteLine();
        }
    }
}

运行结果如下:

源代码下载链接

VS2017安装Ceres库需要按照以下步骤进行操作: 1. 下载Ceres库:首先,你需要从Ceres库的官方网站(http://ceres-solver.org/)下载最新版本的Ceres库。确保选择与你的操作系统和VS2017版本兼容的库文件。 2. 配置VS项目:打开Visual Studio 2017,创建一个新的空项目或者打开一个已有项目。然后,右键单击你的项目,选择“属性”(Properties)。 3. 配置C++属性:在项目属性对话框,选择“配置属性”(Configuration Properties)-> “C/C++” -> “常规”(General)。在“附加包含目录”(Additional Include Directories)添加Ceres库的安装路径。 4. 配置链接器属性:在“配置属性” -> “链接器” -> “常规”(General),添加Ceres库的.lib文件的路径到“附加库目录”(Additional Library Directories)。 5. 链接Ceres库:在“配置属性” -> “链接器” -> “输入”(Input),添加Ceres库的.lib文件的名称到“附加依赖项”(Additional Dependencies)。 6. 配置运行时包:如果你的项目使用了Ceres库的动态链接库(.dll文件),你还需要将Ceres库的.dll文件复制到你的项目的运行目录下,或者将Ceres库的路径添加到系统的环境变量。 7. 进行编译:保存这些项目属性设置,并且尝试编译你的项目。如果一切正常,编译过程应该顺利完成而不报错。 希望上述步骤能够帮助你成功安装Ceres库并在VS2017使用它。如果遇到任何问题,请查看Ceres库的文档或者提问社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值