I have a ROS2 Project and I want to develop a good structured filesystem. I wanted to do some "good-practice"(from my point of knowledge) and import a class definition from seperate header and source files into my "main" source file.
I want to get a minimal example running, but have linking issues when building it and am also not sure if what I'm trying is even considered as good practice or not.
I have following package structure (inside workspace/src/):
pkg_user/
/include/pkg_user/
CUserInterface.h // class declaration
/src
CUserInterface.cpp // class definition
user_interface.cpp // "main" source file
CMakeLists.txt
package.xml
"/include/pkg_user/" containing the Class-declaration: CUserInterface.h
#include "my_interfaces/srv/submit_pose.hpp"
#ifndef CUSERINTERFACE_H
#define CUSERINTERFACE_H
class CUserInterface
{
public:
CUserInterface(){};
~CUserInterface(){};
int submit_pose_to_robot();
private:
};
#endif
/src/ containing the Class-definition: CUserInterface.cpp
#include "CUserInterface.h"
int CUserInterface::submit_pose_to_robot()
{
std::cout<<"hello world from CUserInterface.cpp"<<std::endl;
return 0;
}
and also the "main" source code: user_interface.cpp which implements the class
#include "rclcpp/rclcpp.hpp"
#include <unistd.h>
#include "CUserInterface.h"
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
CUserInterface myUI;
myUI.submit_pose_to_robot();
sleep(5);
rclcpp::shutdown();
return 0;
}
Now when I compile that I get following (linker?) error:
jo@myubuntu:~/workspace/ros_ur_driver$ colcon build --packages-select pkg_user
Starting >>> pkg_user
--- stderr: pkg_user
/usr/bin/ld: CMakeFiles/user_interface.dir/src/user_interface.cpp.o: in function `main':
user_interface.cpp:(.text+0xd7): undefined reference to `CUserInterface::submit_pose_to_robot()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/user_interface.dir/build.make:141: user_interface] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/user_interface.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
---
Failed <<< pkg_user [3.73s, exited with code 2]
Summary: 0 packages finished [3.98s]
1 package failed: pkg_user
1 package had stderr output: pkg_user
Unfortunately I have not much knowledge in CMake, and I couldn't figure out how to link(?) the files to get it working. My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.5)
project(pkg_user)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(my_interfaces REQUIRED)
add_executable(user_interface src/user_interface.cpp)
ament_target_dependencies(user_interface rclcpp my_interfaces)
# include my own headers
target_include_directories(user_interface PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME}
)
install(TARGETS
user_interface
DESTINATION lib/${PROJECT_NAME})
ament_package()
How is the right way to place the files and how should the CMakeLists.txt should look like?
Originally posted by jorosuser on ROS Answers with karma: 31 on 2021-11-26
Post score: 0