Ask Your Question
0

Double quotes vs angled brackets for "ros/ros.h"

asked 2020-04-07 20:43:44 -0500

here2infinity gravatar image

updated 2020-04-08 05:36:51 -0500

I am unclear why "ros/ros.h" isn't in angled brackets like <std_msgs>. They both come from ROS, don't they and are installed at the same time?

#include "ros/ros.h"
#include "simple_arm/GoToPosition.h"
#include <std_msgs/Float64.h>

EDIT for clarification:

I understand the convention in C++ of local vs system headers and the search path, but what is local of ros/ros.h? I didn't write it and it came with the installation. The same with std_msgs/Float64.h.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-04-08 12:50:37 -0500

Mark Rose gravatar image

As pointed out elsewhere, this is a C/C++ question rather than a ROS question. The difference between the two styles of #include specify different directory search strategies:

  • Using angle brackets (#include <iostream>) asks the C/C++ preprocessor to look for the given path only in directories in the configured include search path. (In particular, the current directory is not searched.)
  • Using double quotes (#include "iostream") asks the C/C++ preprocessor to look for the given path relative to the current directory and then try directories in the configured include search path. The standards are specific about this, so you can think of using double-quotes as searching a superset of locations.

The default include search path is set by the compiler, and can be altered by environment variables or command-line options (-I, for example). The C/C++ standards use more vague phrasing like "an implementation-defined manner" for the include search path. See your compiler's documentation for specifics. The default include search path differs, typically, between C and C++ compiles. (So using g++ rather than gcc on the command line will give different results, for example.)

It's conventional to use angle brackets for includes provided by the system or libraries and double-quotes for your source code. (But the distinction between "your source" and "libraries" also depends on how you've set up your project.) It seems to me more conventional to use #include <ros/ros.h> rather than double-quotes.

edit flag offensive delete link more

Comments

1

Please note that AFAIK, what you describe is not actually a standard, or prescribed behaviour, but a convention (which admittedly many (all?) popular compilers adhere to).

gvdhoorn gravatar image gvdhoorn  ( 2020-04-08 12:55:00 -0500 )edit
1

Yes, you're correct (though, as you mention, I don't know of a popular compile for C/C++ that doesn't do it that way). The standard says that everything using angle brackets is implementation-defined, and that using double-quotes it first tries the path using the current directory and then uses the angle-bracket method (whatever it is).

Mark Rose gravatar image Mark Rose  ( 2020-04-08 15:29:04 -0500 )edit
1

answered 2020-04-08 04:08:40 -0500

gvdhoorn gravatar image

updated 2020-04-08 06:22:39 -0500

Without more context, there would be no good reason to #include the ros/ros.h header with quotes and the other(s) with <>.

Both will work.

A weak convention however is to use quotes for "local headers" and <> for headers from other libraries.

edit flag offensive delete link more

Comments

1

Note: this is not a ROS question, but a general C++ one.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-08 04:08:59 -0500 )edit

It is a ROS question. I clarified in the question with an edit.

here2infinity gravatar image here2infinity  ( 2020-04-08 05:36:44 -0500 )edit

I'm not sure how your edit changes the question.

"local to ros/ros.h" does not really make sense to me.

My answer would remain the same: both quotes and <> would eventually result in the same header being included, unless you happen to have another ros directory in the directory which stores the .cpp (or whichever file is #includeing ros/ros.h) and that directory also contains a ros.h.

But there is no actual C++ standard which requires preprocessors/compilers to follow this convention, so it's implementation defined.

Perhaps, if you could tell us where you found this code, it would provide more context and perhaps we could then say something more about why it was done with quotes in this case.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-08 06:18:04 -0500 )edit

The code is from an example from an old course. I think they made a mistake and weren't consistent with the angled brackets. By local, I meant that it was a file written by me and not something like <vector> that comes with C++.

here2infinity gravatar image here2infinity  ( 2020-04-08 06:53:22 -0500 )edit

There are style guides which prefer the use of quotes over <> for certain types of commits.

But without knowing whether the course authors / developers followed any of those, it'll be difficult to determine whether that would be the (main) reason here.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-08 07:01:33 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-07 20:43:44 -0500

Seen: 51 times

Last updated: Apr 08