0
$\begingroup$

Rosanswers logo

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!


Originally posted by Rydel on ROS Answers with karma: 600 on 2012-08-20

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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


Originally posted by weiin with karma: 2268 on 2012-08-20

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Rydel on 2012-08-21:
why would my_class need to be compiled into an executable?

Comment by weiin on 2012-08-21:
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.

Comment by weiin on 2012-08-21:
This explains a bit on the linking of headers: http://www.unix.com/programming/47881-help-compiling-header-files-c-file-using-gcc.html

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by SivamPillai with karma: 612 on 2012-08-20

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.