在MSVC C DLL中导出模板类的模板成员函数

Exporting templated member function of a templated class in MSVC C++ DLL

本文关键字:成员 函数 MSVC DLL      更新时间:2023-10-16

我正在编译PCL库(https://github.com/pointCloudlibrary/pcl.git)中,以在我自己的项目中使用。这是一个大量使用C 模板的项目。为了减少编译时间,我将PCL_NO_PRECOMPILE设置为OFF,这意味着该实现不在标题文件中。我的应用程序只能使用编译PCL时已实例化的类和功能。

问题是,在MSVC DLL中未导出模板类的模板成员函数。我正在使用MS Visual Studio 2017社区版。这是特定的问题。

我正在创建pcl::Hough3DGrouping的子类,该类别在https://github.com/pointcloudlibrary/pcl/pcl/master/master/recognition/include/include/pcl/recognition/cg/hough_3d.h。感兴趣的具体功能是在第510行文件底部定义的受保护函数computeRf()。当我查看DLL中的导出符号时,我看不到computeRf()。因此,我无法从我的自定义代码中使用它。

我以为computeRf()没有在DLL中导出,因为它已模板。因此,我尝试在https://github.com/pointcloudlibrary/pcl/blob/master/master/recognition/src/src/cg/hough_3d.cpp中尝试了显式实例化。特别是我添加了

template void pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >, pcl::PointCloud<pcl::ReferenceFrame>)

在https://github.com/pointCloudlibrary/pcl/blob/master/master/Recognition/src/cg/cg/hough_3d.cpp中的第44行上

编译时,我会收到错误:

E:libspclsrcrecognitionsrccghough_3d.cpp(45): error C3190: 'void pcl::Hough3DGrouping<pcl::PointXYZRGB,pcl::PointXYZRGB,pcl::ReferenceFrame,pcl::ReferenceFrame>::computeRf(const boost::shared_ptr<const pcl::PointCloud<PointT>>,pcl::PointCloud<PointModelRfT>)' with the provided template arguments is not the explicit instantiation of any member function of 'pcl::Hough3DGrouping<pcl::PointXYZRGB,pcl::PointXYZRGB,pcl::ReferenceFrame,pcl::ReferenceFrame>'
    with
    [
        PointT=pcl::PointXYZRGB,
        PointModelRfT=pcl::ReferenceFrame
    ]

我的目标是在我的应用程序中创建pcl::Hough3DGrouping的子类,并从中调用基类的computeRf()。我唯一需要的实例是:

pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>

我应该如何使用最小的更改对PCL源代码?

模板不能从dll中导出。

复制粘贴您尝试导出的内容和签名:

template<typename PointType, typename PointRfType> void
computeRf
(const boost::shared_ptr<const pcl::PointCloud<PointType> > &input, pcl::PointCloud<PointRfType> &rf)
template void
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::
computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>
(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >, pcl::PointCloud<pcl::ReferenceFrame>)

我注意到有所不同。您试图导出一些函数参数不是参考的内容。模板类的模板方法具有参考函数参数。

编译器说"这些不匹配"。我会相信的。

进行参数参考,它将与类中的方法匹配。

template void
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::
computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>
(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >&, pcl::PointCloud<pcl::ReferenceFrame>&)

您在Instantiation系列中有两个错误:

  1. hough3dgrouping模板参数顺序是错误的,应为

    pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::PointXYZRGB, pcl::ReferenceFrame
    

    而不是

    pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame
    
  2. 您在参数列表中忘记了& s:

    computeRf(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB>>&, 
              pcl::PointCloud<pcl::ReferenceFrame>&)
    

    不需要computeRf的模板参数,因为扣除有效。