views:

145

answers:

4

Is there any difference between #include "./test.h" and #include "test.h" for C/C++ preprocessor?

+4  A: 

No, there is no difference.

You could also have

#include "../thisdir/test.h"

And it would be the same

Tom
You would have to change all your #includes if you renamed the directory though
Jeffrey Aylesworth
And if I did "../../thisdir/test.h", it would be a nightmare :)
Tom
+1  A: 

According to the C standard, there is no difference: the compiler gets to specify how they are searched. In practice, there shouldn't be any difference, either, for any of the implementations I am aware of.

Lars Wirzenius
A: 

In my opinion there is an important difference.

In the case of #include "test.h" the include file is searched for in all directories specified to the compiler with the option -I.

In the case of #include "./test.h" only the residing directory of the referring file is used.

Juan Macek
Isnt the -I option used for adding "standard " directories to the include path ( that is, a directory that will be scanned on an angled bracket include )
Tom
@Tom, assuming we are talking about gcc, -I specifies directories to search for both "" and <> includes. You can use -isystem to specify paths that should only be searched for angle brackets.
Nick Meyer
Not true. Both will still search through all directories. Just that "" will search the local directories before looking at those specified with -I
sep
@Nick Meyer. Thanks, didn't know that.
Tom
+1  A: 

Both styles will be treated the same by the pre-processor. The standard practice is

#include "test.h"

and pass the include file path as an option to the compiler. (For instance, the -I option of GCC). This makes it easy to change the location of header files. You just need to make a single change in the project's make file.

Vijay Mathew