ROS CPP Snap7 Linux - Catkin/CMake finds library but does not link

asked 2021-04-26 05:41:03 -0500

TwoBid gravatar image

updated 2021-04-27 06:59:45 -0500

I would like to use Snap7 ( Multi-platform Ethernet S7 PLC communication suite) to establish a communication between ROS and a Siemens S7 1500.

I encountered a linker problem - Although I can find the includes and the library .so file itself, using a custom FindLibSnap7.cmake, target_link_libraries is not linking against the found library. Here my approach and the outputs helping debugging:

(Note: Currently we are forced to use ROS Kinetic, so using the Snap7 Python wrapper seems like not an option cause its written in python3.)


System Setup:

Ubuntu 16.04 LTS x86 ROS Kinetic Distro


I installed the library:

  • sudo add-apt-repository ppa:gijzelaar/snap7
  • sudo apt-get update
  • sudo apt-get install libsnap71 libsnap7-dev

Result:

$ 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 ...
(more)
edit retag flag offensive close merge delete

Comments

Not an answer, but:

Currently we are forced to use ROS Kinetic, so using the Snap7 Python wrapper seems like not an option cause its written in python3.

There's a good chance you can use Python 3, using either catkin_virtualenv or rospypi/simple.

gvdhoorn gravatar image gvdhoorn  ( 2021-04-26 05:49:17 -0500 )edit

Pedantic perhaps, but:

ROS CPP Snap7 Linux - Catkin finds library but does not link

Catkin doesn't link, it's not involved in that. This is all CMake.

gvdhoorn gravatar image gvdhoorn  ( 2021-04-27 06:58:40 -0500 )edit