Properly use #include <> and #include ""

Michael Olbrich michael-olbrich at web.de
Fri Jun 15 18:30:07 BST 2007


On Fri, Jun 15, 2007 at 04:41:39PM +0200, Hans Meine wrote:
> 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.

Very true, but will you always get its content?

> > ----- 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.

Not necessarily from a logical point of view. Or do you always add
#include <QtCore/QString>
when you are using QStrings? In most cases it is indirectly included.

> 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).

I had the problem once. One version of a lib in /usr and another in
/usr/local. The second one which added an abstraction layer that I was
using. It didn't break until I included a header that was not part of
the abstraction layer _and_ had changed since the version in /usr.

To point is, having two versions of the same lib in the search path
should always fail unless the version you are using comes first. Using
<> makes sure of that.

And by the way, even if the files are from different libraries the
probability that the include guards are the same is rather high. After
all must use some variation of the file name.

> 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.

Actually -I$(QTDIR)/include is always necessary. Qt uses
#include <QtGui/QWidget>
in it's headers.

michael

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070615/70b652e1/attachment.sig>


More information about the kde-core-devel mailing list