自学内容网 自学内容网

vs2019从一个含main函数的cpp文件到生成动态生成库

小白,只会写简单的cpp文件,算法写完之后需要项目工程化,和上位机开发人员完成交接,记录一下。


一、VS创建空项目

在这里插入图片描述
在这里插入图片描述
点击下一步,
我这里创建的项目名称为LidarCoreDetection
位置D:\VisualStudio\vsProject
解决方案名称:LidarCoreDetection
在这里插入图片描述
在这里插入图片描述
创建源文件名为LidarCoreDetection.cpp
同理,在头文件中创建LidarCoreDetection.h文件
建议.cpp和.h文件同名

二、编写代码

在创建的LidarCoreDetection.h中编写代码

#ifndef LIDARCOREDETECTION_H
#define LIDARCOREDETECTION_H

//定义导出的函数,这里是最重要的,名字自己取
#ifdef LIDARDLL_EXPORTS
#define LIDARDLL_API __declspec(dllexport)
#else
#define LIDARDLL_API __declspec(dllimport)
#endif

#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <vector>
#include <Eigen/Dense>

// 导出函数声明
LIDARDLL_API void exportFunction();

//项目中的声明函数放这里
bool isCoilUnwinding(const pcl::PointCloud<pcl::PointXYZ>::Ptr& inputPointCloud);

bool isNearLine(const pcl::PointXYZ& point, double slope, double intercept, double threshold);

void removeSteelAxisPoints(pcl::PointCloud<pcl::PointXYZ>::Ptr& inputPointCloud, pcl::PointCloud<pcl::PointXYZ>::Ptr& outputPointCloud, double slope, double intercept, double threshold);

void filterCloudByCondition(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr& conditionCloud, double GTY, double LTY, double GTX, double LTX);

void filterCloudByRadius(pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr& radiusCloud, double searchRadius, int minNeighbors);

double distanceToPointLine(const pcl::PointXYZ& point, double a, double b);

pcl::PointXYZ findIntersection(float m1, float b1, float m2, float b2);

Eigen::VectorXd fitPolynomial(const std::vector<pcl::PointXYZ>& points, int degree);

#endif // LIDARCOREDETECTION_H

在创建的LidarCoreDetection.cpp中编写代码,注意必须引用LidarCoreDetection.h

#include "LidarCoreDetection.h"

int main() {
exportFunction(); // 调用导出的函数
return 0;
}

编写函数的cpp:源文件创建.cpp文件,名称为函数名称
在这里插入图片描述
这里创建isNearLine.cpp

#include "LidarCoreDetection.h"

// 定义一个函数来检查点是否靠近给定的直线
bool isNearLine(const pcl::PointXYZ& point, double slope, double intercept, double threshold) {
    // 计算点到直线的距离
    double distance = std::abs(point.y - (slope * point.x + intercept)) / std::sqrt(slope * slope + 1);
    return distance <= threshold;
}

同理,创建其他所有函数的cpp文件
在这里插入图片描述
至此代码部分完成,剩下就是配置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
预处理器需要添加:

LIDARDLL_EXPORTS

这个就是你刚刚定义的导出函数,添加了宏定义

然后
在这里插入图片描述
生成如下文件
在这里插入图片描述



原文地址:https://blog.csdn.net/weixin_43798721/article/details/142615782

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!