Includes in Qt Creator



  • Hi,

    I'm trying to figure out how the header pathes are handled in qtcreator.

    I've imported a c++-project with structure like this:
    src/
    dir1/
    file1.cpp
    file1.h
    ...
    subdir1/
    file2.cpp
    file2.h
    dir2/
    file3.cpp
    file3.h
    ...

    Now, in file3.h I have, for example, following #includes:
    @#include "dir1/file1.h"
    #include "dir1/subdir1/file2.h"@

    So, everything is specified relative to the src/ directory. The parser says, however, that the includes cannot be found.
    The are found if I just use
    @#include "file1.h"
    #include "file2.h"@

    But it is not possible to change the include-directives in this project.
    How can I configure this behaviour in qt creator?



  • Have you set a correct "include path":http://qt-project.org/doc/qt-5.0/qtdoc/qmake-variable-reference.html#includepath at the pro file of your project?



  • No. It's not a qt-project. There is no pro-file. I just want to use the creator as the source editor. I don't use creator to trigger the build process neither. I just want to navigate through the code und edit it with creator.

    Actually, it worked as expected a couple of month ago - creator recognized the includes relative to the top source directory and I was able to navigate.



  • It found the header files without the directory, because the headers are in the same directory as the source files. So I am a little confused, what you want to do. Also this sentence is a little confused:

    bq.
    So, everything is specified relative to the src/ directory.

    How I know, all files are relative to its own path.
    if you have this structure:

    src/file1.cpp
    src/file1.h
    src/subdir/file2.cpp

    then including file1.h in file2.cpp, you have to write
    @#include "../file1.h" @



  • Sorry, I didn't notice that my tree-like formating of the directory structure was broken in the first post.

    So, here again a simplified version of the directory structure:
    src/dir1/file1.cpp
    src/dir1/file1.h
    src/dir1/subdir1/file2.cpp
    src/dir1/subdir1/file2.h

    src/dir2/file3.cpp
    src/dir2/file3.h

    "Relative to the src-directory" means that all includes look like here:
    @#include "dir1/file1.h"
    #include "dir1/subdir1/file2.h"@

    The logic is similar to cmake's include_directories(include_dir) statement, where include_dir is relative to the directory where CMakeLists.txt resides in.
    For example, there is an option in kdevelop, where you can specify for the internal parser, what this "starting directory" is. And as I said, it also worked in qtcreator two or three releases ago.

    Your suggestion to use something like
    @#include "../file1.h"@

    is not an option in big projects with a lot of sub directories where you can easily end up with something like this

    @ #include "../../../../foo/bar/file.h" @

    etc. In order to avoid such ugliness many build systems and source editors can be taught about the top-directory where then all the includes in the code are specified relative to this path. In my example this top directory ist "src/" and all includes should be in this form
    #include "dir1/file1.h"
    #include "dir1/subdir1/file2.h"


  • Moderators

    #include "..." is always relative to the file you are in first and then heads of to the include paths if there is no local match. So include "dir1/something" must fail win "dir2/include_here.cpp" (provided no include path is set).

    Use #include <...> if you want to not search starting from the current directory first. This will check the directories in the include path only.

    Creator extracts the include directories (as well as defines, files that are part of the project, compiler settings, etc.) from the build system you are using, so there is no need to set this up in the UI somewhere.

    You can not have full navigation support without all the data of the project (include paths, defines, compiler flags, list of files that belong to the project/, etc.). To make full use of creator you will need a project of some kind. You can

    • Either use one of the supported build systems qmake, cmake, autotools or qbs (in 2.7.x).
    • You can even generate a simple .pro file for creators sake only using qmake -project.
    • You can use a "generic project" which is just a couple of files that list all the required information one item per line.

    If this was working two or three releases ago, then this was a bug. We did improve the Creator a lot in the meantime so that it no longer has to rely on guessing include paths:)



  • [quote author="Tobias Hunger" date="1362650871"]#include "..." is always relative to the file you are in first and then heads of to the include paths if there is no local match. So include "dir1/something" must fail win "dir2/include_here.cpp" (provided no include path is set).[/quote]
    Yes, this is correct, provided no include path is set. Once you specify the include path in your build system correctly, you can build your code. Once you specify the include path in your source editor, the internal parser can cope with the includes I'd like to have.

    [quote]
    Use #include <...> if you want to not search starting from the current directory first. This will check the directories in the include path only.

    Creator extracts the include directories (as well as defines, files that are part of the project, compiler settings, etc.) from the build system you are using, so there is no need to set this up in the UI somewhere.
    [/quote]
    There is need for this feature if you use creator just as a source editor since there is no need to trigger code compilation from the UI or the build system is not supported by creator.

    [quote]
    You can not have full navigation support without all the data of the project (include paths, defines, compiler flags, list of files that belong to the project/, etc.).
    [/quote]
    This part I don't understand. Why is it not possible to have a full navigation support in creator? It's merely a simple dumb option for the parser where to look for the files - either relative to the current file or relative to the specified directory.
    Eclipse can do this - in the properties for C++/Indexer you can provide your pathes. Current version of eclipse can even figure out where to look for the files without any additional work. Note, eclipse doesn't now anything about the used build system.
    kdevelop can also handle this situatios. I cannot look it up now, though.

    [quote]
    To make full use of creator you will need a project of some kind. You can

    • Either use one of the supported build systems qmake, cmake, autotools or qbs (in 2.7.x).
    • You can even generate a simple .pro file for creators sake only using qmake -project.
    • You can use a "generic project" which is just a couple of files that list all the required information one item per line.
      [/quote]
      It's a huge project with several GBs of code. I cannot switch to another build system. Neither can I change the include-philiosphy in this project.

    Concerning "generic project": I guess, I get this project if I use "import project" in the "new project"-wizard and don't specify any software versioning or build systems. This is exactly what I did and what I wan't creator to use for.

    [quote]
    If this was working two or three releases ago, then this was a bug. We did improve the Creator a lot in the meantime so that it no longer has to rely on guessing include paths:)
    [/quote]
    Yeah :-) Creator is great peace of software and I'd like to use it.
    But again, you don't need to guess the path. Just use the specified path or the default behaviour (relative to the current file) if no path was specified.
    So, to put the long story shor: I'm missing in creator the possibility to configure the indexer.


  • Moderators

    Oh, you are using a generic project already? I understood you are just opening independent files:-)

    Making include paths known to a generic project is done by appending them to the projectname.includes file (one directory per line).



  • I've just added my src-directory to my .includes file and everything works now as expected :-)
    Thank you a lot!

    There are a lot of entries in this file in my case, subdirectories in src/, like
    /absolute_path_to_src/src/dir1
    /absolute_path_to_src/src/dir2
    etc, but not the /absolute_path_to_src/src itself. When importing the project, I provided /absolute_path_to_src/src as the project path. This path didn't get included in my_project.includes...
    Is it a bug or a feature?

    By the way, creator 2.6.0 and 2.6.2 went out of memory while parsing this project on my machine with 48GB of RAM. 2.7beta2 completes this job without crashing :-)


  • Moderators

    Good to hear that 2.7 works for you:-)

    I'd suggest to weed out the includes paths to make them match with what you pass to your compiler.



  • I removed all the includes and only put there my /absolute_path_to_src/src.
    Everything works now as expected. Thank you again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.