Ask Your Question

Undefined reference to functions in header class? [closed]

asked 2014-07-24 14:51:27 -0600

randylewis7 gravatar image

updated 2014-07-25 13:08:24 -0600

Hello, I'm currently attempting to convert a C++ driver demo program that came with a hardware system my boss purchased into a C++ ROS node that will do pretty much the same thing (except within ROS), which is to connect to a smart motor and laser scanner via USB and ethernet connections respectively, and then publish the scans continually while rotating the scanner back and forth (the original program saves the scans to a log file, and I will eventually change this to publishing ROS scan_msgs but have not gotten around to this yet). Right now I am running into some strange compiler errors that basically say "undefined reference to <every function="" from="" the="" classes="" in="" the="" imported="" header="" files="">". Here is the code/compiler errors:

CMakeLists: (there are a couple other files in this project; I kept them in case their CMake stuff is screwing up this particular node. sweepLaserInterface is the one I'm talking about here) Node: (not yet modified from original except for the include statements because I moved the header and other cpp files into the include folder) Errors:

Any ideas as to what could be going wrong? This program was working perfectly until I tried to repackage it as a ROS node.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by randylewis7
close date 2014-07-25 15:21:17.762744

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-24 15:15:01 -0600

kmhallen gravatar image

You must link against the library that contains the code for those functions.

target_link_libraries(sweepLaserInterface_node ${catkin_LIBRARIES} some_library)


Include the code for those functions in the executable.

add_executable(sweepLaserInterface_node src/sweepLaserInterface.cpp src/motorInterface.cpp)
edit flag offensive delete link more


This totally sounds right, so maybe I am misimplimenting this solution, but this didn't solve the problem. I tried the first way (with target_link_libraries), but couldn't get the path right (not sure where it starts looking), so moved on to the second method (add_executable). My CMakeLists.txt now reads: #sweepLaserInterface node add_executable(sweepLaserInterface_node src/sweepLaserInterface.cpp include/sweepLaserInterface/sickLMS115Interface.cpp include/sweepLaserInterface/motorInterface.cpp) target_link_libraries(sweepLaserInterface_node ${catkin_LIBRARIES}) , but I am getting similar (but I don't think exactly the same) errors:

randylewis7 gravatar imagerandylewis7 ( 2014-07-24 15:58:44 -0600 )edit

#sweepLaserInterface node add_executable(sweepLaserInterface_node src/sweepLaserInterface.cpp include/sweepLaserInterface/sickLMS115Interface.cpp include/sweepLaserInterface/motorInterface.cpp) target_link_libraries(sweepLaserInterface_node ${catkin_LIBRARIES})

randylewis7 gravatar imagerandylewis7 ( 2014-07-24 15:59:17 -0600 )edit

Sorry, I guess you can't do the indenting thing in comments, so I recommented the CMakeLists contents for readability.

randylewis7 gravatar imagerandylewis7 ( 2014-07-24 16:00:52 -0600 )edit

please ask c++ questions on programming forums.

McMurdo gravatar imageMcMurdo ( 2014-07-24 16:35:39 -0600 )edit

Ok, I will next time. When I try to add the libraries to the target_link_libaries line, however, I get the error: /usr/bin/ld: cannot find -linclude/sweepLaserInterface/sickLMS115Interface.cpp /usr/bin/ld: cannot find -linclude/sweepLaserInterface/motorInterface.cpp, or just -l<whatever path="" i="" put="" in="" front="" of="" the="" libraries="">. Do you know what I could be doing wrong here? What directory does the target link libraries command start its search in?

randylewis7 gravatar imagerandylewis7 ( 2014-07-24 16:48:18 -0600 )edit

you should link it against a shared object. Not the cpp source.

McMurdo gravatar imageMcMurdo ( 2014-07-24 17:11:51 -0600 )edit

If you do not yet have the shared object/static object, then use method 2 from the answer.

McMurdo gravatar imageMcMurdo ( 2014-07-24 17:15:22 -0600 )edit

I'm not sure what you mean by linking it against a shared object (what exactly that would look like), but, as I mentioned, I already tried method 2, and it did not solve the problem. In fact, I believe you originally responded to that with "you have to do both," which you have now deleted and thus thoroughly confused me.

randylewis7 gravatar imagerandylewis7 ( 2014-07-25 11:23:08 -0600 )edit

Question Tools

1 follower


Asked: 2014-07-24 14:51:27 -0600

Seen: 2,152 times

Last updated: Jul 25 '14