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

ROS and CMake help with linking

asked 2015-01-12 15:04:11 -0500

TheHenshinger gravatar image

updated 2015-01-14 14:49:01 -0500

Ok, so I made a mistake by including .cpp files instead of the .h files of my classes in my project. The problem I am having is after fixing it to the right way, including the .h and not the .cpp, I am getting linking errors.

I am not sure if I am doing this right though but I am trying to link the classes to the project but keep getting a undefined reference when at the linking stage of cmake.

This is my CMAKE file:

cmake_minimum_required(VERSION 2.4.6)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)


#set the default path for built executables to the "bin" directory
#set the default path for built libraries to the "lib" directory

#uncomment if you have defined messages
#uncomment if you have defined services

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/ARDroneV3.cpp)
rosbuild_add_library(${PROJECT_NAME} src/Classes/Vec4/Vec4.cpp)
rosbuild_add_library(${PROJECT_NAME} src/Classes/Mat3/mat3.cpp)
rosbuild_add_library(${PROJECT_NAME} src/Classes/Vec2/Vec2.cpp)
#rosbuild_link_boost(${PROJECT_NAME} thread)
#target_link_libraries(example ${PROJECT_NAME})
rosbuild_add_executable(ARDroneV3 src/ARDrone.cpp src/CJoystick.cpp src/CFrame.cpp src/CControl.cpp)

Any help will be appreciated and please go easy, I am new to CMAKE so still trying to learn it. Thanks for any and all help.


The lab I'm at is using Fuerte on Ubuntu 12.04

The errors is a undefined reference to Mat3, Vec3, Vec4 and Vec2. If I do it the wrong way by including the .cop file it works buy doing the right way I get the undefined reference error while linking.

Update part duex I guess my description to my error was a bit ambigious, so this is one of the errors. You will get the point as it applies to all the classes that I created that were mentioned (Mat3,Vec3..and so on)

In function `CControl::Hover()':
/UAV/ARDroneV3/src/CControl.cpp:63: undefined reference to `Vec4<float>::setA(float const&)'

Update part Three

After some googling I found out it was how the compiler was handling templated classes. I went ahead and found this Solution. I went with answer one and it works fine since I know how the classes will be used.

After doing that I added the .cpp files to the rosbuld_add_excutable directive while commenting out the eronous things above. Works like a charm now.

rosbuild_add_executable(ARDroneV3 src/ARDrone.cpp src/CJoystick.cpp src/CFrame.cpp src/CControl.cpp src/Classes/Vec4/Vec4.cpp src/Classes/Mat3/mat3.cpp src/Classes/Vec2/Vec2.cpp src/Classes/Vec3/vec3.cpp)
edit retag flag offensive close merge delete



Could you please clarify a couple things for us? For example, it would be nice to know the exact linker error and the ros version. Please edit your question with this information. We can be much more helpful that way. :)

allenh1 gravatar image allenh1  ( 2015-01-12 18:31:01 -0500 )edit

I edited it, hope it helps.

TheHenshinger gravatar image TheHenshinger  ( 2015-01-13 16:59:56 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-01-14 02:50:24 -0500

ahendrix gravatar image

You're making this more complicated than it needs to be; you don't need to create a separate library for each file you want to compile.

You can compile all of your files into a single executable with just:

rosbuild_add_executable(ARDroneV3 src/ARDrone.cpp src/CJoystick.cpp src/CFrame.cpp src/CControl.cpp)

The rest of your target_link_libraries and rosbuild_add_library calls are unnecessary.

If that doesn't work, copy and paste the exact compile/link error into your question.

edit flag offensive delete link more


That doesn't work, I still get the linking errors. I edited my question with one of the errors. I am not going to post them all because they are all the same and there are quite a lot of them.

TheHenshinger gravatar image TheHenshinger  ( 2015-01-14 10:45:36 -0500 )edit

Yeah, you've found the correct solution; templated classes should be implemented in header files; not in cpp files.

ahendrix gravatar image ahendrix  ( 2015-01-14 22:38:46 -0500 )edit

Question Tools

1 follower


Asked: 2015-01-12 15:04:11 -0500

Seen: 204 times

Last updated: Jan 14 '15