【学习笔记】解决编译 Serial Communication Library 时的 Catkin 依赖问题
Serial Communication Library 是一个用 C++ 编写的用于连接类似 rs-232 串口的跨平台库。它提供了一个现代的 C++ 接口,它的工作流程设计在外观和感觉上与 PySerial 相似,但串口速率和控制由 C++ 提供。
github 地址:
https://github.com/wjwwood/serial
Serial Communication Library 在多个机器人相关项目中使用,可以像大多数 unix 库一样通过 make 和 sudo make install 构建并安装到操作系统中,但因为它是一个 catkin 项目,所以也可以在 catkin 工作区中与其他 catkin 项目一起构建。
我使用 cmake 来编译这个工程,会有如下的错误:
sh
CMake Error at CMakeLists.txt:5 (find_package):
By not providing "Findcatkin.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "catkin", but
CMake did not find one.
Could not find a package configuration file provided by "catkin" with any
of the following names:
catkinConfig.cmake
catkin-config.cmake
Add the installation prefix of "catkin" to CMAKE_PREFIX_PATH or set
"catkin_DIR" to a directory containing one of the above files. If "catkin"
provides a separate development package or SDK, be sure it has been
installed.
可以看到,必须安装相应的 Catkin 环境才可以编译,因为我没有安装 Catkin 环境,所以没办法编译成功。但是我不想依赖 Catkin,所以就去寻找了去除 Catkin 依赖的方法。
我在这个github项目的 Issues 中看到了很多人也有这个需求,最后在这里Remove Catkin dependency while keeping CMake's find_package() feature by dbolkensteyn · Pull Request #133 · wjwwood/serial · GitHub中找到了解决方案,只需要修改 CMakeLists.txt 就可以删除Catkin依赖。
CMakeLists.txt 中需要修改的地方对比图如下:
最后将 CMakeLists.txt 修改成如下:
cmake_minimum_required(VERSION 2.8.3)
project(serial)
if(APPLE)
find_library(IOKIT_LIBRARY IOKit)
find_library(FOUNDATION_LIBRARY Foundation)
endif()
## Sources
set(serial_SRCS
src/serial.cc
include/serial/serial.h
include/serial/v8stdint.h
)
if(APPLE)
# If OSX
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_osx.cc)
elseif(UNIX)
# If unix
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_linux.cc)
else()
# If windows
list(APPEND serial_SRCS src/impl/win.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_win.cc)
endif()
## Add serial library
add_library(${PROJECT_NAME} STATIC ${serial_SRCS})
if(APPLE)
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif(UNIX)
target_link_libraries(${PROJECT_NAME} rt pthread)
else()
target_link_libraries(${PROJECT_NAME} setupapi)
endif()
## Add example project
add_executable(serial_example examples/serial_example.cc)
add_dependencies(serial_example ${PROJECT_NAME})
target_link_libraries(serial_example ${PROJECT_NAME})
## Include headers
include_directories(include)
## Install
set(INSTALL_LIB_DIR lib)
set(INSTALL_INCLUDE_DIR include)
set(INSTALL_CMAKE_DIR share/serial/cmake)
## Install executable
install(TARGETS ${PROJECT_NAME}
DESTINATION ${INSTALL_LIB_DIR}
EXPORT ${PROJECT_NAME}-targets
)
## Install headers
install(FILES include/serial/serial.h include/serial/v8stdint.h
DESTINATION ${INSTALL_INCLUDE_DIR}/serial)
## Install CMake files
install(EXPORT ${PROJECT_NAME}-targets DESTINATION ${INSTALL_CMAKE_DIR})
include(CMakePackageConfigHelpers)
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Version.cmake
VERSION 1.2.1
COMPATIBILITY AnyNewerVersion)
install(FILES ${CMAKE_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Version.cmake
DESTINATION ${INSTALL_CMAKE_DIR})
## Tests
# FIXME
#if(CATKIN_ENABLE_TESTING)
# add_subdirectory(tests)
#endif()
修改完之后重新 cmake 就可以了。
本文链接:https://blog.csdn.net/u012028275/article/details/140701592