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

Adding a cpp file to include in executable file

asked 2017-11-15 07:58:38 -0500

MadsFox gravatar image

New to CMake and trying to figure out how to include a cpp file that an executable file links to. Trying to program Object Oriented. I want to add a robot.cpp as a class file, so that i will be able to create a Robot object in my executable file.

The robot.cpp file at this location: include/manuscriptBuilder/robot.cpp

Made these modifications to the CMakeLists.txt:

  • under find_package i added:
    "robot"
  • under catkin_package i added:
    "DEPENDS robot"
  • include_directories( include ${catkin_INCLUDE_DIRS})
  • add_library(robot include/manuscriptBuilder/robot.cpp)
  • target_link_libraries(motor_command_pub_test_node ${catkin_LIBRARIES})

Enden up with this error message:

CMake Warning at /opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake:76 (find_package):

Could not find a package configuration file provided by "robot" with any of the following names:

robotConfig.cmake
robot-config.cmake

Add the installation prefix of "robot" to CMAKE_PREFIX_PATH or set "robot_DIR" to a directory containing one of the above files. If "robot" provides a separate development package or SDK, be sure it has been installed.

Call Stack (most recent call first):

ev3_ros/CMakeLists.txt:10 (find_package)

-- Could not find the required component 'robot'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.

CMake Error at /opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake:83 (find_package):

Could not find a package configuration file provided by "robot" with any of the following names:

robotConfig.cmake
robot-config.cmake

Add the installation prefix of "robot" to CMAKE_PREFIX_PATH or set "robot_DIR" to a directory containing one of the above files. If "robot" provides a separate development package or SDK, be sure it has been installed.

Call Stack (most recent call first):

ev3_ros/CMakeLists.txt:10 (find_package)

-- Configuring incomplete, errors occurred!

As i said i am new to CMake and catkin and C++ all together, so any tutorials to understand the CMakeLists.txt file and how to use it in relation to ROS would make me very happy.

edit retag flag offensive close merge delete

Comments

It has worked in another project, where i had a header file and a cpp file, with the same name. I just used add_library, added it as a dependence and required component.

MadsFox gravatar image MadsFox  ( 2017-11-15 07:58:36 -0500 )edit

Please provide the entire CMakeLists.txt file -- that will make it easier to debug. (Remove the boilerplate comments.)

clyde gravatar image clyde  ( 2017-11-15 10:41:04 -0500 )edit

As an aside, the typical layout is this:

~/catkin_ws/src/manuscriptBuilder/robot.cpp
~/catkin_ws/src/manuscriptBuilder/include/manuscriptBuilder/robot.h

If you follow this layout the tutorials will make more sense.

clyde gravatar image clyde  ( 2017-11-15 10:45:18 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-11-15 10:38:37 -0500

ninja777 gravatar image

updated 2017-11-15 11:55:02 -0500

find_package is for finding existing cmake packages, not the current project. Do not include 'robot' to find_package. same with Depends - these are the packages that your package is dependent on.

You need to create a library first and then link it to your executable. Assuming your sources for robot are stored in variable SOURCES and those of the executable are stored in NODE_SOURCES, the following snippet should do the trick. you can replace those variables with your source files.

add_library(robot ${SOURCES})
target_link_libraries(robot ${catkin_LIBRARIES})

add_executable(robot_node ${NODE_SOURCES})
target_link_libraries(robot_node  ${catkin_LIBRARIES} robot)
edit flag offensive delete link more
0

answered 2017-11-15 10:36:46 -0500

lucasw gravatar image

updated 2017-11-15 10:37:57 -0500

Don't do these unless you want to move robot.cpp into an entirely new package (that is named robot in it's package.xml):

under find_package i added: 
"robot"
under catkin_package i added: 
"DEPENDS robot"

But you do need to link to the robot library:

target_link_libraries(motor_command_pub_test_node robot ${catkin_LIBRARIES})

The add_library will create a librobot.so that is linkable within the same package (let's call it 'foo'). If a separate package needs it then it would list the package foo as a dependency in foo/package.xml and under find_package() and ${catkin_LIBRARIES} would contain it.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2017-11-15 07:53:12 -0500

Seen: 4,140 times

Last updated: Nov 15 '17