Unit 4: Unable to find 'topic_ex/Age.h'

Hello,

While trying to publish to the custom message Age, I have run into a problem.

First of all, I have already successfully compiled a separate package to hold the custom message Age, as can be attributed by the below console output:

user:~$ rosmsg list | grep Age
topic_ex/Age

When I try to publish to a custom topic using this custom message (see code below), I see that the IDE is able to locate the Age.h header file (that had been generated earlier when I had compiled the package containing the custom message).

#include <iostream>
#include <list>
#include <map>
#include <ros/ros.h>
#include <string>
// Include the custom message types
// https://people.eng.unimelb.edu.au/pbeuchat/asclinic/software/ros_define_and_use_custom_message_types.html
#include "topic_ex/Age.h"

using namespace std;

int main(int argc, char **argv) {
  // Initialize a ros-node
  ros::init(argc, argv, "rosbot_age_node");

  // Initialize a publisher to publish the time
  ros::NodeHandle nh;
  ros::Publisher pub = nh.advertise<topic_ex::Age>("/rob_age", 1000);
  // At what rate should data be steamed into the new topic
  ros::Rate loop_rate(2);

  // Initialize a age-type variable to hold the data in
  topic_ex::Age rob_age;
  // Initialize the sub-variables with 0
  rob_age.days = 0;
  rob_age.months = 0;
  rob_age.years = 0;
  // A temp variable to hold the time from rosbot
  unsigned long int temp_time = 0;

  while (ros::ok()) {
    // Convert the time appropriately for their respective fields for Age
    // message
    rob_age.days = temp_time / 60;       // For minutes
    rob_age.months = rob_age.days / 60;  // For hours
    rob_age.years = rob_age.months / 24; // For days
    // Publish the message
    pub.publish(rob_age);
    ros::spinOnce();
    loop_rate.sleep();
    ++temp_time;
  }

  return 0;
}

However, when I tried to compile the publisher package, I got the following error on console:

[ 93%] Built target topic_ex_generate_messages
/home/user/catkin_ws/src/age_publisher_pkg/src/publish_age.cpp:9:10: fatal error: topic_ex/Age.h: No such file or directory
    9 | #include "topic_ex/Age.h"
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [age_publisher_pkg/CMakeFiles/publish_age.dir/build.make:63: age_publisher_pkg/CMakeFiles/publish_age.dir/src/publish_age.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:528: age_publisher_pkg/CMakeFiles/publish_age.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Invoking "make -j8 -l8" failed

, which is strange because the IDE affirmed that the header file exists at the mentioned location.

I realized later that maybe the CMake file needs to know that it should include the custom-message package topic_ex. So I made the following CMakeList:

cmake_minimum_required(VERSION 3.0.2)
project(age_publisher_pkg)

# In package.xml, you have to state them as build_depend.
find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  topic_ex # custom-message package
)

catkin_package(
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

add_executable(publish_age src/publish_age.cpp)

add_dependencies(publish_age ${publish_age_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

add_dependencies(publish_age topic_ex_generate_messages_cpp)

target_link_libraries(publish_age
  ${catkin_LIBRARIES}
)

With this, the publisher compiled.

This unit should be modified to include the following pieces of information:

  1. What format should the custom-message header file should be: #include "topic_ex/Age.h" → `#include “/.h”, and
  2. What changes must be made in the CMake file of the custom subscriber: add the <custom message package name> under find_package() and as build_depend in the package.xml file.

This is just for your information.

Hi @arnab007 ,

I think your include statement syntax is wrong.

The line you have is:

But it should be:

#include <topic_ex/Age.h>

You need to use angle brackets (chevrons) and not double quotes.

I hope this fixes your issue.

Regards,
Girish

Hello Girish,

Thanks for your reply.

Regarding your suggestion, you will find (link) that both approaches are valid for their respective use-cases. This is outlined in the free TheConstruct course C++ for Robotics, Unit 1, right after the end of End of Exercise 1.1.

Regards,

Arnab

Perhaps you are trying to do something that is ahead of the unit, because this concept is thought later in the course.

Were you asked to create your node in a new package or the same package (topic_ex) where you have Age.msg? That makes a world of difference.

If you need to use a (custom) message from another package, it’s of course necessary to include that package as a dependency, preferrably when creating the package:

cd ~/catkin_ws/src
catkin_create_pkg my_new_pkg roscpp pkg_with_custom_msg

Hello bayodesegun,

So, the only reason I tried that was because the example output was for a package named topic_ex. I was half-way through adding the message as part of another package when I came across the text-in-red Note.
Hence, I tried following the intended example as closely as possible. No point in trying something new with this concept if I can’t even get the associated example to work.

Regards,
Arnab

What does the note say?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.