2017-09-15 23 views
-2

我正在使用PCL開發ROS包。在C++中運行後的模板化類錯誤11

問題在於它使用C++ 98和C++ 11編譯成功。但是,當節點在支持C++ 11的版本(CMakeLists L:5,未註釋)中啓動時,它立即發生段錯誤,儘管在沒有使用C++ 11的情況下編譯時它工作正常。

this包的相同代碼體系結構(模板化庫)顯示相同的行爲。

我發現模板庫是保持原因的人。 但是,我還沒弄明白。我在這裏錯過了什麼?

平臺:Ubuntu 14.04,ROS Indigo,GCC 4.8.4。

謝謝。

節點代碼:

#include <ros/ros.h> 
#include "pcl_tools.h" 
// PCL specific includes 
#include <sensor_msgs/PointCloud2.h> 
#include <pcl_conversions/pcl_conversions.h> 
#include <pcl/point_cloud.h> 
#include <pcl/point_types.h> 
#include <pcl/filters/voxel_grid.h> 
using namespace pcl_tools; 
ros::Publisher pub; 

void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input) 
{ 
    /// ros to pcl cloud 
    pcl::PCLPointCloud2 cloud2_in; 
    pcl_conversions::toPCL(*input,cloud2_in); 
    pcl::PointCloud< pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud< pcl::PointXYZ>); 
    pcl::fromPCLPointCloud2(cloud2_in,*temp_cloud); 

    /// use templated function 
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>); 
    cloud_filtered = passthroughfilter<pcl::PointXYZ>(temp_cloud, "y", -2, 2.0); 
} 

int main (int argc, char** argv) 
{ 
    ros::init (argc, argv, "template_test"); 
    ros::NodeHandle nh; 
    ros::Subscriber sub = nh.subscribe ("/camera/depth/points", 1, cloud_cb); 
    ros::spin(); 
} 

模板頭:

#ifndef PCL_TOOLS 
#define PCL_TOOLS 

#include <pcl/point_cloud.h> 
#include <pcl/point_types.h> 
#include <pcl/filters/passthrough.h> 
#include <string> 
#include <sstream> 
#include <assert.h> 

namespace pcl_tools 
{ 
    template <typename pointT> boost::shared_ptr< pcl::PointCloud<pointT> > 
    passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> >, std::string, float, float, bool invert = false); 
} 

template <typename pointT> 
boost::shared_ptr< pcl::PointCloud<pointT> > pcl_tools::passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> > input, 
                   std::string axis, float min , float max, bool invert = false) 
{ 
    typedef boost::shared_ptr< pcl::PointCloud<pointT> > PointCloudPtr; 
    PointCloudPtr cloud(new pcl::PointCloud<pointT>); 

    // Create the filtering object 
    pcl::PassThrough<pointT> pass; 
    pass.setInputCloud (input); 
    pass.setFilterFieldName (axis); 
    pass.setFilterLimits (min, max); 
    pass.setFilterLimitsNegative (invert); 
    pass.filter (*cloud); 
    return cloud; 
} 
#endif 

的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3) 
project(test_template) 

## Compile as C++11, supported in ROS Kinetic and newer 
add_compile_options(-std=c++11) 

find_package(catkin REQUIRED COMPONENTS 
    cv_bridge 
    geometry_msgs 
    image_transport 
    pcl_conversions 
    pcl_ros 
    roscpp 
    rospy 
    sensor_msgs 
    std_msgs 
) 

add_definitions(${PCL_DEFINITIONS}) 
catkin_package() 

include_directories(
    include/${PROJECT_NAME} 
    ${catkin_INCLUDE_DIRS} 
    ${PCL_INCLUDE_DIRS} 
) 

link_directories(${PCL_LIBRARY_DIRS}) 

add_executable(pcl_template_test src/pcl_template.cpp) 
target_link_libraries(pcl_template_test ${catkin_LIBRARIES}) 
+4

你有沒有使用過調試器? – Klaus

+0

嗯,是的。但我遇到了同樣的問題,使用這個[包](https://github.com/spencer-project/spencer_people_tracking/tree/master/detection/rgbd_detectors/pcl_people_detector),sio,我懷疑更多的作爲模板規格差異的C++ 11版本。此外,你有沒有使用ROS,因爲調試不是那麼相關... – Vtik

回答

0

嗯,事實證明這是由PCL圖書館拋出的。實際上,通過apt-get安裝PCL將安裝非C++ 11兼容版本。所以,爲了克服這個問題,我必須從源代碼重新編譯它,並添加C++ 11支持。更多關於這個問題here