0
$\begingroup$

Rosanswers logo

Hello, I am working on setting up a robot I have in ROS, using ROS drivers written by others. Specifically, I'm using an LMS151 laser scanner and an ax2550 motor controller, with the drivers found here:

https://github.com/clearpathrobotics/LMS1xx https://github.com/wjwwood/ax2550

While inspecting the code inside these projects, I noticed that they both have a similar format: a cpp file labeled .cpp, and another labeled _node.cpp. Then they both build their executable from both cpp files, using the line

add_executable(_node src/_node.cpp src/.cpp),

which is what I don't understand. How does building one executable from two files work? How does the compiler combine the two into one program? It must somehow work, because both of the authors do it, and I've gotten one of the nodes (the LMS1xx one to work already), but I don't understand how.


Originally posted by mysteriousmonkey29 on ROS Answers with karma: 170 on 2014-07-22

Post score: 0


Original comments

Comment by McMurdo on 2014-07-23:
As an aside, even the simple ros hello-world talker/listener is not just one source file. In the true spirit it is at least 2-3 source files. The only difference is that here they have been already compiled and put into a nice shared object and you simply link them with your program.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

In building one executable from multiple files only one file will be having main() function and will be using/calling functions and classes defined in other files. All the files are compiled to object files and then linked to one executable.

add_executable(LMS1xx_node src/LMS1xx_node.cpp src/LMS1xx.cpp)

In this main is defined in LMS1xx_node.cpp and using class LMS1xx defined in LMS1xx.cpp.

Note : dont be confused with executable name as fiile name it can be anything.

add_executable(exename src/aaaa.cpp src/bbb.cpp)

Originally posted by bvbdort with karma: 3034 on 2014-07-22

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by mysteriousmonkey29 on 2014-07-22:
Ok, that makes more sense. What's the point of doing this? Why wouldn't you just include the functions and classes in the LMS1xx_node.cpp file, or stick them in the included LMS1xx.h file? (both drivers also included a .h file)

Comment by bvbdort on 2014-07-22:
Dividing code into multiple files is for organization, reusable and for efficient compiling.

Comment by mysteriousmonkey29 on 2014-07-22:
Ok, but then why not just include all of these LMS1xx.cpp methods in the header .h file? I guess what I don't get is the double division; LMS1xx_node.cpp, LMS1xx.cpp, and LMS1xx.h

Comment by bvbdort on 2014-07-22:
Its good practice to put declarations in .h files and definiations in .cpp files. #include "file.h" will be replaced with content of ".h" file and separatingcode into .h and c.pp will reduce the build time. Hope this image make things clear https://secweb.cs.odu.edu/~zeil/cs333/website-s12/Lectures/cppProgramStructure/page/compileWithPreProc.gif

Comment by mysteriousmonkey29 on 2014-07-23:
Ok, thanks!

$\endgroup$

Your Answer

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