Ask Your Question
0

C++ undefined reference on constructor

asked 2014-10-26 21:12:23 -0500

kritchie gravatar image

I'm trying to catkin_make my ROS package and I always get this error :

Linking CXX executable devel/lib/sbg_ins_ig500n/sbg_ins_ig500n CMakeFiles/sbg_ins_ig500n.dir/src/node.cpp.o:
In function `main': /home[...]/src/sbg_ins_ig500n/src/node.cpp:24:
undefined reference to `SBGInsIg500N::SBGInsIg500N()' colle
ct2: error: ld returned 1 exit status make[2]: *** 
[devel/lib/sbg_ins_ig500n/sbg_ins_ig500n] 
Error 1 make[1]: *** [CMakeFiles/sbg_ins_ig500n.dir/all] Error 2 make: *** [all] Error 2
    *** Failure: Exit code 2 ***

Since it's an "undefined reference" error, I checked the methods names for any typo but found none and I also checked my code files to be sure the correct header files we're included and everything seems fine. This is the node.cpp code : (The error happens when I try to create an object from the class SBGInsIg500N). The class is located in a folder called "sbg_ins" which is a sub folder of my src directory in my package.

#include "sbg_ins/sbg_ins_ig500n.h"  
int main(int argc, char **argv){
    SBGInsIg500N s;
    ...

And this is the CMakeLists file :

cmake_minimum_required(VERSION 2.8.3)
project(sbg_ins_ig500n)

find_package(catkin REQUIRED COMPONENTS sensor_msgs)
find_package(Boost REQUIRED COMPONENTS system)
find_package(catkin REQUIRED COMPONENTS roscpp)
find_package(catkin REQUIRED COMPONENTS tf)

catkin_package(DEPENDS system_lib)

include_directories(${catkin_INCLUDE_DIRS} src/sbg_ins/)

add_executable(sbg_ins_ig500n src/node.cpp)

target_link_libraries(sbg_ins_ig500n ${catkin_LIBRARIES})

## C++11 enable for catkin_build
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")

Calling catkin_make on my workspace outputs the error above, is there something I'm doing wrong ?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-10-27 10:07:33 -0500

rmattes gravatar image

This is a basic compiling/linking issue, not really a catkin issue. The problem is that the implementation of "SBGInsIg500N::SBGInsIg500N()" is not being compiled. The prototype of the function (a reference to the actual function) is located in the header file, which lets your node's source compile. But the actual contents of the function, which are probably located in a corresponding cpp file, are not compiled and thus available at link time. The linker cannot find a suitable definition for the referenced constructor, and prints an error.

To make the function's definition available, you have to direct the buildsystem to compile the cpp file where the function is defined, and then link the compiled object file into your executable. You can do this in two ways:

  1. Change your call to add_executable to add the source file where the constructor is defined: add_executable(sbg_ins_ig500n src/node.cpp src/sbg_ins/<sbg_ins_file>.cpp)

  2. Compile your sbg_ins class into a shared or static library, and then link that library to your sbg_ins_500gn executable.

edit flag offensive delete link more

Comments

It worked ! Thx !

kritchie gravatar image kritchie  ( 2014-10-27 13:15:07 -0500 )edit
1

answered 2014-10-27 03:00:16 -0500

gvdhoorn gravatar image

updated 2014-10-27 03:05:07 -0500

...
find_package(catkin REQUIRED COMPONENTS sensor_msgs)
...
find_package(catkin REQUIRED COMPONENTS roscpp)
find_package(catkin REQUIRED COMPONENTS tf)
...

This is most likely not really the cause of your issue, but afaik multiple calls to find_package(catkin ..) will either not work (ie: the later calls won't actually do anything, as the result for catkin has already been cached), or will overwrite results of earlier invocations. Just list all your catkin dependencies after COMPONENTS in the first find_package(catkin ..).

catkin_package(DEPENDS system_lib)

Unless you have a specific reason for not doing it, this should also list all the catkin pkgs you depend on. So CATKIN_DEPENDS sensor_msgs roscpp tf.

include_directories(${catkin_INCLUDE_DIRS} src/sbg_ins/)

Minor, but it is convention to list the include dir of the current package before any other includes. That would work around identically named headers existing in other packages getting resolved before your own.

edit flag offensive delete link more

Comments

I will look into that ! Thank you very much !

kritchie gravatar image kritchie  ( 2014-10-27 13:16:07 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-10-26 21:12:23 -0500

Seen: 9,312 times

Last updated: Oct 27 '14