0. Preface
During this time, I started to learn to use cmake to match various packages, and some processes are recorded here for easy review.
This section discusses referencing third-party libraries and exporting your own
1. Import external library
For example, we cloned the ompl library from github and want to use it in our own package, how to do it?
First, put the ompl package and your own pkg package in the src directory of the workspace, and compile ompl in advance:
colcon build --packages-select ompl
Add the following code to the CMakeFiles file of our pkg package:
find_package(ompl REQUIRED)
find_package
It will automatically locate and load the configuration file of the specified library, first search in the current hierarchical directory (install folder), if not found, go to the system directory (usr/local/share) to continue searching (so when using binary installation at the same time After compiling with the source code, the source code is used first).
(REQUIRED: optional field. It means that the package must be found. If it cannot be found, the entire CMake will be stopped immediately. If REQUIRED is not specified, CMake will continue to execute.)
After the lookup is complete, several keywords are returned:
- <LibaryName>_FOUND
is used to judge whether the target function package is successfully found - <LibaryName>_INCLUDE_DIR or <LibaryName>_INCLUDES
contains the path of the folder containing the header file - <LibaryName>_LIBRARY or <LibaryName>_LIBRARIES
library file path
If you want to see the path address specifically indexed by the keyword, you can use:
message(STATUS "find OMPL_INCLUDE:${OMPL_INCLUDE_DIRS}")
message(STATUS "find OMPL_LIB:${OMPL_LIBRARIES}")
Come check it out. Since we use colcon build to compile, the output log information will be stored in the log folder under the workspace, just enter the folder corresponding to the date and time and open the stdout file.
Then, we import the header and library files of the target library:
include_directories(
${
OMPL_INCLUDE_DIRS}
)
add_executable(ompl_test_node
src/ompl_test_node.cpp
)
target_link_libraries(ompl_test_node
${
OMPL_LIBRARIES}
)
In this way, we have successfully imported the header file and successfully linked the library file to the target executable file.
2. Export custom library
This part refers to the article: https://zhuanlan.zhihu.com/p/87738686
Conversely, we can also let others or our future self use our module through find_package().
For the method of exporting dynamic libraries, just add_executable
change the above toadd_library
add_library(omcl_ros2 SHARED
src/ompl_test_node.cpp
src/fcl_test_node.cpp
)
ament_target_dependencies(omcl_ros2
rclcpp
)
target_link_libraries(omcl_ros2
${
OMPL_LIBRARIES}
${
FCL_LIBRARIES}
)
install(TARGETS
omcl_ros2
DESTINATION lib/${
PROJECT_NAME}
)
install
The dynamic library can be installed into the lib folder by . When called in other packages, the path to the dynamic library can be obtained through find_package.
Finally, install the header file:
install(DIRECTORY
include/${
PROJECT_NAME}
DESTINATION include
)