0
$\begingroup$

Rosanswers logo

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.


Originally posted by Hridaynath on ROS Answers with karma: 65 on 2017-07-04

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2017-07-07

This answer was ACCEPTED on the original site

Post score: 5


Original comments

Comment by Hridaynath on 2017-07-07:
@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.

Comment by Hridaynath on 2017-07-07:
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)?

Comment by Hridaynath on 2017-07-07:
I have read some of website but I am clueless.

Comment by gvdhoorn on 2017-07-07:
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.

Comment by Hridaynath on 2017-07-10:
@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)

Comment by Hridaynath on 2017-07-10:
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;

Comment by Hridaynath on 2017-07-10:
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.

Comment by Hridaynath on 2017-07-10:
what I am missing.. I have almost trying for two days now. please help..

Comment by gvdhoorn on 2017-07-10:
I would suggest you append to your original question text next time, instead of using a nr of comments. Comments aren't suited for these kind of things.

Use the edit button/link to update your original question in that case.

Comment by Hridaynath on 2017-07-10:
@gvdhoorn Thank you very much :) The issue is solved and I could go ahead now. (I learned something today.)

Use the edit button/link to update your original question in that case

Yes for sure I will keep this in mind next time.

Thank you very much :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.