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

Executable built from multiple source files? [closed]

asked 2014-07-22 12:52:43 -0600

mysteriousmonkey29 gravatar image

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/... 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 <device>.cpp, and another labeled <device>_node.cpp. Then they both build their executable from both cpp files, using the line

add_executable(<device>_node src/<device>_node.cpp src/<device>.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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by mysteriousmonkey29
close date 2014-07-23 11:24:07.210066

Comments

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.

McMurdo gravatar image McMurdo  ( 2014-07-23 01:40:08 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-07-22 15:23:46 -0600

bvbdort gravatar image

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)
edit flag offensive delete link more

Comments

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 <device>.h file)

mysteriousmonkey29 gravatar image mysteriousmonkey29  ( 2014-07-22 15:43:51 -0600 )edit

Dividing code into multiple files is for organization, reusable and for efficient compiling.

bvbdort gravatar image bvbdort  ( 2014-07-22 16:56:43 -0600 )edit

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

mysteriousmonkey29 gravatar image mysteriousmonkey29  ( 2014-07-22 17:15:59 -0600 )edit

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

bvbdort gravatar image bvbdort  ( 2014-07-22 17:50:46 -0600 )edit

Ok, thanks!

mysteriousmonkey29 gravatar image mysteriousmonkey29  ( 2014-07-23 11:23:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-07-22 12:52:43 -0600

Seen: 6,762 times

Last updated: Jul 22 '14