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

my main code cannot find my header file

asked 2017-05-27 21:16:02 -0500

tylernaes gravatar image

updated 2017-05-28 13:40:34 -0500

I have a package called "motor_driver" that has a 'src' folder and an 'include' folder along with the CMakeLists.txt and the Package.xml. In the 'src' folder I have a file called ego_points.cpp and in the 'include' folder i have a header file called motor_driver.h.

In the ego_points.cpp file, I include the motor_driver.h file but when I run catkin_make in my workspace I get the following error:

/home/student/Desktop/Naes_Thesis/src/motor_driver/src/ego_points.cpp:7:26: fatal error: motor_driver.h: No such file or directory  
    #include "motor_driver.h"
                             ^
    compilation terminated.
    make[2]: *** [motor_driver/CMakeFiles/motor_driver_node.dir/src/ego_points.cpp.o] Error 1
    make[1]: *** [motor_driver/CMakeFiles/motor_driver_node.dir/all] Error 2
    make: *** [all] Error 2
    Invoking "make -j8 -l8" failed

My CMakeLists.txt and my ego_points.cpp are provided below. Could someone please tell me what I am doing wrong? Thank you in advance.

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(motor_driver)

find_package(catkin REQUIRED COMPONENTS
  rplidar_ros
  roscpp
  serial
  std_msgs
  sensor_msgs
  message_generation
)

add_message_files(FILES
  Motor_speeds.msg
  cartesian.msg
)

generate_messages(DEPENDENCIES
  std_msgs
  sensor_msgs
  motor_driver
)

catkin_package(
     INCLUDE_DIRS include
     CATKIN_DEPENDS rospy roscpp serial std_msgs message_runtime rplidar_ros sensor_msgs
)

include_directories(
   /src
   /launch
   /include
   ${catkin_INCLUDE_DIRS}
)

add_executable(${PROJECT_NAME}_node src/ego_points.cpp)
target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES})

add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

install(TARGETS ${PROJECT_NAME}_node
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(DIRECTORY include 
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
   FILES_MATCHING PATTERN "*.h"
)

install(FILES
ego_points.launch
   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

ego_points.cpp:

#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "iostream"
#include "string"
#include "motor_driver/Motor_speeds.h"
#include "motor_driver/cartesian.h"
#include "motor_driver.h"

using namespace std;
using namespace motor_driver;

Points p1;

int main(int argc, char** argv) {
        ros::init(argc, argv, "motor_driver_node");
        ros::NodeHandle nh;
    ros::Subscriber sub;
    p1 = new Points();
    ros::Rate r(1);
        while (ros::ok) {
                sub = nh.subscribe<sensor_msgs::LaserScan>("/scan",10, &p1.ScanCallback);
        cout << "This is in the main loop" << endl;
        cout << "This is the first value of p1 scanned data: " << p1.scannedData[0] << endl; 
        r.sleep();
                ros::spin();
    }
    return 0;
}
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2017-05-28 13:13:41 -0500

gvdhoorn gravatar image

updated 2017-05-28 15:48:23 -0500

I think the bigger problem here is that the include path isn't setup correctly:

include_directories(
   /src
   /launch
   /include
   ...
)

these are all absolute paths, and I doubt the OP has his headers in a dir called include in the root of his partition.

This should be enough:

include_directories(
   include
   ${catkin_INCLUDE_DIRS}
)

The paths in your CMakeLists.txt are relative to the file itself. Since CMakeLists.txt is in the root of your package -- as is your include directory -- specifying include is enough.

edit flag offensive delete link more

Comments

Also: @tylernaes: src and launch are not supposed to be on the include path (src maybe in certain special situations, but not for a simple package/node).

gvdhoorn gravatar image gvdhoorn  ( 2017-05-28 15:49:34 -0500 )edit

Btw: whether the suggestion of @Haibo works depends on whether you created a subdir called motor_driver in your include directory. If not, then leave off the motor_driver/ prefix.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-28 15:49:54 -0500 )edit
0

answered 2017-05-27 23:47:49 -0500

Haibo gravatar image

You have a ros package called "motor_driver" and a header file under this package call "motor_driver.h". If that is the case, you should use

#include <package_name/header_name.h>

which is

#include <motor_driver/motor_driver.h>
edit flag offensive delete link more

Comments

Thank you @Haibo and @gvdhoorn for the responses. In regards to your suggestions:

@Haibo, I have tried using #include <motor_driver/motor_driver.h> and it still gives me the same error of "no such file or directory".

@gvdhoorn, I took /src /launch out. but how should I set up include_directories?

tylernaes gravatar image tylernaes  ( 2017-05-28 12:51:03 -0500 )edit

I do not have any folders in my include directory so I will leave it as #include </motor_driver.h>. Thank you for clarifying why I should not include the prefix.

Even after making the changes you suggested @gvdhoorn, It still will not find my header file when compiling.

tylernaes gravatar image tylernaes  ( 2017-05-28 13:30:09 -0500 )edit

Even after making the changes you suggested @gvdhoorn, It still will not find my header file when compiling.

no, and that is to be expected, as you keep prefixing everything with a /. That makes everything an absolute path.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-28 13:32:24 -0500 )edit

Oh. I'm sorry, I didn't see that you took the "/" out of "/include" in the include_directories(). That fixed my issue. Thank you so much @gvdhoorn!

I'm closing this question now.

tylernaes gravatar image tylernaes  ( 2017-05-28 13:40:19 -0500 )edit

We don't close questions here on ROS Answers. Rather, we mark them as answered by ticking the checkbox to the left of the answer that answered your question.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-28 15:47:47 -0500 )edit

I had the same issue and I was able to solve my catkin_make issue but my roslint is throwing me an error and asks me to include the include directory while declaring my header file. My include dir is alongside my src dir. .h file is inside include and .cpp file that uses the .h is inside src.

venkisagunner gravatar image venkisagunner  ( 2018-05-21 10:33:53 -0500 )edit

Question Tools

Stats

Asked: 2017-05-27 21:16:02 -0500

Seen: 8,328 times

Last updated: May 28 '17