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

Adding header files to a node

asked 2015-03-10 12:09:29 -0500

nouf gravatar image

updated 2015-03-11 07:57:53 -0500

Hello

I'm trying to add C++ header files to a ROS node but when I do catkin_make it gives the following error:

    CMakeFiles/hello.dir/src/hello.cpp.o: In function `chatterCallback(boost::shared_ptr<std_msgs::String_<std::allocator<void> > const> const&)':
hello.cpp:(.text+0x13): undefined reference to `sleepms(int)'
hello.cpp:(.text+0x3b): undefined reference to `RoboteqDevice::SetCommand(int, int, int)'
hello.cpp:(.text+0xa4): undefined reference to `RoboteqDevice::Disconnect()'
CMakeFiles/hello.dir/src/hello.cpp.o: In function `main':
hello.cpp:(.text+0x201): undefined reference to `RoboteqDevice::Connect(std::string)'
CMakeFiles/hello.dir/src/hello.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x598): undefined reference to `RoboteqDevice::RoboteqDevice()'
hello.cpp:(.text+0x5a7): undefined reference to `RoboteqDevice::~RoboteqDevice()'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/nouf/catkin_ws/devel/lib/roboteq_motor/hello] Error 1
make[1]: *** [roboteq_motor/CMakeFiles/hello.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

The header files are under include/PKG_NAME

This is the .cpp node:

#include <iostream>
#include <stdio.h>
#include <string.h>

#include "roboteq_motor/RoboteqDevice.h"
#include "roboteq_motor/ErrorCodes.h"
#include "roboteq_motor/Constants.h"
#include <unistd.h>
#include <ros/ros.h>
#include <std_msgs/String.h>

using namespace std;
int status;
RoboteqDevice device;
void chatterCallback( const std_msgs::String::ConstPtr& msg){
  sleepms(10);

    cout<<"- SetCommand(_GO, 1, 1)...";
    if((status = device.SetCommand(_GO, 1, 700)) != RQ_SUCCESS)
        cout<<"failed --> "<<status<<endl;
    else
        cout<<"succeeded."<<endl;

    device.Disconnect();
 }
int main(int argc, char *argv[])
{

ros::init(argc, argv, "hello");
ros::NodeHandle nh;
    string response = "";

    status = device.Connect("/dev/ttyACM0");

    if(status != RQ_SUCCESS)
    {
        cout<<"Error connecting to device: "<<status<<"."<<endl;
        return 1;
    }

    //Wait 10 ms before sending another command to device

ros::Subscriber sub = nh.subscribe("chatter", 1000, chatterCallback);
ros::spin();
    return 0;

This is CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(roboteq_motor)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)
catkin_package(
 INCLUDE_DIRS include/${PROJECT_NAME}/
  LIBRARIES roboteq_motor
  CATKIN_DEPENDS roscpp std_msgs
  DEPENDS system_lib
)
include_directories(include ${catkin_INCLUDE_DIRS})
 install(DIRECTORY include/${PROJECT_NAME}/
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  FILES_MATCHING PATTERN "*.h"
  PATTERN ".svn" EXCLUDE
 )
add_executable(hello src/hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})

SOLUTION: I changed this line

add_executable(hello src/hello.cpp)

to this

add_executable(hello src/hello.cpp src/RoboteqDevice.cpp)

THANK YOU ALL

edit retag flag offensive close merge delete

Comments

Btw: these are linker errors (missing symbols), not compiler errors (missing includes).

gvdhoorn gravatar image gvdhoorn  ( 2015-03-11 04:57:34 -0500 )edit

What are the missing symbols?

nouf gravatar image nouf  ( 2015-03-11 05:36:29 -0500 )edit

hello.cpp:(.text+0x3b): undefined reference to `RoboteqDevice::SetCommand(int, int, int)'

etc. Those are symbols the linker cannot find. They are not errors from the compiler.

gvdhoorn gravatar image gvdhoorn  ( 2015-03-11 05:54:04 -0500 )edit

so what do you suggest as a solution?

nouf gravatar image nouf  ( 2015-03-11 06:49:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-03-11 02:28:33 -0500

Wolf gravatar image

I guess you need to change

target_link_libraries(hello ${catkin_LIBRARIES})

for

target_link_libraries(hello ${catkin_LIBRARIES} roboteq_motor )

.

Note that:

catkin_package(
 INCLUDE_DIRS include/${PROJECT_NAME}/
  LIBRARIES roboteq_motor
  CATKIN_DEPENDS roscpp std_msgs
  DEPENDS system_lib
)

makes packages that depend on this package implicitly linking their executables / libs against the roboteq_motor library but for the execs/libs in this you still need to add the lib to target_link_libraries

edit flag offensive delete link more

Comments

1

@Wolf: there is no add_library(..) anywhere in the CMakeLists.txt, so hello cannot be linked against it. Ifroboteq_motor is an actual library, but is external, then the roboteq_motor pkg should be added to the find_package(catkin ..). Otherwise, add the .cpp to add_executable(..).

gvdhoorn gravatar image gvdhoorn  ( 2015-03-11 02:53:27 -0500 )edit

roboteq_motor is not a library. It's the current package name

nouf gravatar image nouf  ( 2015-03-11 05:37:11 -0500 )edit
1

Then LIBRARIES roboteq_motor in your CMakeLists.txt up there makes no sense! Anyway you need to define the functions declared in your roboteq_motor/RoboteqDevice.h etc. headers anywhere and have to link this against your hello.cpp (as other src of lib)

Wolf gravatar image Wolf  ( 2015-03-11 07:18:43 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-10 12:09:29 -0500

Seen: 3,132 times

Last updated: Mar 11 '15