Basic C++ libraries to demonstrate creating C++ libraries in ROS packages.
This package is created using the following commands
cd ~/ros_workspaces/learning_ws/src
catkin_create_pkg basic_cpp_libs roscppThis package is to serve as a collection of libraries. These libraries could be used by nodes of this package or other packages. Traditionally, it is more efficient to package functionality as modules (or libraries) that you can include (or import) in your nodes.
Suggested order of traversal for the items in this package (specially for beginners)
| S. No. | Name | Link | Description |
|---|---|---|---|
| 1 | Simple Library | Libraries > SimpleLibrary | A simple C++ library for demonstration |
| 2 | Node for Simple Library | Nodes > SimpleLibraryNode | A node demonstrating the use of simple_library library |
| 3 | Basic Math library | Libraries > BasicMath | A library to demonstrate a more complex library in a ROS package |
| 4 | node for Math library | Nodes > BasicMath | A node demonstrating the use of basic_math library |
Libraries defined in this package. As a convention, libraries are put inside the src folder.
| Field | Value |
|---|---|
| Header file | include/basic_cpp_libs/simple_library.h |
| Source code | src/simple_library.cpp |
A library to only demonstrate how C++ libraries can be created in packages.
C++ code requires building. These procedures can be found easily. In the CMakeLists.txt file, do the following
-
Make sure that
roscppis set up as a dependency (in bothCMakeLists.txtandpackage.xmlfiles). -
In the
catkin_packagefunction (undercatkin specific configurationheader), modify it to the followingcatkin_package( INCLUDE_DIRS include LIBRARIES ${PROJECT_NAME} CATKIN_DEPENDS roscpp # DEPENDS system_lib )The
INCLUDE_DIRSis to include the folders containing header file code. TheLIBRARIESis used to export a name for dependent packages. TheCATKIN_DEPENDSis used to list thecatkin_packagesused. -
In the
include_directoriesfunction (under theBuildheader), modify it to the followinginclude_directories( include ${catkin_INCLUDE_DIRS} )List of directories that contains the header files
-
After that, add a
add_libraryfunction for including source code fileadd_library(${PROJECT_NAME} src/simple_library.cpp )List of
.cppsource files for the library. Things are declared in the header files (listed in theinclude_directoriesfunction) and defined in these.cppfiles (source files). -
Link the libraries for this library using
target_link_libraries, add the following at the appropriate placetarget_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})This will allow catkin libraries to be linked to this library (which is named
${PROJECT_NAME}) -
Mark this library for installation (these will be placed under
Installheader). Add the following underMark libraries for installationinstall(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} )- The
TARGETSlist out the library names that this package has - The
ARCHIVE DESTINATIONcommand is for the archiving folder, the${CATKIN_PACKAGE_LIB_DESTINATION}variable is essentially set tolib - The
LIBRARY DESTINATIONcommand is for the destination to store the library file (that can be directly linked). This is also indirectly set tolib(through the variable), which is the folder indevel(inside the workspace) where the.sofile will go. - The
RUNTIME DESTINATIONcommand is for the runtime binaries. This is set tobinthrough the variable.
- The
-
Now that all configurations are done, we need to tell the final destination where the header files must be installed to. Add the following
installfunction under the one described aboveinstall( DIRECTORY include/${PROJECT_NAME} DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} )This is finally done to expose the header files.
Now that everything is set, run catkin_make in the workspace directory. After a successful build, a file by the name of libbasic_cpp_libs.so must be available in the folder devel/lib/ (in the workspace). This is the library file that can be linked to (for creating nodes).
A node in this package is made for demonstrating this library.
| Field | Value |
|---|---|
| Main header file | include/basic_math/basic_math.hpp |
A relatively more sophisticated library for demonstrating complex C++ libraries inside ROS packages.
The directory structure for header files is as follows
include
├── basic_cpp_libs
│ └── simple_library.h
└── basic_math
├── algebra
│ ├── algebra.hpp
│ ├── real_number.hpp
│ └── real_number.ipp
├── basic_math.hpp
└── imp_functions
├── factorial.hpp
└── imp_functions.hppAnd the source files are as following (in the src folder)
src/basic_math
└── imp_functions
└── factorial.cppC++ Libraries in ROS packages has to be built. Traditionally, like the simple_library, the headers are included in the include/PACKAGE_NAME folder. But the folder name and even the library name can be changed to anything and that is experimented here.
In the CMakeLists.txt file, do the following
-
In the
catkin_packagefunction (undercatkin specific configurationheader), add the library name to theLIBRARIESlist. The function is modified to thiscatkin_package( INCLUDE_DIRS include LIBRARIES ${PROJECT_NAME} ${PROJECT_NAME}_basic_math CATKIN_DEPENDS roscpp # DEPENDS system_lib )Note that the
${PROJECT_NAME}library name is taken by the simple_library of this package. This library will take the name${PROJECT_NAME}_basic_math. Other arguments are same as in case ofsimple_library -
Each library requires a dedicated
add_libraryfunction for pointing out the source code (definitions for all the things declared in the headers). For this one, the function may look like thisadd_library(${PROJECT_NAME}_basic_math # Important functions (source code) src/basic_math/imp_functions/factorial.cpp )There can be many source files in this list (as are required for the entire library)
-
The library has to be linked to
${catkin_LIBRARIES}, this is done usingtarget_link_libraries. Add the followingtarget_link_libraries(${PROJECT_NAME}_basic_math ${catkin_LIBRARIES})This will link catkin libraries with this library
-
This library will be installed at the same space, so it will share the marked libraries as the
simple_library. Modify theinstallfunction to mark libraries for installation to the followinginstall(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_basic_math ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} )Note that this library
${PROJECT_NAME}_basic_mathis simply appended to the list of libraries underTARGETS. This is because this library shares theARCHIVE DESTINATIONand other settings with thesimple_library. Most of the libraries belonging to a package share these settings. -
Add an
installto set the install destination for the C++ header files. As this library is not in a folder bearing the package name insideinclude, theinstallcommand is a little different from beforeinstall( DIRECTORY include/basic_math DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/basic_math )Notice that the
DIRECTORYfor this library is set toinclude/basic_mathwhich has the header files (unconventional, but can work). Similarly, theDESTINATIONis set to${CATKIN_GLOBAL_INCLUDE_DESTINATION}/basic_mathinstead of${CATKIN_PACKAGE_INCLUDE_DESTINATION}. The difference is${CATKIN_PACKAGE_INCLUDE_DESTINATION}is set to${CATKIN_GLOBAL_INCLUDE_DESTINATION}/${PROJECT_NAME}, which becomesinclude/basic_cpp_libs. Therefore, the#includestatements will start includes from that folder (that is,#include<basic_cpp_libs/...).${CATKIN_GLOBAL_INCLUDE_DESTINATION}is set toinclude, therefore${CATKIN_GLOBAL_INCLUDE_DESTINATION}/basic_mathbecomesinclude/basic_math. The#includestatements will start includes with#include<basic_math/....
After this, run catkin_make in the workspace folder. After a successful build, a file named libbasic_cpp_libs_basic_math.so shall appear in the folder devel/lib for signifying this library. This file will be linked with executables.
A node in this package is made for demonstrating this library.
Nodes contained in this package. Most of them are for demonstrating uses of the libraries in this package.
| Field | Value |
|---|---|
| Name | simple_library_node |
| Code | src/simple_library_node.cpp |
A C++ node made to demonstrate using the simple_library library of this package.
To build and run this node, add the following lines to the CMakeLists.txt file
add_executable(simple_library_node src/simple_library_node.cpp)
target_link_libraries(simple_library_node ${catkin_LIBRARIES} ${PROJECT_NAME})Notice that the target_link_libraries for the simple_library_node executable links the ${PROJECT_NAME}, which is the library name (in the add_library and other building steps).
After this, run catkin_make in the workspace folder. To run this node, first run roscore and then execute this node using
rosrun basic_cpp_libs simple_library_node| Field | Value |
|---|---|
| Name | basic_math_node |
| Code | src/basic_math_node.cpp |
A C++ node made to demonstrate using the basic_math library of this package.
To build and run this code, add the following lines to the CMakeLists.txt file
add_executable(basic_math_node src/basic_math_node.cpp)
target_link_libraries(basic_math_node ${catkin_LIBRARIES} ${PROJECT_NAME}_basic_math)Notice that the target_link_libraries for the basic_math_node executable links the ${PROJECT_NAME}_basic_math, which is the library name for basic_math library in this package (described in the add_library for it).
After this, run catkin_make in the workspace folder. To run this node, first run roscore and then execute this node using
rosrun basic_cpp_libs basic_math_node- How to build and install C++ headers through catkin
- List of variables defined in catkin