ROS CPP Snap7 Linux - CMake does not link against lib

225 views Asked by At

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 snap7 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.)

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(DIRECTORY config/
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config
  FILES_MATCHING PATTERN "*.yaml"
  PATTERN ".svn" EXCLUDE
  )

catkin_make output:

-- +++ 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 output

$ ldd libagv_snap7_driver.so | grep snap
  libsnappy.so.1 => /usr/lib/x86_64-linux-gnu/libsnappy.so.1 (0x00007fe89c01e000)

So as you can see the includes are found, as well the library. Providing ${LibSnap7_LIBRARIES} to target_link_libraries is not linking agains libsnap7.so

After installing snap7 from source I found the example cpp's are linked correctly.

When anyone has an idea why this incorrect link is happening I would be happy for any help.

Thank you in advance!

0

There are 0 answers