Properly use #include <> and #include ""

Hans Meine hans_meine at gmx.net
Fri Jun 15 15:41:39 BST 2007


Am Freitag, 15. Juni 2007 16:21:39 schrieb Michael Olbrich:
> Please don't use "" for _any_ installed header. This can result in very
> confusing errors.

This is the first time I hear that.

> ----- /usr/include/a/foo.h --------
> #include "bar.h"
OK, clearly the programmer meant /usr/include/a/bar.h here.

> ----- test1.c ---------------------
> #include <foo.h>
> ...
> -----------------------------------
>
> ----- test2.c ---------------------
> #include <bar.h>
> #include <foo.h>
> ...
> -----------------------------------
>
> compiling with -I/usr/include/b/ -I/usr/include/a/ gets different
> results for test1.c and test2.c. That is imho wrong.
Why?  test2.c and test1.c *are* different.

I am not sure what "error" should happen here (I only see an extra include 
which probably populates the namespace), but maybe that is because you did 
not tell us about the include guards within the header files.  Something 
strange may happen if both foo.h's use the same include guards, but then they 
probably come from the same library, and you should have only one of the dirs 
in the include path (and /usr/include/b/foo.h would exist, too).

Normally, using #include "..." is the preferred way to include header files 
from the same library AFAIK, since you know exactly which file you mean.
A definite advantage is that it does not matter whether the program does
#include <QtGui/QWidget> and is compiled with -I$(QTDIR)/include or whether it 
uses #include <QWidget> together with -I$(QTDIR)/include/QtGui.

Ciao, /  /
     /--/
    /  / ANS




More information about the kde-core-devel mailing list