Ask Your Question
0

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

asked 2016-11-03 05:29:22 -0500

Marcofon gravatar image

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;
}

Thank you in advance for your help! :)

edit retag flag offensive 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

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-11-03 09:18:19 -0500

Dimitri Schachmann gravatar image

"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!)

You need to make the link directories known in your CMake:

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

So before adding your node target put

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

into your CMakeLists.txt

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.

edit flag offensive delete link more

Comments

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!!!

Marcofon gravatar image Marcofon  ( 2016-11-03 09:30:10 -0500 )edit
0

answered 2016-11-03 09:31:36 -0500

Marcofon gravatar image

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} )
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-11-03 05:29:22 -0500

Seen: 479 times

Last updated: Nov 03 '16