我想使用 Snap7 (多平台以太网 S7 PLC 通信套件)在 ROS 和 Siemens S7 1500 之间建立通信。
我遇到了链接器问题 - 虽然我可以使用自定义 FindLibSnap7.cmake 找到包含和库 .so 文件本身,但target_link_libraries
没有链接到 snap7 库。这是我的方法和有助于调试的输出:
(注意:目前我们被迫使用 ROS Kinetic,因此使用 Snap7 Python 包装器似乎不是一个选项,因为它是用 python3 编写的。)
我安装了库:
sudo add-apt-repository ppa:gijzelaar/snap7
sudo apt-get update
sudo apt-get install libsnap71 libsnap7-dev
结果:
$ ll /usr/lib/libsnap7.so
-rwxr-xr-x 1 root root 305944 Apr 24 22:18 /usr/lib/libsnap7.so*
$ ll /usr/include/snap7.h
-rw-r--r-- 1 root root 41954 Jun 2 2015 /usr/include/snap7.h
FindLibSnap7.cmake:
# find libsnap7
#
# exports:
#
# LibSNAP7_FOUND
# LibSNAP7_INCLUDE_DIRS
# LibSNAP7_LIBRARIES
#
find_package(PkgConfig REQUIRED)
# Use pkg-config to get hints about paths
#pkg_check_modules(LibSNAP7_PKGCONF REQUIRED libsnap7)
# Include dir
find_path(LibSNAP7_INCLUDE_DIR
NAMES snap7.h
PATHS ${LibSNAP7_PKGCONF_INCLUDE_DIRS}
)
find_library(
LibSNAP7_LIBRARY
NAMES snap7 libsnap7
PATHS /usr/lib
# PATH_SUFFIXES lib
# NO_DEFAULT_PATH
)
#FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibSNAP7 DEFAULT_MSG LibSNAP7_LIBRARY LibSNAP7_INCLUDE_DIR)
message("LibSNAP7_INCLUDE_DIR ${LibSNAP7_INCLUDE_DIR}")
message("LibSNAP7_LIBRARY ${LibSNAP7_LIBRARY}")
set(LibSnap7_LIBRARIES ${LibSNAP7_LIBRARY})
set(LibSnap7_INCLUDE_DIRS ${LibSNAP7_INCLUDE_DIR})
set(LibSnap7_FOUND yes)
# Set the include dir variables and the libraries and let libfind_process do the rest.
# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
#set(LibSNAP7_PROCESS_INCLUDES LibSNAP7_INCLUDE_DIR)
#set(LibSNAP7_PROCESS_LIBS LibSNAP7_LIBRARY)
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(agv_snap7_driver)
set(CMAKE_VERBOSE_MAKEFILE ON)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
##########################################################################################
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
MESSAGE( STATUS "CMAKE_MODULE_PATH: " ${CMAKE_MODULE_PATH} )
find_package(LibSnap7 REQUIRED)
include_directories(${LibSnap7_INCLUDE_DIRS})
message("LibSnap7_LIBRARIES ${LibSnap7_LIBRARIES}")
message("LibSnap7_INCLUDE_DIRS ${LibSnap7_INCLUDE_DIRS}")
##########################################################################################
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
nodelet
agv_utils
agv_msgs
)
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS ${THIS_PACKAGE_ROS_DEPS}
DEPENDS LibSnap7
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
${LibSnap7_INCLUDE_DIRS}
)
## Declare a C++ library
add_library(${PROJECT_NAME}
src/agv_snap7_driver_config.cpp
src/agv_snap7_driver.cpp
)
## Add cmake target dependencies of the executable
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries( ${PROJECT_NAME} ${catkin_LIBRARIES} ${LibSnap7_LIBRARIES})
#############
## Install ##
#############
## Mark libraries for installation
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)
install(FILES nodelet.xml
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
FILES_MATCHING PATTERN "*.launch"
PATTERN ".svn" EXCLUDE
)
install(DIRECTORY config/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config
FILES_MATCHING PATTERN "*.yaml"
PATTERN ".svn" EXCLUDE
)
catkin_make 输出:
-- +++ processing catkin package: 'agv_snap7_driver'
-- ==> add_subdirectory(agv_base_hardware/agv_snap7_driver)
-- CMAKE_MODULE_PATH: /home/twobit/workspaces/bestvc_ws/branches/Gestamp_Amorebieta_01/base/src/agv_base_hardware/agv_snap7_driver/cmake
LibSNAP7_INCLUDE_DIR /usr/include
LibSNAP7_LIBRARY /usr/lib/libsnap7.so
LibSnap7_LIBRARIES /usr/lib/libsnap7.so
LibSnap7_INCLUDE_DIRS /usr/include
lld 输出
$ ldd libagv_snap7_driver.so | grep snap
libsnappy.so.1 => /usr/lib/x86_64-linux-gnu/libsnappy.so.1 (0x00007fe89c01e000)
如您所见,找到了包含以及库。提供${LibSnap7_LIBRARIES}
给target_link_libraries
不是链接到 libsnap7.so
从源代码安装 snap7 后,我发现示例 cpp 的链接正确。
如果有人知道为什么会发生此错误链接,我将很乐意提供任何帮助。
先感谢您!