Ask Your Question
1

ROS C++ custom class compiler and linker

asked 2012-08-20 08:25:09 -0500

Rydel gravatar image

updated 2014-01-28 17:13:24 -0500

ngrennan gravatar image

Hi, for some reason I cannot figure out how to get my c++ ros node to compile correctly with 2 source files and a header file. One source file is simply the main, and the other 2 files are a custom class, the header holding the function declarations and the .cpp containing the actual definitions (body)... pretty standard layout, any-who... where should I put the class .h and .cpp files to compile and link correctly and do I have to do anything special with CMakeLists?

Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-08-20 15:17:58 -0500

weiin gravatar image

What I usually do: cpp files are in src folder, h files in include/my_package folder

eg. for files:

src/main.cpp
src/my_class.cpp
include/my_package/my_class.h

CMakeLists.txt will then use:

rosbuild_add_executable(binary_name src/main.cpp src/my_class.cpp)

But there are many ways to achieve the same. You can look at how other packages are structured and choose the one that you like. OR you can look at REP122 to see how the file system hierarchy is done for ROS packages

edit flag offensive delete link more

Comments

why would my_class need to be compiled into an executable?

Rydel gravatar image Rydel  ( 2012-08-21 05:49:41 -0500 )edit

good question, I don't know the "correct" answer, but I do know the compiler complains if you don't. :) There are other ways like putting the class file into a library (as suggested by Sivam) or use -lpath flag. I think the bottomline is, the compiler needs to know where the class files are.

weiin gravatar image weiin  ( 2012-08-21 15:11:42 -0500 )edit
weiin gravatar image weiin  ( 2012-08-21 15:12:26 -0500 )edit
1

answered 2012-08-20 08:49:02 -0500

updated 2012-08-20 23:14:10 -0500

Lorenz gravatar image

Hi,

One simple technique would be to copy all the source files and header files in the "src" directory.

In CMakelists You'll have to add the following lines:

rosbuild_add_library(binary_name src/file_name_1)
rosbuild_add_executable(binary_name src/file_name_2)

file_name_1 is the cpp file that contains the class definition. file_name_2 is the cpp file containing the main() function. The header file, I believe would already be included in the cpp file containing class definition and hence is not required to be added in the Cmakelists.

This technique should most probably work with your scenario if I understood it right.

Good luck.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-08-20 08:25:09 -0500

Seen: 2,949 times

Last updated: Aug 20 '12