Qt Creator can't find headers (says: “No such file or directory”)

前端 未结 6 1028
轻奢々
轻奢々 2021-02-05 10:11

How can I tell Qt Creator 2.4.1 (based on Qt 4.7.4 32-bit) where to look by default for header files?

When I open a C file in Qt Creator and say



        
相关标签:
6条回答
  • 2021-02-05 10:37

    which operating system and compiler you are using and version of both? Check if the environment variables of the install is correct, for example Where is the headers of c++?. A variation is to add the path of the "includes of your headers" to the project configuration in the left panel go to "project - program - target" and add the path of the includes. another is to add the full path to the location of the c++ standard headers to the variable "includepath" in your .pro file.

    0 讨论(0)
  • 2021-02-05 10:41

    Standalone C++ files can be edited, but it is likely you will loose the functionality of #include's and autocomplete, among other thigns. I personally keep all my projects in Qt somehow, whether it is a simple GUI program that does one task, so I can debug it (gdb doesn't seem to like command line programs, atleast in my experience) or use a "Simple C++ command line" project for non-Qt dependent projects.

    0 讨论(0)
  • 2021-02-05 10:43

    Or must I create an entire project every time I want to edit a standalone C++ file?

    No, you can include standalone file in existing project by just one right click on root of the project tree - context menu - add existing file. You can assign hot key for that, as well as for removing file from the project, so it will be fast and easy to use your favorite editor and its environment. You can look at it as you just opening file. Unfortunately there are no command-line arguments for assigning a default project (and its environment) for a standalone file for using it by double click in a file manager.

    Hope it helps.

    0 讨论(0)
  • 2021-02-05 10:47

    I found myself often faced with this problem. I can reproduce it on my machine right now as well (Mac OS).

    It looks like QtCreator needs to have a project to correctly handle GCC path analysis (on top of Qt frameworks paths).
    The process Qt uses to find the headers is that it launches GCC (or your compiler on Windows) with special arguments that make it output the paths where the compiler finds its headers. BUT, to do that, it must have a project associated to your files, because it uses this project to determine what toolchain to use, thus where GCC is found.

    So the answer is this: create a project, always, to use the syntax analyzis.
    Note that it is quite important that when you create this project, you define which version of the Qt SDK and the toolchain you'll use, otherwise the syntax control-click won't work.

    You might find some interesting ways of using a Qt Project file although not using the Qt SDK or using Creator to build your project. See an answer to a similar question here: https://stackoverflow.com/a/5817226/389405

    Note that I personnally use this method, with a .pro file that simply lists all the subdirectories of my project, and with all keyboard shortcuts to build disabled, so that I only use Qt Creator as an editor. The syntax highlighting/linking is awesome and exceptionnally quick, far, far quicker than Eclipse!

    To do that, simply issue qmake -pro in the directory of your project. It will create a [DIR].pro file that you can remove any time.

    I filled a bug a year ago on an aspect of this syntax analyzis that was bothering me here: https://bugreports.qt.io/browse/QTCREATORBUG-4846, the reason beiing that the compiler I use output its data in French instead of English. They fixed the code in 2.4 but it might be Unix-specific (see the comments of the issue for more information) so you'll probably want to test if this issue can be applied to your case.

    If your issue continues even after creating a project for your edition, make sure to point it to the guys at qt-project.org !

    Otherwise, the only solution I see is modifying the source code of QtCreator. You can find in their last post some information about how to contribute here: https://blog.qt.io/blog/2012/03/15/qt-creator-2-5-beta/ (and try their new beta which supports C++ lambdas).

    0 讨论(0)
  • 2021-02-05 10:52
    add INCLUDEPATH to "project.pro" file
    
    example for raspberry pi qt projects:
    # Qt Creator can't find headers (says: “No such file or directory”)
    # add compile includes
    INCLUDEPATH += /home/user/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/libc/usr/include/ \
                   /home/user/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/libc/usr/include/arm-linux-gnueabihf/
    # pi rootfs includes
    INCLUDEPATH += /home/user/raspi/sysroot/usr/include/
    
    0 讨论(0)
  • 2021-02-05 10:57

    It depends on which toolchain is defined for the current project.

    Toolchains are defined in Tools-Options-Build & Run-Tool Chains. Most are autodetected, but you can add your own.

    There is no default toolchain, so you need a project for those headers to be found in the toolchain selected for it.

    0 讨论(0)
提交回复
热议问题