Difference between revisions of "User:Mkretz"

Jump to: navigation, search
(removed Thomas' comment)
(as an application developer)
Line 17: Line 17:
 
=== as an application developer ===
 
=== as an application developer ===
 
* Include headers from '''external''' libraries using '''angle brackets'''.
 
* Include headers from '''external''' libraries using '''angle brackets'''.
<code cpp>
+
<code cpp-qt>
 
#include <iostream>
 
#include <iostream>
 
#include <QtCore/QDate>
 
#include <QtCore/QDate>
Line 23: Line 23:
 
</code>
 
</code>
 
* Include headers from your '''own project''' using '''double quotes'''.
 
* Include headers from your '''own project''' using '''double quotes'''.
<code cpp>
+
<code cpp-qt>
 
#include "myclass.h"
 
#include "myclass.h"
 
</code>
 
</code>
Line 30: Line 30:
 
''Headers of your own application have a defined relative location to the source files of your application. Using KDE4's cmake macros your source directory is the first include switch to the compiler and therefore there's no difference in using angle brackets or double quotes. If you work with a different buildsystem that does not include the current source directory or disable CMAKE_INCLUDE_CURRENT_DIR then all includes (inside your application) using angle brackets will break.''
 
''Headers of your own application have a defined relative location to the source files of your application. Using KDE4's cmake macros your source directory is the first include switch to the compiler and therefore there's no difference in using angle brackets or double quotes. If you work with a different buildsystem that does not include the current source directory or disable CMAKE_INCLUDE_CURRENT_DIR then all includes (inside your application) using angle brackets will break.''
  
''Ideally the buildsystem would not need to specify <tt>-I<source directory></tt> though as that can break with library headers that have the same filename as a header of your project (i.e.: If a library has the header file <tt>foo.h</tt> and your project has a different file with the same filename the compiler will always pick the header from your project instead of the one from the library because the source directory of the project is specified first.)''
+
''Ideally the buildsystem would not need to specify <tt>-I&lt;source directory&gt;</tt> though as that can break with library headers that have the same filename as a header of your project (i.e.: If a library has the header file <tt>foo.h</tt> and your project has a different file with the same filename the compiler will always pick the header from your project instead of the one from the library because the source directory of the project is specified first.)''
  
 
=== as a library developer ===
 
=== as a library developer ===
 
* Include headers from '''external''' libraries using '''angle brackets'''.
 
* Include headers from '''external''' libraries using '''angle brackets'''.
<code cpp>
+
<code cpp-qt>
 
#include <iostream>
 
#include <iostream>
 
#include <QtCore/QDate>
 
#include <QtCore/QDate>
Line 40: Line 40:
 
</code>
 
</code>
 
* Include headers of your '''own library''' and libraries that belong to it using '''double quotes'''.
 
* Include headers of your '''own library''' and libraries that belong to it using '''double quotes'''.
<code cpp>
+
<code cpp-qt>
 
#include "xyz.h" // same library and same directory
 
#include "xyz.h" // same library and same directory
 
</code>
 
</code>
Line 50: Line 50:
 
''<tt>/usr/include/libxyz/xyz.h</tt> includes <tt>foo.h</tt> using angle brackets and expects to have it replaced with the contents of the file <tt>/usr/include/libzyx/foo.h</tt>. Assuming there's another library that also ships a <tt>foo.h</tt> file in the directory <tt>/usr/include/anotherlib/</tt>. If the application that uses both libraries compiles with "<tt>g++ -I/usr/include/libxyz -I/usr/include/anotherlib ...</tt>" libxyz will work as expected. If the application compiles with "<tt>g++ -I/usr/include/anotherlib -I/usr/include/libxyz ...</tt>" the header <tt>xyz.h</tt> will include the file <tt>/usr/include/anotherlib/foo.h</tt> instead of the file that is shipped with libxyz. The same problem can appear if an application has a header file of the same name as a library and specifies <tt>-I./</tt> as the first include directory.''
 
''<tt>/usr/include/libxyz/xyz.h</tt> includes <tt>foo.h</tt> using angle brackets and expects to have it replaced with the contents of the file <tt>/usr/include/libzyx/foo.h</tt>. Assuming there's another library that also ships a <tt>foo.h</tt> file in the directory <tt>/usr/include/anotherlib/</tt>. If the application that uses both libraries compiles with "<tt>g++ -I/usr/include/libxyz -I/usr/include/anotherlib ...</tt>" libxyz will work as expected. If the application compiles with "<tt>g++ -I/usr/include/anotherlib -I/usr/include/libxyz ...</tt>" the header <tt>xyz.h</tt> will include the file <tt>/usr/include/anotherlib/foo.h</tt> instead of the file that is shipped with libxyz. The same problem can appear if an application has a header file of the same name as a library and specifies <tt>-I./</tt> as the first include directory.''
  
If you use subdirectories for the installed header files you need to have the exact same directory structure for the headers in the source directory. Example: <tt>/usr/include/libfoo/</tt> contains the directory <tt>bar</tt>. In <tt>libfoo</tt> resides the header <tt>header1.h</tt>, in <tt>libfoo/bar</tt> the file <tt>header2.h</tt>. The latter depends on the former so it includes it using<code cpp>#include "../header1.h"</code>If the source directory structure of the library is not the same (in this case: <tt>header2.h</tt> in a subdirectory of the directory where <tt>header1.h</tt> resides) this obviously will break.
+
If you use subdirectories for the installed header files you need to have the exact same directory structure for the headers in the source directory. Example: <tt>/usr/include/libfoo/</tt> contains the directory <tt>bar</tt>. In <tt>libfoo</tt> resides the header <tt>header1.h</tt>, in <tt>libfoo/bar</tt> the file <tt>header2.h</tt>. The latter depends on the former so it includes it using<code cpp-qt>#include "../header1.h"</code>If the source directory structure of the library is not the same (in this case: <tt>header2.h</tt> in a subdirectory of the directory where <tt>header1.h</tt> resides) this obviously will break.

Revision as of 15:16, 10 August 2011

getting #includes right

There are two types of #include statements: #include <foo.h> and #include "foo.h".

Say we have the file xyz.h in /usr/include/mylib/ that contains the following:

  1. include <header1.h>
  2. include "header2.h"

The preprocessor will search for the file header1.h in all the paths given as -I arguments and then replace the line with the contents of that file.

For line 2 the preprocessor tries to use the file /usr/include/mylib/header2.h first and if it does not exist search for the file like it did for header1.h. The important part to note here is that the preprocessor does not look in the directory of the source file that includes xyz.h but in the directory where xyz.h resides.

Now, which include statement is the one to use? After all you can specify every directory you want using -I and thus could use #include <...> everywhere.

as an application developer

  • Include headers from external libraries using angle brackets.

  1. include <iostream>
  2. include <QtCore/QDate>
  3. include <zlib.h>

  • Include headers from your own project using double quotes.

  1. include "myclass.h"

Rationale: The header files of external libraries are obviously not in the same directory as your source files. So you need to use angle brackets.

Headers of your own application have a defined relative location to the source files of your application. Using KDE4's cmake macros your source directory is the first include switch to the compiler and therefore there's no difference in using angle brackets or double quotes. If you work with a different buildsystem that does not include the current source directory or disable CMAKE_INCLUDE_CURRENT_DIR then all includes (inside your application) using angle brackets will break.

Ideally the buildsystem would not need to specify -I<source directory> though as that can break with library headers that have the same filename as a header of your project (i.e.: If a library has the header file foo.h and your project has a different file with the same filename the compiler will always pick the header from your project instead of the one from the library because the source directory of the project is specified first.)

as a library developer

  • Include headers from external libraries using angle brackets.

  1. include <iostream>
  2. include <QtCore/QDate>
  3. include <zlib.h>

  • Include headers of your own library and libraries that belong to it using double quotes.

  1. include "xyz.h" // same library and same directory

Rationale: The header files of external libraries are obviously not in a fixed location relative to your source files. So you need to use angle brackets.

Headers of your own libraries have a fixed relative location in the filesystem. Therefore you can use double quotes. You should use double quotes because otherwise the include statement could include a different header file than expected. An example how angle brackets can break the build:

/usr/include/libxyz/xyz.h includes foo.h using angle brackets and expects to have it replaced with the contents of the file /usr/include/libzyx/foo.h. Assuming there's another library that also ships a foo.h file in the directory /usr/include/anotherlib/. If the application that uses both libraries compiles with "g++ -I/usr/include/libxyz -I/usr/include/anotherlib ..." libxyz will work as expected. If the application compiles with "g++ -I/usr/include/anotherlib -I/usr/include/libxyz ..." the header xyz.h will include the file /usr/include/anotherlib/foo.h instead of the file that is shipped with libxyz. The same problem can appear if an application has a header file of the same name as a library and specifies -I./ as the first include directory.

If you use subdirectories for the installed header files you need to have the exact same directory structure for the headers in the source directory. Example: /usr/include/libfoo/ contains the directory bar. In libfoo resides the header header1.h, in libfoo/bar the file header2.h. The latter depends on the former so it includes it using#include "../header1.h"If the source directory structure of the library is not the same (in this case: header2.h in a subdirectory of the directory where header1.h resides) this obviously will break.


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal