ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org

# Multiple files in one add_executable

I refer to this CMakeLists.txt

I am confused with the add_executable there. For example:

add_executable(drone_stateestimation ${STATEESTIMATION_SOURCE_FILES}${STATEESTIMATION_HEADER_FILES})


The STATEESTIMATION_SOURCE_FILES and STATEESTIMATION_HEADER_FILES refers to multiple files:

set(STATEESTIMATION_SOURCE_FILES
src/stateestimation/GLWindow2.cc
src/stateestimation/main_stateestimation.cpp
....


and

set(STATEESTIMATION_HEADER_FILES
src/stateestimation/GLWindow2.h
src/stateestimation/MouseKeyHandler.h
src/HelperFunctions.h
....


My QUESTIONS:

I don't understand how they work. The case that I know (from my limited experience) so far is just add_executable( node_name file.cpp).

1. If there are multiple files referring to one node, which code is executed when we run that node?

-----.

PS: I saw from the catkin wiki page here as well on point 7.4 "This will build a target executable called myProgram which is built from 3 source files: src/main.cpp, src/some_file.cpp and src/another_file.cpp."

But I don't understand. Like when we call that node , which file is executed? are they all executed? which order?

edit retag close merge delete

Sort by » oldest newest most voted

1 . If there are multiple files referring to one node, which code is executed when we run that node?

The code that is part of the main entry point, which is by default: int main(int argc, char *argv[]).

Note that this is no different from how you'd specify a target in (GNU) Make: it's perfectly valid for binaries to consist of multiple object files.

2 . Why do we add the header files to the add_executable? I never saw this before.

This is often done for CMake generators that generate projects for tools / IDEs that don't show all files by default (in a project explorer view fi), but only those files that are "part of" a target. Qt Creator is one of those, as is Visual Studio.

Note: these questions are not ROS specific: your first question is basic CMake / Make, the second is a 'trick' (and listed as one of the CMake antipatterns (but read the Errata section at the bottom of the page).

Edit:

But I don't understand. Like when we call that node , which file is executed? are they all executed? which order?

Please read the link to the wikipedia article about entry points. The question "which file is executed" doesn't really make sense in the context of compiled languages and object code (as there isn't necessarily a 1-to-1 correspondence).

more

@gvdhoorn thanks for the answer. I'm quite new, so I'm also not rlly familiar with (GNU) Make that you mentioned.

Why don't we put the other files that is not main entry point in either add_dependencies or include or include_dir instead?

( 2016-09-26 03:37:29 -0600 )edit

re: (GNU) Make: that was just an example: any build tool that supports building C(++) binaries will work the same.

re: add_dependencies(..): that is used for completely different things (namely: to express dependencies between CMake targets, not source files).

( 2016-09-26 03:41:36 -0600 )edit

And include paths are again something different: those contain paths not files, and nothing gets compiled, so that doesn't help when building an executable binary.

( 2016-09-26 03:42:26 -0600 )edit

We specify the other files , because they contain the things (variables/functions) needed in the main function.

That is it exactly.

( 2016-09-26 03:43:04 -0600 )edit

Thank you so much for the explanation

( 2016-09-26 03:47:27 -0600 )edit

Hi sorry, I read from the web that you put. I don't really understand this

Certain IDEs such as Qt Creator and Visual Studio will only display files that belong to a target. Thus headers need to be added in the source list or otherwise they can’t be edited.

( 2016-09-26 04:37:59 -0600 )edit

-What does "display files" mean? Does it mean displaying in the IDE directories?

-"otherwise they can't be edited" -> because they are not displayed in the IDE directories? but aren't we always able to just find the .h file and open and edit it?

( 2016-09-26 04:40:28 -0600 )edit

It's a UI thing: Qt Creator will only list files in the project explorer if that file "belongs" to (ie: is related to) something. It only shows compilation targets, so the headers are added to the targets in order for them to show up in the IDE.

( 2016-09-26 04:41:08 -0600 )edit

I think I get it.

Among those multiple files, only one file has a main function.

We specify the other files , because they contain the things (variables/functions) needed in the main function.

So It is okay to have multiple files in one add_executable, as long as only one file has the main function.

more

## Stats

Seen: 6,776 times

Last updated: Sep 26 '16