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

I wont be able to create reference to Bag class: undefined reference to `rosbag::Bag::Bag()

asked 2017-07-04 04:51:16 -0600

Hridaynath gravatar image

updated 2017-07-05 04:20:35 -0600

I am using this tutorial. I just want to read the Bag file, but there is linker error: undefined reference to `rosbag::Bag::Bag()'

as per tutorial I added all header files (and those all are present in the place where they are supposed to.) The header files are included successfully. I checked it by :

cout<< rosbag::rosmode::Read<< endl;

which produce the output :

2

Now if I add the following line to source code

rosbag::Bag bag;

it raising an error:

undefined reference to `rosbag::Bag::Bag()'

I have one doubt: Is rosbag/bag.h is responsible to create reference of rosbag::Bag ??

Further I have seen ros.cpp here But this bag.cpp is not there in my system. (I tried to find it with command - locate bag.cpp)

Also I don't want to use any node, launch, workspace or package of ros to do so, As I want to run this in isolation. (I am not a very good c++ programmer, forgive me if I am doing any silly mistake. :) )

Any help would be appreciated. Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
5

answered 2017-07-07 02:42:10 -0600

gvdhoorn gravatar image

updated 2017-07-10 06:35:18 -0600

The error you quote is basically a linking error, meaning the linker can't find the library that provides the binary code for a particular symbol you use.

This is not a ROS problem.

In this case you're not linking against the rosbag libraries.

You'll need to make sure that you've added rosbag to the find_package(catkin COMPONENTS ..) (or a regular find_package(..)) line in your CMakeLists.txt, and that you've added the appropriate target_link_libraries(..) that links your executable against the rosbag library.

As this is all CMake, you'll want to keep the CMake Documentation at hand.


Edit:

@gvdhoorn I made CMakeLists.txt as you suggested: like below:

cmake_minimum_required (VERSION 2.6)
project (tutorial)
find_package(catkin REQUIRED rosbag)
set(SOURCES sampleOne.cpp)
add_executable(test ${SOURCES})
target_link_libraries(test)

Two things here:

  1. if you just need rosbag, there is no need for find_package(catkin ..), just use find_package(rosbag REQUIRED) directly.
  2. you have a target_link_libraries(..) in there, but you don't actually link your test binary against any libraries.

The last point is actually why you're still getting errors.

both source code and CMakelists.txt is in a directory named "cmake_example".

Now I ran

cmake .
make

Please don't use in-source builds with CMake.

which does not product executable named "test"

but if I remove first line of source code

rosbag::Bag bag;

it does create executable named "test", which I can happily execute.

Which makes sense, as you're not actually linking against any of rosbag's libraries.

Try the following:

cmake_minimum_required(VERSION 2.6)
project (tutorial)
find_package(rosbag REQUIRED)
include_directories(${rosbag_INCLUDE_DIRS})
add_executable(test sampleOne.cpp)
target_link_libraries(test ${rosbag_LIBRARIES})

That will probably work.

edit flag offensive delete link more

Comments

@gvdhoorn Thank you very much for your reply. But I am trying to work with ROS Bag file in Isolation (that is no workspace, no package) usig CPP API.

I can read and manipulate data of Bag file in python without any workspace and package.

Hridaynath gravatar image Hridaynath  ( 2017-07-07 06:46:57 -0600 )edit

similarly for some task since there is no python library exist, I have to work in cpp.

So my next question is it impossible to work in isolation (that is without workspace and package)?

Hridaynath gravatar image Hridaynath  ( 2017-07-07 06:49:23 -0600 )edit

I have read some of website but I am clueless.

Hridaynath gravatar image Hridaynath  ( 2017-07-07 06:49:57 -0600 )edit

I'm not sure what you are asking exactly. You don't need to have anything installed apart from the rosbag package, which can be build using just CMake. That should make both the C++ and the Python APIs available.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-07 07:08:03 -0600 )edit

@gvdhoorn I made CMakeLists.txt as you suggested: like below:

cmake_minimum_required (VERSION 2.6)
project (tutorial)
find_package(catkin REQUIRED rosbag)
set(SOURCES sampleOne.cpp)
add_executable(test ${SOURCES})
target_link_libraries(test)
Hridaynath gravatar image Hridaynath  ( 2017-07-10 06:19:02 -0600 )edit

I have following code in my "sampleOne.cpp" source file.

    rosbag::Bag bag;
    cout<<rosbag::bagmode::Read<<endl;
    cout<<"hello done with ros bag instance"<<endl;
    return 0;
Hridaynath gravatar image Hridaynath  ( 2017-07-10 06:20:50 -0600 )edit

both source code and CMakelists.txt is in a directory named "cmake_example". Now I ran

cmake .
make

which does not product executable named "test" but if I remove first line of source code

rosbag::Bag bag;

it does create executable named "test", which I can happily execute.

Hridaynath gravatar image Hridaynath  ( 2017-07-10 06:25:28 -0600 )edit

what I am missing.. I have almost trying for two days now. please help..

Hridaynath gravatar image Hridaynath  ( 2017-07-10 06:25:45 -0600 )edit
0

answered 2022-02-07 09:51:31 -0600

mahasamatman gravatar image

The accepted solution didn't work for me on ROS melodic. But including the rosbag_storage library in my project file worked. I'm posting this in case someone has a similar problem.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-07-04 04:51:16 -0600

Seen: 3,417 times

Last updated: Feb 07 '22