I want to use a c library for a piece of c code in a ros node [closed]

Hello, as the title says I'm trying to create a ROS node that have some lines of C code inside that depend to an external C library. I'm getting errors in the console like: getvec (a particular function of that library) reference not defined. I think that my node doesn't read properly the library, am I right? I have included .so and .h files in the include folder of the package in which is my node, as suggested by some experienced ROS users, they told me that is enough to define a library (of course i used #include in the .cpp file). Maybe i have to modify also the CMakeLists.txt? Since i surfed some ROS wiki/answer pages and other sites that said so. Here is the code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>

int main(int argc, char **argv)
{

ros::init(argc, argv, "heart_rate_monitor");

ros::NodeHandle n;

int i;
WFDB_Sample v[2];
WFDB_Siginfo s[2];

if (isigopen("100s", s, 2) < 2)
exit(1);

for (i = 0; i < 10; i++) {
if (getvec(v) < 0)
break;
printf("%d\t%d\n", v[0], v[1]);

}
exit(0);

return 0;
}


edit retag reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Marcofon close date 2016-11-03 09:33:20.653739

Sort by » oldest newest most voted

"undefined reference" is a linker error, so it means that it does not find your *.so file, which is not surprising, since it does not search in the "include" directory for it (it's only for headers!)

https://cmake.org/cmake/help/v3.0/com...

 link_directories(/path/where/your/so/lies) 

But just the directory patch. without the *.so file.

As an alternative you can specify it at runtime (just to try it out). Before launching your node, run in the shell:

 export LD_LIBRARY_PATH=/path/where/your/so/lies 

You need to run it every time you open a new shell.

more

Thank you for your help, in further tries i modified my CMake to add the .so files. It was everything correct except one thing. I will post it as an answer! Thank you very much!!!

( 2016-11-03 09:30:10 -0500 )edit

I found the problem! In the CMakeLists.txt i used the correct name of my library (libwfdb). But, it seems that's the ROS logic, the compiler/linker put the suffix lib at every library file when it does the linking. In my case, since the "lib prefix" was already present because it's a part of the library name, the linking resulted in liblibwfdb, causing my errors! I simply deleted lib in my CMakeLists and all went fine! Thank you all for all your help! :)

Here is the lines of the CMakeLists.txt involving what I'm saying:

include_directories(include ${catkin_INCLUDE_DIRS}) FIND_LIBRARY(WFDB_LIBRARY wfdb /usr/local/lib/) add_executable(heart_rate_monitor src/heart.cpp) target_link_libraries(heart_rate_monitor${catkin_LIBRARIES} \${WFDB_LIBRARY} )

more