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

Why does my compiler warn at usage of operators of ros::TimeBase?

asked 2019-12-10 04:44:44 -0500

max11gen gravatar image

updated 2019-12-12 10:55:21 -0500

When I use ros::Time::operator<(ros::Time) like this:

ros::Time start=ros::Time::now();
// Some Processing
ros::Time end=ros::Time::now();
if(start>end) {} // Just an example...

I always get compiler warnings like

Warning: instantiation of function 'ros::TimeBase<ros::time, ros::duration="">::operator<' required here, but no definition is available. Add an explicit instantiation declaration to suppress this warning if 'ros::TimeBase<ros::time, ros::duration="">::operator<' is explicitly instantiated in another translation unit

Same thing happens for other operators like + or -. I'm not very experienced with template programming and I don't really know, what these warnings are supposed to tell me.
Now my questions are:
1) Am I using the operators wrong?
2) If not: Is it safe to use them this way?
3) If so: Would it be fine to just add those forward declarations to my header files as suggested by the compiler?

EDIT:
As requested by @Weasfas here a full example:

test_node.cpp:

#include <ros/ros.h>
#include <std_msgs/Bool.h>
#include <thread>

int main()
{
  ros::Time start(ros::Time::now());
  std::this_thread::sleep_for(std::chrono::seconds(1));
  ros::Time end(ros::Time::now());
  ROS_INFO("%d",start<end);
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(test_pack)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  sensor_msgs
  message_generation
  roslib
)
include_directories(
../../devel/include
${catkin_INCLUDE_DIRS}
)
add_executable(test_node src/test_node.cpp)
target_link_libraries(test_node ${catkin_LIBRARIES})

This code will trigger a warning as described before.

edit retag flag offensive close merge delete

Comments

Hi,

The problem is not about using it properly or not. The class ros::Time does not provide the ros::Time operators like <, >, +, -, * ...

You may want to use ros::Duration instead of ros::Time because ros::Time refers to a specific moment in time whereas ros::Duration refers to a period of time. Take a look at the ros::Timewiki.

Weasfas gravatar image Weasfas  ( 2019-12-10 09:17:56 -0500 )edit

@Weasfas I already had a look at ros::Time wiki. If you look under 1.4 line 3 you will see a call to ros::Time::operator+. Also looking at the API, that is linked in the wiki, you will see, that ros::Time indeed does have all those operators. The operators are inherited from ros::TimeBase.

max11gen gravatar image max11gen  ( 2019-12-10 09:25:25 -0500 )edit

@max11gen Are you sure you have the cmake and headers properly set up?. I am using a similar example and all is compiled and executed perfectly:

    ros::Time start=ros::Time::now();
    ros::Duration(2.0).sleep();
    ros::Time end=ros::Time::now();

    if(end > start)
    {
        ROS_INFO("Secs: %d ---- %d", start.sec, end.sec);           
    }
Weasfas gravatar image Weasfas  ( 2019-12-10 09:30:35 -0500 )edit

@Weasfas I think you missunderstood me. My code doesn't fail to compile, it compiles fine. I just get warnings (no errors) and I always try to keep my code free of warnings. Thanks for your efforts though.

max11gen gravatar image max11gen  ( 2019-12-10 09:39:39 -0500 )edit

@max11gen Ok, lets put it this way. I compiled your code, my code and I got no warnings, so it is something related to your set up. Tested in both: Kinetic and Melodic.

Weasfas gravatar image Weasfas  ( 2019-12-10 09:42:04 -0500 )edit

@Weasfas Ah ok. Well, I really have no idea where to search for problems in my setup, when actually everything compiles...

max11gen gravatar image max11gen  ( 2019-12-10 10:24:57 -0500 )edit

@max11gen Yes, that is a problem. Because I think that in your case the warning is important since you are using the output of the operator in a conditional clause and you have no clue of what the if is evaluating. Have you tried to use the CMakeList provided by the default catkin package generator? or even the ones provided in the roscpp tutorials. This is the only thing I come up with in order to troubleshoot that problem.

Weasfas gravatar image Weasfas  ( 2019-12-10 10:42:48 -0500 )edit

@Weasfas I build all my packages starting with the CMakeList provided by default catkin package generator. Then I iteratively comment in, whatever I need and extend the CMakeList. Building the package with the plain default is not an option now, since it doesn't set all dependencies that my packaged needs to be compiled.

max11gen gravatar image max11gen  ( 2019-12-11 03:40:30 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-08 09:45:57 -0500

merosss gravatar image

updated 2023-02-08 10:19:24 -0500

The same exact warning annoyed me for a while. In my case the warnings are coming from the CLang Code Model inspector (in QTCreator). I guess that what's happening is what the warning is suggesting, that the operator "is explicitly instantiated in another translation unit".

The only solution I found is to use forward declarations, here are some examples for -, < and >:

template<> ros::Duration ros::TimeBase<ros::Time, ros::Duration>::operator-(const ros::Time &rhs) const;
template<> bool ros::TimeBase<ros::Time, ros::Duration>::operator<(const ros::Time &rhs) const;
template<> bool ros::TimeBase<ros::Time, ros::Duration>::operator>(const ros::Time &rhs) const;
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-12-10 04:44:44 -0500

Seen: 440 times

Last updated: Feb 08 '23