? If I have my own library projects, what style should I use to #include headers from th...">

What are the rules for #include "xxx.h" Vs #include <xxx.h>?

If I have my own library projects, what style should I use to #include headers from them in my application? Are there strict rules, and do the two really have different meanings for the compiler / preprocessor or only for standards?

+4
source share
4 answers

There are several rules in accordance with the ISO standard. Both forms are implementation dependent, depending on where they look for header files. They do not even have to be files.

Section 2.9 in C ++ 11 makes no distinction between the two varieties, different from the fact that you can include " in the variant <> and > in the variant "" , but few will be stupid enough to use these characters in file names :-)

Section 16.2 further states:


The form preprocessing directive # include < h-char-sequence> new-line searches for a sequence of definitions defined for implementation for the header identified by the uniquely specified sequence between the separators < and > , and causes this directive to be replaced with the entire contents of the header. How the locations are indicated or the identified header is determined by the implementation.

The form preprocessing directive # include " q-char-sequence" new-line causes this directive to be replaced with all the contents of the source file identified by the specified sequence between the delimiters. " named source file is executed in a search method. If this search is not supported or if the search is not performed, the directive is processed as if it were reading # include < h-char-sequence> new-line with an identical contained sequence (including> characters, if any) from the original directive.


I prefer to use <> for system headers and "" for my own headers, but only for personal preferences. I would like to note that the above C ++ 11 document states:

Note. Although the implementation can provide a mechanism for creating arbitrary source files for searching <> , in general, programmers should use the form <> for headers provided with the implementation, and the form "" for sources outside the control of the implementation.

This is not necessary, but it is a good idea.

+12
source

Usually, using quotation marks, you mean that the header files are located in relative positions in the project directory. On the other hand, if you use angle brackets, the compiler will expect your header file files to be standard. For example, /usr/include , /usr/local/include or any other default locations for your compiler.

In GCC , if you use the -I flag, angle brackets in the specified places will also be searched for it.

Example:

 $ gcc -Wall -I/path/to/my/library/include myfile.c 

So, if you have myfile.h in /path/to/my/library/include , you can use #include <myfile.h> in myfile.c source.

+2
source

This affects where the preprocessor searches for the included file. From MSDN:

"Cited form: this form instructs the preprocessor to search for included files in the same directory of the file that the #include statement contains, and then in the directories of any files that include (#include) this file. The preprocessor then searches the path specified by the / I, and then along the paths specified by the INCLUDE environment variable.

Form with angle brackets: this form instructs the preprocessor to search for included files first along the path specified by the / I compiler option, and then when compiling from the command line along the path specified by the INCLUDE environment variable. "

As an approximate guide, I use only quotation marks when I try to specify the path to the directory containing the C # include file in it. Otherwise, I just use angle brackets. As an example from my current project:

 #include <algorithm> // standard library headers #include <numeric> #include <stack> #include <boost/function.hpp> // third-party library headers #include <boost/lexical_cast.hpp> #include <common/io/util/LineIO.h> // specified relative to my own base include dir #include "PartitionForest.h" // a header in the current directory 
0
source

After using dozens of compilers on several different operating systems, I recommend using <xh> only for headers related to the system and operating system, and "yh" for everything else, including your libraries and project headers.

Then you configure the appropriate inclusion search paths using your -I compiler option (or something else). This is easier if you use something like make or ant to complete your builds.

For third-party software titles, you can use any form. If the package is installed and accessible to all users (for example, somewhere like /usr/local/bin or /usr/site/bin ), the form <xh> is probably more correct. If it is installed in your local assembly tree, the form "yh" more accurate, since it is controlled during the assembly process.

This combination is the most portable.

0
source

All Articles