# Unit 4: Unable to find 'topic\_ex/Age.h'

**URL:** <https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393>\
**Category:** ROS Basics In 5 Days (C++)\
**Created:** [March 12, 2024, 4:49pm UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393 "2024-03-12T16:49:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnab007](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arnab007](https://get-help.theconstruct.ai/u/arnab007)\
**Post date:** [March 12, 2024, 4:49pm UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/1 "2024-03-12T16:49:19Z")

</div>

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:

```auto
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).

```auto
#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:

```auto
[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`:

```auto
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.

---

<div class="post-metadata">

**Author:** ![girishkumar.kannan](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/girishkumar.kannan/32/6994_2.png) [@girishkumar.kannan](https://get-help.theconstruct.ai/u/girishkumar.kannan)\
**Post date:** [March 13, 2024, 3:54am UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/2 "2024-03-13T03:54:52Z")

</div>

Hi @arnab007 ,

I think your `include` statement syntax is wrong.

The line you have is:

> [@arnab007](#):
>
> `#include "topic_ex/Age.h"`

But it should be:

```cpp
#include <topic_ex/Age.h>

```

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

I hope this fixes your issue.

Regards,  
Girish

---

<div class="post-metadata">

**Author:** ![arnab007](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arnab007](https://get-help.theconstruct.ai/u/arnab007)\
**Post date:** [March 13, 2024, 1:04pm UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/3 "2024-03-13T13:04:04Z")

</div>

Hello Girish,

Thanks for your reply.

Regarding your suggestion, you will find ([link](https://s3.eu-west-1.amazonaws.com/notebooks.ws/cpp_for_robotics/unit2.html?AWSAccessKeyId=AKIAJLU2ZOTUFJRMDOAA&Signature=X6hWuA8166H5zL1ZKZqLYj1kua8%3D&Expires=1710349115#Code-explanation)) 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

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [March 14, 2024, 12:03pm UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/4 "2024-03-14T12:03:31Z")

</div>

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:

```auto
cd ~/catkin_ws/src
catkin_create_pkg my_new_pkg roscpp pkg_with_custom_msg

```

---

<div class="post-metadata">

**Author:** ![arnab007](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arnab007](https://get-help.theconstruct.ai/u/arnab007)\
**Post date:** [March 14, 2024, 1:54pm UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/5 "2024-03-14T13:54:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/10684_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [March 15, 2024, 9:44am UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/6 "2024-03-15T09:44:39Z")

</div>

> [@arnab007](#):
>
> the text-in-red `Note`

What does the note say?

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [March 20, 2024, 9:45am UTC](https://get-help.theconstruct.ai/t/unit-4-unable-to-find-topic-ex-age-h/29393/7 "2024-03-20T09:45:30Z")

</div>

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