【学习笔记】解决Serial Communication Library编译问题

【学习笔记】解决编译 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

相关推荐
虾球xz14 分钟前
游戏引擎学习第11天
stm32·学习·游戏引擎
心怀梦想的咸鱼16 分钟前
Ue5 umg学习(三)文本控件
学习·ue5
心怀梦想的咸鱼18 分钟前
Ue5 umg学习(二)图像控件,锚点
学习·ue5
可均可可31 分钟前
C++之OpenCV入门到提高005:005 图像操作
c++·图像处理·opencv·图像操作
zyx没烦恼35 分钟前
【STL】set,multiset,map,multimap的介绍以及使用
开发语言·c++
机器视觉知识推荐、就业指导1 小时前
基于Qt/C++与OpenCV库 实现基于海康相机的图像采集和显示系统(工程源码可联系博主索要)
c++·qt·opencv
aPurpleBerry1 小时前
【问题解决】Github上手动Delete file之后, git remote add+git push出错
git·github
MarcoAI1 小时前
github SSH连接(windows)
windows·ssh·github
小A1591 小时前
STM32完全学习——系统时钟设置
stm32·嵌入式硬件·学习
myloveasuka1 小时前
类与对象(1)
开发语言·c++