ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to utilize *.so file in CMakeLists.txt?

asked 2017-10-28 23:45:01 -0500

I am very new to ROS and C++, and I apologize if my question is obvious.

I have looked over very many questions and answers and tried the code offered by the answers; I looked over the documentation at http://wiki.ros.org/catkin/CMakeLists... ; I moved files from /usr/local/lib to /opt/ros/indigo/lib; and I tried very many different lines of code in CMakeLists.txt and I still cannot make my code compile.

I am using Ubuntu 14.04, with Indigo and Catkin as the build tool.

The Error:

jimmy_state_publisher.cpp:(.text+0x310): undefined reference to `LJUSB_OpenDevice'
jimmy_state_publisher.cpp:(.text+0x81b): undefined reference to `LJUSB_Write'
jimmy_state_publisher.cpp:(.text+0x851): undefined reference to `LJUSB_CloseDevice'
jimmy_state_publisher.cpp:(.text+0x876): undefined reference to `LJUSB_Read'
lots more of the same type of error....

I am using a device called 'Labjack U3'. The driver for this device is called exodriver https://labjack.com/support/software/... . I downloaded the driver and installed it. It generated a liblabjackusb.so file in /usr/local/lib. All the undefined reference errors are methods/functions implemented in the driver source file. How do I modify my CMakeLists.txt to allow my only executable (jimmy_state_publisher.cpp) in the ROS package to use the liblabjackusb.so file? What modifications in the CMakeLists.txt file do I need to make for the package to compile?

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)

project(jimmy_v2)

find_package(catkin REQUIRED COMPONENTS roscpp rospy sensor_msgs std_msgs tf)

catkin_package()

include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(jimmy_state_publisher src/jimmy_state_publisher.cpp)

target_link_libraries(jimmy_state_publisher ${catkin_LIBRARIES})

I did #include "labjackusb.h" in the executable file that I created.

How do I modify the CMakeLists.txt file to take the labjackusb.so file into consideration? or if that is the wrong question, how do I build my project successfully?

If any information that you need is omitted I apologize.... simply let me know what else I need to include.

Thank you in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2017-10-29 02:09:27 -0500

gvdhoorn gravatar image

I have looked over very many questions and answers and tried the code offered by the answers;

When including a comment like this in your question, please always also include some links to those Q&As. Without that -- and what you've tried -- you run the risk of receiving the same advice/answer again, which wastes everyone's time.

Also: please always include information on your OS (ie: which version, how you installed it), your platform (ie: regular Intel PC? Embedded ARM board?) and your ROS installation (which version, how did you install it (using apt-get or built from sources using catkin?).

I moved files from /usr/local/lib to /opt/ros/indigo/lib

Please don't do that: until you understand things better, consider /opt/ros/* a no-go area.


General comment: this is not a ROS 'problem', but a CMake one. In a nutshell, you haven't told CMake to generate the Makefile statements that will cause the linker to link the labjack libraries with your program (ie: jimmy_state_publisher). As the library isn't linked, and the linker performs a final check at the end, it sees there are various symbols (ie: function calls) you're using that it can't find, so it gives you those errors.

As to your CMakeLists.txt:

include_directories(include ${catkin_INCLUDE_DIRS})

this tells CMake to add those directories to the include path (used to search for headers).

add_executable(jimmy_state_publisher src/jimmy_state_publisher.cpp)

this tells CMake to create a target that will result in the compilation of all source files you specify (*.cpp, in this case only one) which will make up the executable.

target_link_libraries(jimmy_state_publisher ${catkin_LIBRARIES})

But, your executable is using functions and variables not implemented in your own code, so it'll need to get those somewhere else. That is where libraries come in, and this last CMake statement tells CMake to make sure the executable target that we defined earlier will get linked to all the libraries that we specify (in this case the libraries contained in the variable catkin_LIBRARIES).

Now the issue is that you don't add (any of) the "Labjack U3" librar(y)(ies) here, so the linker will do its check and discover that functions like LJUSB_OpenDevice(..) and the others are being called in jimmy_state_publisher, but their implementations are nowhere to be found. And then it returns the error.

You'll have to add the "Labjack U3" librar(y)(ies) to the target_link_libraries(..) statement. It should look something like:

target_link_libraries(jimmy_state_publisher labjack_u3 ${catkin_LIBRARIES})

Where I obviously made up the name labjack_u3, you'll have to make sure you use the correct name.

Note that even though the file will most likely have a name like liblabjack_u3, you give CMake the name wihout the lib. It will add that for you.

edit flag offensive delete link more

Comments

1

See also #q246012 for a related question, and see catkin/C++ system lib deps for how to do this slightly nicer. But that depends on the Labjack software having the proper (pkgconfig) files.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-29 02:17:42 -0500 )edit

And if you've done all of the above, but still get the errors about missing symbols, you could be running into a C++ symbol mangling issue. See #q227146 in that case.

Note again that this is not a 'ROS problem' (I'm repeating that so you don't limit your searches to pages about ROS).

gvdhoorn gravatar image gvdhoorn  ( 2017-10-29 02:19:35 -0500 )edit

@gvdhoorn Thank you very much! Your explanation and answer are very helpful. Thank you for your time!

BuilderMike gravatar image BuilderMike  ( 2017-10-29 11:12:18 -0500 )edit

As well, once you add libraries in the project's CMakeLists.txt (as answer detailed), make sure you delete the devel, build directories, as well as the "global" CMakeLists.txt before running catkin_make!!!

BuilderMike gravatar image BuilderMike  ( 2017-11-14 06:47:32 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-28 23:45:01 -0500

Seen: 8,293 times

Last updated: Oct 29 '17