gcc, vc compile output header file include order

g++ -H print header file contain order and validity

Author: dzqabc

blog: https://www.cnblogs.com/dongzhiquan/archive/2013/04/11/3013486.html

Published: 2013-04-11 00:24

g++ -H option

Sometimes in order to locate some problems, such as header file loop inclusion, which causes compilation problems, we need to know the order of header file inclusion.

In addition to normal compilation, g++ -H also prints the name of each header file. Each file is indented to indicate its depth in the include stack. Invalid precompiled header files will also be printed; invalid precompiled header files print...x, valid print...!

References & Examples

Similar compilation options in vc++

/showIncludes (https://docs.microsoft.com/zh-cn/cpp/build/reference/showincludes-list-include-files?view=vs-2019)

  • The output of the compiler includes a list of files, nested include files can display include files in the include file
  • grammar
    /showIncludes
    
  • Set this compiler option in the Visual Studio development environment
    • Open the project's "Property Pages" dialog box.
    • Click the "C/C++" folder.
    • Click the Advanced Properties page.
    • Modify the display include attributes.




gcc -E generates preprocessed files

Author: Heart Tian Jushi

blog: https://www.cnblogs.com/liuzhenbo/p/11030852.html

RSS: http://c.biancheng.net/view/2375.html

Published: 2019-06-16 10:58

-E option, keep the output file of the preprocessor

Before the C language code is handed over to the compiler, the preprocessor will perform some text replacement operations, such as macro expansion, file inclusion, and deletion of part of the code.

Under normal circumstances, GCC will not retain the output file of the preprocessing stage, that is, the .i file. However, you can use the -E option to keep the output file of the preprocessor for use in diagnostic code. The -E option instructs GCC to stop after preprocessing.

By default, the output of the preprocessor will be imported to the standard output stream (that is, the display), which can be imported to an output file using the -o option:

$ gcc -E circle.c -o circle.i  # 表示把预处理的结果导出到 circle.i 文件。

The -C option prevents the preprocessor from deleting comments in the file

Because the header file may be quite large, if the source file includes multiple header files, its preprocessor output may be complicated and difficult to read. It is helpful to use the -C option, which prevents the preprocessor from deleting comments in source and header files:

$ gcc -E -C circle.c -o circle.c

Note that this is uppercase -C, not lowercase -c. Lowercase -c means only compile without linking.

Common options in the GCC preprocessor stage

-Dname[=definition]

Before processing the source file, define the macro name first. The macro name must be undefined in the source file and header file. Use this option with the #ifdef name command in the source code to achieve conditional compilation. If an alternate value is not specified, the macro is defined as the value 1.

-Uname

If the macro name has been defined in the command line or GCC default settings, the definition of name is "cancelled". The -D and -U options will be processed according to the order in which they appear on the command line.

-Idirectory [: directory […]]

When including the required header files into the source code through the #include command, in addition to the system standard include directory, specify other directories to search for these header files.

-iquote directory[:directory[…]]

This is an option added in the recent GCC version. It specifies the search directory for header files specified in quotation marks instead of angle brackets in the #include command.

-isystem directory[:directory[…]]

This option specifies the search directory for system header files in addition to the standard system include directory, and the directory specified by it is searched in preference to the standard system include directory. The equal sign at the beginning of the directory description is regarded as a placeholder for the system root directory. You can use the -sysroot or -isysroot option to modify it.

-isysroot directory

This option specifies the system root directory when searching for header files. For example, if the compiler normally searches for system header files in the /usr/include directory and its subdirectories, this option will lead to the search in directory/usr/include and its subdirectories.

The -sysroot option uses a hyphen instead of i, which specifies a directory other than the system root directory for link library search instead of header file search. If isysroot is not available, sysroot specifies the directory for both the header file and the link library search.

-I- option

In newer versions of GCC, this option is replaced by -iquote. In the old version, this option was used to split all -Idirectory options on the command line into two groups. All directories with the -I option to the left of -I- are considered equivalent to using the -iquote option; this means that they only search for header file names that are quoted in the #include command.

All directories with the -I option to the right of -I- will be searched for the header file names in all #include commands, regardless of whether the file name is in quotation marks or angle brackets.

Moreover, if -I- appears in the command line, the directory including the source file itself is no longer automatically used as the directory for searching header files.

For include directories, the usual search order is:

  • The directory containing the specified source file (for file names enclosed in quotation marks in the #include command).
  • The directories specified with the -iquote option are searched in the order they appear on the command line. Search only for header file names that are quoted in the #include command.
  • The directories specified by -I selection are searched in the order they appear on the command line.
  • Use the directory specified by the environment variable CPATH.
  • The directories specified with the -isystem option are searched in the order they appear on the command line.
  • Use the directory specified by the environment variable C_INCLUDE_PATH.
  • The default include directory of the system.

More examples

Guess you like

Origin blog.csdn.net/hylaking/article/details/107063802