Ask Your Question
0

how to run libmodbus

asked 2016-04-06 15:19:58 -0500

Christian Hidalgo gravatar image

updated 2016-04-08 17:35:24 -0500

I added

include_directories(include /usr/include/libmodbus)

to the CMakeList.txt of my package, and my test node is

#include <ros/ros.h>
#include <libmodbus/src/modbus.h>


int main(int argc, char** argv) {
  modbus_t *Scara;
  Scara = modbus_new_tcp("192.168.1.219", 502);
        if (modbus_connect(Scara) == -1) {
            ROS_ERROR("Conexion fallida");
            modbus_free(Scara);
        }else{
            ROS_INFO("Conexion exitosa");
        }                      

  return 0;
}

but when i run catkin_make i get the following errors

In function `main':
scaraListen.cpp:(.text+0x1b): undefined reference to `modbus_new_tcp'
scaraListen.cpp:(.text+0x2b): undefined reference to `modbus_connect'
scaraListen.cpp:(.text+0x135): undefined reference to `modbus_free' 
collect2: error: ld returned 1 exit status

It seems that it's not linking to the library, but when i add

target_link_libraries(main /usr/include/libmodbus.so)

to the CMakeList.txt and try to compile i get:

(target_link_libraries):
   Cannot specify link libraries for target "main" which is not built by this project.

Where i'm messing up?

EDIT: my complete CMakeList.txt looks like this

cmake_minimum_required(VERSION 2.8.3)
project(pointCloud)


find_package(catkin REQUIRED COMPONENTS
  pcl_conversions
  pcl_msgs
  pcl_ros
  roscpp
  rospy
  std_msgs
  sensor_msgs
  tf
  laser_geometry
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)


include_directories(include ${catkin_INCLUDE_DIRS})
include_directories(include /usr/include/libmodbus)

add_executable(converter src/converter.cpp)
target_link_libraries(converter ${catkin_LIBRARIES})
add_dependencies(converter pointCloud_conv)

add_executable(tfTest src/tftest.cpp)
target_link_libraries(tfTest ${catkin_LIBRARIES})
add_dependencies(tfTest pointCloud_conv)

add_executable(scaraListen src/scaraListen.cpp)
target_link_libraries(scaraListen ${catkin_LIBRARIES})
add_dependencies(scaraListen pointCloud_conv)
edit retag flag offensive close merge delete

Comments

This seems like a simple linking problem. Can you please add your CMakeLists.txt to your post? Use the edit button/link for that. Please do not include all the boilerplate comments, just the commands.

gvdhoorn gravatar imagegvdhoorn ( 2016-04-07 03:25:44 -0500 )edit

Also: in general it's not a good idea to hard code paths to libraries and headers / include paths. CMake supports some nice features for auto-detection of locations of such things, and I think you're not using them.

gvdhoorn gravatar imagegvdhoorn ( 2016-04-07 03:26:27 -0500 )edit

I had some problems with libmodbus so I just wrote a ros driver to prevent others from having to figure it out. Let me know if this helps. Feel free to build on this for your own project.

https://github.com/sonyccd/ros_plc_mo...

sonyccd gravatar imagesonyccd ( 2016-04-07 08:17:00 -0500 )edit

@sonyccd: I appreciate your contribution and the package, but this it is not an answer, so please don't post it as one. This is what comments are for.

gvdhoorn gravatar imagegvdhoorn ( 2016-04-07 08:40:27 -0500 )edit

Good to know thanks for the advice

sonyccd gravatar imagesonyccd ( 2016-04-07 08:56:25 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-04-09 03:30:14 -0500

gvdhoorn gravatar image

updated 2016-04-09 03:32:17 -0500

It seems that it's not linking to the library, but when i add

target_link_libraries(main /usr/include/libmodbus.so)

to the CMakeList.txt and try to compile i get:

(target_link_libraries):
   Cannot specify link libraries for target "main" which is not built by this project.

And that is because main is not a target that you define in your CMakeLists.txt.

You can only link libraries against things for which you have a add_executable(..) or a add_library(..) statement. In your case, that would be converter, tfTest and scaraListen.

From the error message you posted, it would appear that the main function you posted is in a file called scaraListen.cpp, so I'm guessing you should add the libmodbus.so library to the target_link_libraries(scaraListen ..) statement (which is similar to what @sonyccd posted).

PS: as I wrote in my comment, you might want to avoid hard-coding absolute paths to headers and libraries in your CMakeLists.txt.

edit flag offensive delete link more
2

answered 2016-04-07 08:59:03 -0500

updated 2016-04-07 09:00:50 -0500

All I did to compile was add this to the cmake.

target_link_libraries(ros_plc_modbus
   ${catkin_LIBRARIES}
   modbus
 )

and added this to the cpp file

#include <modbus/modbus.h>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-04-06 15:19:58 -0500

Seen: 1,193 times

Last updated: Apr 09 '16