Linking Classes in 2 user defined packages
Hello.
I'm beggeing with ROS I'm studing CMakeLists.txt using diffrent approches to execute same code. I have a problem with the linking phase of compilation of two user defined packages :
pkg1 : spheroimureader I've defined a Class ImuTopicReader
pkg 2 : spherocrashserver I've defined a Class CrashServer
I want to use the Class ImuTopicReader inside CrashServer class
the files:
spheroimureader :
->src/topic_reader.h ->src/topic_reader.cpp
spherocrashserver :
->/include/sphero_crash_server/topic_reader.h ->src/topic_reader.h ->src/topic_reader.cpp
I've got these problems :
fatal error: topic_reader.h: no such file or directory
CMakeLists.txt for spherotopicreader :
cmake_minimum_required(VERSION 2.8.3)
project(sphero_imu_reader)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
sensor_msgs
roscpp
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES imu_topic_reader
CATKIN_DEPENDS sensor_msgs roscpp
# DEPENDS system_lib
)
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/topic_reader.cpp)
add_dependencies(${PROJECT_NAME}_node ${catkin_EXPORTED_TARGETS})
target_link_libraries(${PROJECT_NAME}_node
${catk
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
package.xml for Spheroimureader
<buildtool_depend>catkin</buildtool_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>roscpp</build_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<exec_depend>sensor_msgs</exec_depend>
<exec_depend>roscpp</exec_depend>
topicreader.h for spheroimu_reader pkg
#ifndef SPHERO_IMU_READER
#define SPHERO_IMU_READER
#include "ros/ros.h"
#include "sensor_msgs/Imu.h"
#endif
class ImuTopicReader
{
private:
std::string name_;
ros::NodeHandle n_;
ros::Subscriber subs_;
sensor_msgs::Imu imu_;
public:
ImuTopicReader(std::string name)
{
name_ = name;
subs_ = n_.subscribe(name_,1,&ImuTopicReader::imuCallback,this);
}
void imuCallback(const sensor_msgs::Imu::ConstPtr& msg);
sensor_msgs::Imu getImu();
};
CMakeLists.txt for spherocrashserver :
cmake_minimum_required(VERSION 2.8.3)
project(sphero_crash_server)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
sensor_msgs
sphero_imu_reader
)
catkin_package(
CATKIN_DEPENDS roscpp sphero_imu_reader sensor_msgs
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/server.cpp)
add_dependencies(${PROJECT_NAME}_node } ${catkin_EXPORTED_TARGETS} ${catkin_LIBRARIES})
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
package.xml for spherocrashserver :
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>sphero_imu_reader</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>sphero_imu_reader</build_export_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>sphero_imu_reader</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
server.h in pgk spherocrashserver :
#include "ros/ros.h"
#include "sphero_crash_server/topic_reader.h"
#include "std_srvs/Trigger.h"
#include "sensor_msgs/Imu.h"
#include <algorithm>
#include <vector>
class CrashServer
{
private:
ros::NodeHandle n_;
ImuTopicReader imu_topic_;
float threshold_;
ros::ServiceServer service_;
std::string name_;
public:
CrashServer (std::string name,float threshold)
{
name_ = name;
imu_topic_ = ImuTopicReader(name_);
threshold_ = threshold;
service_ = n_.advertiseService("detect_crash_action_serv",&CrashServer::detectionCallback,this);
std::cout << "done" <<std::endl;
}
std::string sectorDetection();
bool detectionCallback(std_srvs::Trigger::Request &req , std_srvs::Trigger::Response &res);
};
Asked by houssemDZ-25 on 2019-02-11 13:18:54 UTC
Answers
You should change a litlle bit your layout to have things working (and respect ROS standards), organize your files like that first :
catkin_ws/
src/
sphero_imu_reader/
src/
topic_reader.cpp
include/
sphero_imu_reader/
topic_reader.h
sphero_crash_server/
src/
server.cpp
include/
sphero_crash_server/
server.h
Now for your both CMakeLists.txt
, just add this inside catkin_package()
function :
INCLUDE_DIRS include
Now in server.h
you can add this line and use the class ImuTopicReader
inside the class CrashServer
:
#include "sphero_imu_reader/topic_reader.h"
Note : You might have cpp issues when calling the constructor but your first error should be gone.
Asked by Delb on 2019-02-12 04:37:06 UTC
Comments
I've edited the layout its didn't even compile for the ImuTopicReader pakacge. I checked many projects on github they haven't used that convention of putting .h only in the include folder
Asked by houssemDZ-25 on 2019-02-13 06:33:55 UTC
its didn't even compile for the ImuTopicReader pakacge What is the error exactly ?
Also have you made the changes in CmakeLists.txt to tell that the headers are in the include file as I told you at the end of my answer ?
Asked by Delb on 2019-02-13 08:29:31 UTC
I checked many projects on github they haven't used that convention
Were they official ros packages ? You can see this layout in packages like move_base
, there is even a ros_best_practices
package using this layout. Can you tell me one of those package ?
Asked by Delb on 2019-02-13 08:35:11 UTC
- I've changed the CmaleLists.txt and I
try to compile ImuTopicReader and I've
got this error : fatal error: sphero_imu_reader/topic_reader.h: no
such file or dir
- for exemple the package gmaping package doesn't use this convention https://github.com/ros-perception/slam_gmappi
Asked by houssemDZ-25 on 2019-02-13 16:42:38 UTC
Can you edit your question to show us the new CMakeLists.txt files and also the source files please ? And aalso the description of your layout too, for both packages.
Asked by Delb on 2019-02-13 16:53:23 UTC
Comments