tags:

views:

135

answers:

5

test.c:

#include "file.h"

In the above statement, which directories will be searched ?

I suppose the directory where test.c locates will be searched, right?

But is that all?

BTW, what's the benefit to use a header file? Java doesn't require a header file...

+1  A: 

It's controlled by your compiler. For example, gcc has a -I option to specify the include path.

C and C++ prototypes are required so the compiler (distinguished from the linker) can make sure functions are being called with the correct arguments. Java is different in that the compiler uses the binary .class files (which unlike C are in a standard bytecode format with all type information preserved) to check that calls are valid.

Matthew Flaschen
But there should be some default searched paths, like where `stdlib.h` locates,right?
Your question asked about `"test.h"`, not `<test.h>`. Standard headers like stdlib.h use the bracket syntax (`<stdlib.h>`), and these headers are found in a default system path.
Matthew Flaschen
+6  A: 
  • #include <header_name>: Standard include file: look in standard paths (system include paths setup for the compiler) first
  • #include "header_name": Look in current path first, then in include path (project specific lookup paths)

The benefit of using a header file is to provide others with the interface of your library, without the implementation. Java does not require it because java bytecode or jar is able to describe itself (reflexion). C code cannot (yet) do it.

In Java you would only need the jar and have the correct use statement. In C you will (mostly) need a header and a lib file (or header and dll).

The other reason is the way c code is compiled. The compiler compiles translation units (a c/cpp file with all included headers) and the linker in a second step links the whole stuff. Declarations must not be compiled, and this saves time and avoid code to be useless generated for each compilation unit which the linker would have to cleanup.

This is just a general idea, I am not a compiler specialist but should help a bit.

jdehaan
What's standard path and why is reflexion impossible in C/C++?
There are system (and potentially compiler-dependent) paths where system headers are stored. For example, *nix systems tend to use `/usr/include/` among others
Dusty
The standard include path is system-specific, and in C++ isn't required to use the filesystem at all. The reason the compiler can't use the compiled libraries to check types is that type information isn't all preserved, and there is not a standard ABI (Application Binary Interface).
Matthew Flaschen
Impossible is nothing, but C/C++ standard doesn't come with attributes and a standardized typesystem. The objects you create do not per default have ways to get read if you do not know their types (info given by declarations) there are ideas how to implement this http://www.vollmann.com/pubs/meta/meta/meta.html but is not part of the standard.
jdehaan
java is different, but "import" statements could be interpreted (weakly) as the equivalent of a C include.
ShinTakezou
A: 

Using quotes after the #include instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

If you use the angle bracket form, it instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable.

Dusty
It is important to note that when using the angle brackets the compiler does *not* look in the current directory, unless the current directory is in the path. As a result of this, if you `#include <c:\mylib\a.h>` which does `#include <b.h>` (note no path) and b.h is in the same directory as a.h, `#include <b.h>` will fail if that directory is not in the path.
John Dibling
It is for this reason why I often rant that people should almost never use the angle brackets in #include statements in their own code, but this rant usually falls on deaf ears.
John Dibling
A: 
Joseph Quinsey
Edit: And more gcc documentaion: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html#Search-Path
Joseph Quinsey
A: 

The C++ Standard doesn't really say which directories should be searched. This is how the C++ Standard describes what happens when #include "somefile.h" is encountered:

A preprocessing directive of the form

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

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

# include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original directive.

So exactly what directories are searched are at the mercy of your specific C++ implementation.

anon
@Neil: On my machine, `-print-search-dirs` does not include any include directories.
Joseph Quinsey
@Joseph B*gger. You are right - I could have sworn it did that, but it only prints the executables and library search paths.
anon