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

时间:2017-09-09 01:14:45

标签: c++ templates dll

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

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

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

我假设computeRf()没有在DLL中导出,因为它是模板化的。因此,我尝试在https://github.com/PointCloudLibrary/pcl/blob/master/recognition/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/recognition/src/cg/hough_3d.cpp的第44行。

编译时,我收到错误:

E:\libs\pcl\src\recognition\src\cg\hough_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源代码进行最少的更改,我该如何进行此操作?

2 个答案:

答案 0 :(得分:1)

显然,无法从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>&)

答案 1 :(得分:0)

在instantiatrion行中有两个错误:

  1. Hough3DGrouping模板参数顺序错误,应该是

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

    而不是

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

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

    不需要computeRf的模板参数,因为演绎有效。

相关问题