What directories contain a search request in C / C ++?

test.c:

#include "file.h"

In the above statement, which directories will be searched?

I assume that a directory search test.cwill be found, right?

But is that all?

By the way, what is the advantage of using a header file? Java does not require a header file ...

+3
source share
5 answers
  • #include <header_name>: standard include file: see standard paths (the system includes path settings for the compiler) first
  • #include "header_name": First find the current path, then enable the path (search paths for specific projects)

- . Java , java-- jar (). C- () .

Java . C ( ) lib ( dll).

, c-. ( c/cpp ), . , , , .

, , .

+7

. , gcc -I .

C ++, ( ) , . Java , .class( C - ), , .

+2

#include , #include, , (#include) . , /I, , INCLUDE.

, , /I, , INCLUDE.

0

strace, .., . , foo.c #include "foo.h". CYGWIN :

strace /usr/bin/gcc-4.exe foo.c | grep 'src_path.*foo.h,' | sed 's/.*src_path //;s/foo.h.*//'

:

foo.c:1:22: error: foo.h: No such file or directory

/home/Joe/src/utilities/
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include-fixed/
/usr/include/
/usr/include/w32api/

, : , , SunOS 4 , .

, , foo.c . . Aslo http://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html CPATH C_INCLUDE_PATH. . ( , CYGWIN ).

: - cpp -v ( gcc -v). :

COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-pc-cygwin/4.3.4/cc1.exe -E -quiet -v -D__CYGWIN32__ -D__CYGWIN__ 
      -Dunix -D__unix__ -D__unix -idirafter /usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../..
       /include/w32api -idirafter 
       /usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/lib/../../include/w32api - 
       -mtune=generic -march=i686
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/include"
ignoring duplicate directory "/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/lib/../../include/w32api"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-cygwin/4.3.4/include
 /usr/lib/gcc/i686-pc-cygwin/4.3.4/include-fixed
 /usr/include
 /usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../include/w32api
End of search list.
0

++ , . ++, , #include "somefile.h":

 # include "q-char-sequence" new-line

causes the directive to be replaced throughout the contents of the source file, identified by the specified sequence between the delimiters. The named source file was searched in a specific way. 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.

Thus, it is directory lookups that depend on your specific C ++ implementation.

0
source

Source: https://habr.com/ru/post/1749628/


All Articles