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

How to succesfully "catkin_make" with files that include header files (C++)?

asked 2013-07-11 08:29:52 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

I recently started using ROS/catkin and learning to code in c++ and am very lost. I have gone through the tutorials and googled many things but am still confused, especially with what to put in CMakelist.txt in order for "catkin_make" to work. I coded a very simple example just to see if I could build ("catkin_make") successfully, including: main.cpp, add.h, add.cpp.

This is the error I get when I run "catkin_make":

Linking CXX executable /home/elim323/catkin_ws/devel/lib/shortest_path/shortest_path
CMakeFiles/shortest_path.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `add(int, int)'
collect2: ld returned 1 exit status
make[2]: *** [/home/elim323/catkin_ws/devel/lib/shortest_path/shortest_path] Error 1
make[1]: *** [shortest_path/CMakeFiles/shortest_path.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed

The code for each file is as follows:

//main.cpp:

#include <iostream>
#include "add.h"

int main() {
   using namespace std;
   cout << "The sum of 3 and 4 is " << add(3,4) << endl;
   return 0;
}

add.h:

#ifndef ADD_H
#define ADD_H

int add(int,int);

#endif

add.cpp:

#include "add.h"

int add(int a,int b) {
   int sum;
   sum = a + b;
   return sum;
}

They are all in the same folder (/home/catkin_ws/src/shortest_path/src).

My CMakelist.txt file is as follows:

cmake_minimum_required(VERSION 2.8.3)
project(shortest_path)

find_package(catkin REQUIRED)

catkin_package()

include_directories(include  ${catkin_INCLUDE_DIRS})

add_executable(shortest_path src/main.cpp)

I guess my main questions are: what do I need in the CMakelist.txt file in order to build successfully? Are there standard things I should be putting in the CMakelist.txt file when coding in c++?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
7

answered 2013-07-11 09:28:51 -0600

thebyohazard gravatar image

The compiler isn't compiling add.cpp because it doesn't know it needs to, and thus it can't find the definition of add(int, int). You need to make add.cpp a source file in cmake. You would do that in add_executable. It would look like this:

add_executable(shortest_path src/main.cpp src/add.cpp)

You should also probably put your add.h file in the include folder, especially if you want other nodes to be able to see add.h. Keep going through the tutorials!

edit flag offensive delete link more

Comments

Thank you =)

elim323 gravatar image elim323  ( 2013-07-11 10:03:33 -0600 )edit
0

answered 2013-07-11 09:28:20 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

You need all the source files in the add_executable. You are missing src/add.cpp.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-11 08:29:52 -0600

Seen: 7,008 times

Last updated: Jul 11 '13