how to run libmodbus
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)
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.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.
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: 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.
Good to know thanks for the advice