User:Mkretz: Difference between revisions

    From KDE TechBase
    (#include draft)
     
    (escaping and tt)
    Line 1: Line 1:
    == getting #includes right ==
    == getting #includes right ==


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


    Say we have the file <tt>header1.h</tt> in <tt>/usr/include/mylib/</tt> that contains the following:
    Say we have the file <tt>header1.h</tt> in <tt>/usr/include/mylib/</tt> that contains the following:
    Line 9: Line 9:
    </code>
    </code>


    The preprocessor will search for the file <tt>header2.h</tt> in all the paths given as -I arguments and then replace the line with the contents of that file.
    The preprocessor will search for the file <tt>header2.h</tt> in all the paths given as <tt>-I</tt> 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/header3.h first and if it does not exist search for the file like for <tt>header2.h</tt>. The important part to note here is that the preprocessor does not look in the directory of the source file that includes <tt>header1.h</tt> but in the directory where <tt>header1.h</tt> resides.
    For line 2 the preprocessor tries to use the file /usr/include/mylib/header3.h first and if it does not exist search for the file like for <tt>header2.h</tt>. The important part to note here is that the preprocessor does not look in the directory of the source file that includes <tt>header1.h</tt> but in the directory where <tt>header1.h</tt> 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 <tt>#include \<...\></tt> everywhere.
    Now, which include statement is the one to use? After all you can specify every directory you want using <tt>-I</tt> and thus could use <tt>#include <...></tt> everywhere.


    === as an application developer ===
    === as an application developer ===
    Line 22: Line 22:
    #include <zlib.h>
    #include <zlib.h>
    </code>
    </code>
    * Include headers from your own project using double quotes. Using angle brackets works correctly if the first -I switch to the compiler is your own source directory. Ideally you would not need to specify -I./ though, as that might 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.)  
    * Include headers from your own project using double quotes. Using angle brackets works correctly if the first <tt>-I</tt> switch to the compiler is your own source directory. Ideally you would not need to specify <tt>-I./</tt> though, as that might 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 ===
    Line 33: Line 33:
    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 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.


    It is also possible to use angle brackets in library headers, but that requires the application to supply the exactly correct -I paths (e.g. libfoo above can be used either with <tt>-I/usr/include</tt> and <tt>#include \<libfoo/header1.h\></tt> or <tt>-I/usr/include/libfoo</tt> and <tt>#include \<header1.h\></tt>, if libfoo were to use angle brackets to include its own headers one of the include switches has to be present for the lib to find its own headers). Also angle brackets require the application to not put any include directory before the -I switch for the lib that contains header files of the same filename as for the library. An example how angle brackets can break the build:
    It is also possible to use angle brackets in library headers, but that requires the application to supply the exactly correct <tt>-I</tt> paths (e.g. libfoo above can be used either with <tt>-I/usr/include</tt> and <tt>#include <libfoo/header1.h></tt> or <tt>-I/usr/include/libfoo</tt> and <tt>#include <header1.h></tt>, if libfoo were to use angle brackets to include its own headers one of the include switches has to be present for the lib to find its own headers). Also angle brackets require the application to not put any include directory before the <tt>-I</tt> switch for the lib that contains header files of the same filename as for the library. An example how angle brackets can break the build:


    <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>/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 (but anotherlib might break). 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>g++ -I/usr/include/libxyz -I/usr/include/anotherlib ...</tt> libxyz will work as expected (but anotherlib might break). 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.

    Revision as of 17:09, 9 April 2007

    getting #includes right

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

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

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

    The preprocessor will search for the file header2.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/header3.h first and if it does not exist search for the file like for header2.h. The important part to note here is that the preprocessor does not look in the directory of the source file that includes header1.h but in the directory where header1.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; examples:

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

    • Include headers from your own project using double quotes. Using angle brackets works correctly if the first -I switch to the compiler is your own source directory. Ideally you would not need to specify -I./ though, as that might 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 (see above for examples)
    • Include headers of your own library and libraries that belong to it using double quotes. 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 the libfoo directory is the header header1.h. In the bar subdirectory is the file header2.h. The latter depends on the former so it includes it using

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

    It is also possible to use angle brackets in library headers, but that requires the application to supply the exactly correct -I paths (e.g. libfoo above can be used either with -I/usr/include and #include <libfoo/header1.h> or -I/usr/include/libfoo and #include <header1.h>, if libfoo were to use angle brackets to include its own headers one of the include switches has to be present for the lib to find its own headers). Also angle brackets require the application to not put any include directory before the -I switch for the lib that contains header files of the same filename as for the library. 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 (but anotherlib might break). 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.