How to tell g++ compiler where to search for include files?

后端 未结 4 1739
难免孤独
难免孤独 2020-12-01 23:38

In a \"working directory\" I have a lot of *.cpp and *.h files that #include each other and files from subdirectories.

For example:

#inc         


        
4条回答
  •  时光说笑
    2020-12-02 00:01

    As you've already been told, it's useful to read the manual - specifically this chapter - and even more specifically right here.

    Specifically, you want

    g++ -I/root/workingdirectory -I/root/workingdirectory2
    

    Note also the documentation on #include directive syntax, described here as:

    2.1 Include Syntax

    Both user and system header files are included using the preprocessing directive #include. It has two variants:

    #include 
    

    This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).

    #include "file"
    

    This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for . You can prepend directories to the list of quote directories with the -iquote option. The argument of #include, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus,#include specifies inclusion of a system header file named x/*y.

    However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes. (Some systems interpret \ as a pathname separator. All of these also interpret / the same way. It is most portable to use only /.)

    It is an error if there is anything (other than comments) on the line after the file name.

    So for example

    #include "first.h"
    

    will start looking in the same directory as the .cpp file containing this directive (or take a relative path as relative to this directory).

    If you want to use the include path (specified by -I) you should use

    #include 
    

    Usual practice is to use the #include "local.h" form for headers inside a library/package/module (however you've chosen to organize that), and the #include form for headers from external/3rd-party or system libraries.

提交回复
热议问题