未检测到带有 ROS Package:Header 文件的 CMake
CMake with ROS Package:Header files not detected
我试图将我的代码组织成更面向对象的风格,每个 class 都有自己的头文件和 cpp 文件。这是我在工作区包文件夹中的树
- CMakeLists.txt
- include
- - Master_Thread.h
- - Entry.h
- - ROS_Topic_Thread.h
- src
- - pcl_ros_init.cpp
- - archive
- - - pcl_ros_not_updated.cpp
- - - pc_ros_old2.cpp
- - - thread.cpp
- - - pcl_ros_old.cpp
- package.xml
我没有把classes的定义分别放在cpp文件里,我暂时把它们都和主代码(pcl_ros_init.cpp)放在一起了。我的 classes 之间有一些轻微的依赖关系,例如 Master_Thread class 在其声明中使用 ROS_Topic_Thread 现在,Cmake 能够找到包含文件,但在像 Master_Thread.h 这样的头文件,无法找到 ROS_Topic_Thread 的 class 签名
pcl_ros_custom/include/Master_Thread.h:38:36: error: ‘ROS_Topic_Thread’ has not been declared
这是我的CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(pcl_ros_custom)
find_package(catkin REQUIRED COMPONENTS
roscpp
pcl_conversions
pcl_ros)
catkin_package(
INCLUDE_DIRS
CATKIN_DEPENDS roscpp
pcl_conversions
pcl_ros
)
include_directories(
include
${catkin_INCLUDE_DIRS}/include/**
)
add_executable(pcl_ros_init src/pcl_ros_init.cpp)
target_link_libraries(pcl_ros_init
${catkin_LIBRARIES} ${roslib_LIBRARIES} ${PCL_LIBRARIES} )
这是 2 个头文件
#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
//include custom classes
#include "ROS_Topic_Thread.h"
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class Master_Thread
{
private:
std::atomic<bool> mKillswitch;
std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1, mMasterbuffer2, mMasterbuffer3, mMasterbuffer4, mMergedPointCloud2Ptr_buffer;
public:
Master_Thread();
void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2, ROS_Topic_Thread&t3, ROS_Topic_Thread&t4 );
void process_buffer();
void setkillswitch();
};
}
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
#include "Master_Thread.h"
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread
{
private:
ros::Subscriber mSub;
// Topic
const std::string mSUB_TOPIC;
//buffer
std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
std::mutex mMutex;
public:
ROS_Topic_Thread()= delete;
ROS_Topic_Thread(ros::NodeHandle* nodehandle, const std::string& SUBTOPIC, ClusteringBenchmarkEntry* entry);
void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
friend class Master_Thread;
};
} //namespace close scope
#endif //for ROS_TOPIC_THREAD_H
我哪里错了?
编辑
This 是一个非常好的 link,它清除了我在 C++ 中的大部分头文件组织,这在答案之上帮助我顺利进行项目构建。希望对阅读本文的人有所帮助。
本质上,您不需要将 header 包含在另一个中。这修复了通告包含。
在 ROS_Topic_Thread.h 中,friend class Master_Thread;
已经向前声明了 Mater_Thread,因此您不需要 Master_Thread.h header。
在 Master_Thread.h 中,您还可以在 Master_Thread class 的声明之前转发声明 class ROS_Topic_Thread;
,因为您只在 transfer_to_master_buffer
中使用对 ROS_Topic_Thread 的引用] 方法。
#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
//include custom classes
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread;
class Master_Thread
{
private:
std::atomic<bool> mKillswitch;
std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1, mMasterbuffer2, mMasterbuffer3, mMasterbuffer4, mMergedPointCloud2Ptr_buffer;
public:
Master_Thread();
void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2, ROS_Topic_Thread&t3, ROS_Topic_Thread&t4 );
void process_buffer();
void setkillswitch();
};
}
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread
{
private:
ros::Subscriber mSub;
// Topic
const std::string mSUB_TOPIC;
//buffer
std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
std::mutex mMutex;
public:
ROS_Topic_Thread()= delete;
ROS_Topic_Thread(ros::NodeHandle* nodehandle, const std::string& SUBTOPIC, ClusteringBenchmarkEntry* entry);
void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
friend class Master_Thread;
};
} //namespace close scope
#endif //for ROS_TOPIC_THREAD_H
我试图将我的代码组织成更面向对象的风格,每个 class 都有自己的头文件和 cpp 文件。这是我在工作区包文件夹中的树
- CMakeLists.txt
- include
- - Master_Thread.h
- - Entry.h
- - ROS_Topic_Thread.h
- src
- - pcl_ros_init.cpp
- - archive
- - - pcl_ros_not_updated.cpp
- - - pc_ros_old2.cpp
- - - thread.cpp
- - - pcl_ros_old.cpp
- package.xml
我没有把classes的定义分别放在cpp文件里,我暂时把它们都和主代码(pcl_ros_init.cpp)放在一起了。我的 classes 之间有一些轻微的依赖关系,例如 Master_Thread class 在其声明中使用 ROS_Topic_Thread 现在,Cmake 能够找到包含文件,但在像 Master_Thread.h 这样的头文件,无法找到 ROS_Topic_Thread 的 class 签名
pcl_ros_custom/include/Master_Thread.h:38:36: error: ‘ROS_Topic_Thread’ has not been declared
这是我的CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(pcl_ros_custom)
find_package(catkin REQUIRED COMPONENTS
roscpp
pcl_conversions
pcl_ros)
catkin_package(
INCLUDE_DIRS
CATKIN_DEPENDS roscpp
pcl_conversions
pcl_ros
)
include_directories(
include
${catkin_INCLUDE_DIRS}/include/**
)
add_executable(pcl_ros_init src/pcl_ros_init.cpp)
target_link_libraries(pcl_ros_init
${catkin_LIBRARIES} ${roslib_LIBRARIES} ${PCL_LIBRARIES} )
这是 2 个头文件
#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
//include custom classes
#include "ROS_Topic_Thread.h"
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class Master_Thread
{
private:
std::atomic<bool> mKillswitch;
std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1, mMasterbuffer2, mMasterbuffer3, mMasterbuffer4, mMergedPointCloud2Ptr_buffer;
public:
Master_Thread();
void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2, ROS_Topic_Thread&t3, ROS_Topic_Thread&t4 );
void process_buffer();
void setkillswitch();
};
}
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
#include "Master_Thread.h"
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread
{
private:
ros::Subscriber mSub;
// Topic
const std::string mSUB_TOPIC;
//buffer
std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
std::mutex mMutex;
public:
ROS_Topic_Thread()= delete;
ROS_Topic_Thread(ros::NodeHandle* nodehandle, const std::string& SUBTOPIC, ClusteringBenchmarkEntry* entry);
void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
friend class Master_Thread;
};
} //namespace close scope
#endif //for ROS_TOPIC_THREAD_H
我哪里错了?
编辑
This 是一个非常好的 link,它清除了我在 C++ 中的大部分头文件组织,这在答案之上帮助我顺利进行项目构建。希望对阅读本文的人有所帮助。
本质上,您不需要将 header 包含在另一个中。这修复了通告包含。
在 ROS_Topic_Thread.h 中,friend class Master_Thread;
已经向前声明了 Mater_Thread,因此您不需要 Master_Thread.h header。
在 Master_Thread.h 中,您还可以在 Master_Thread class 的声明之前转发声明 class ROS_Topic_Thread;
,因为您只在 transfer_to_master_buffer
中使用对 ROS_Topic_Thread 的引用] 方法。
#ifndef MASTER_THREAD_H
#define MASTER_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
//include custom classes
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread;
class Master_Thread
{
private:
std::atomic<bool> mKillswitch;
std::vector<sensor_msgs::PointCloud2ConstPtr> mMasterbuffer1, mMasterbuffer2, mMasterbuffer3, mMasterbuffer4, mMergedPointCloud2Ptr_buffer;
public:
Master_Thread();
void transfer_to_master_buffer(ROS_Topic_Thread&t1,ROS_Topic_Thread&t2, ROS_Topic_Thread&t3, ROS_Topic_Thread&t4 );
void process_buffer();
void setkillswitch();
};
}
#endif
#ifndef ROS_TOPIC_THREAD_H
#define ROS_TOPIC_THREAD_H
#include <ros/ros.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <sensor_msgs/PointCloud2.h>
#include <thread>
#include <mutex>
#include <chrono>
#include <functional>
#include <utility>
#include <string>
#include "ClusteringBenchmarkEntry.h"
namespace OCB
{
class ROS_Topic_Thread
{
private:
ros::Subscriber mSub;
// Topic
const std::string mSUB_TOPIC;
//buffer
std::vector<sensor_msgs::PointCloud2ConstPtr> mThreadbuffer;
std::mutex mMutex;
public:
ROS_Topic_Thread()= delete;
ROS_Topic_Thread(ros::NodeHandle* nodehandle, const std::string& SUBTOPIC, ClusteringBenchmarkEntry* entry);
void callback(const sensor_msgs::PointCloud2ConstPtr& sub_ptr);
friend class Master_Thread;
};
} //namespace close scope
#endif //for ROS_TOPIC_THREAD_H