18

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

#include <stdio.h>

it underlines the line and says

stdio.h: No such file or directory

I would like to tell it to look for headers in a directory of my choice; how do I do this?

Update

I guess I should also ask: Is this even possible? Or must I create an entire project every time I want to edit a standalone C++ file?

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • Why do you need to use stdio.h? –  Mar 05 '12 at 20:01
  • @Sosukodo: Uhm what? It's standard C code... – user541686 Mar 05 '12 at 21:07
  • 1
    Qt Creator is for writing C++ code with the Qt framework. I guess I just don't understand why you're going to all the trouble to avoid the framework the IDE was designed to be used with. –  Mar 05 '12 at 21:20
  • 8
    @Sosukodo: Qt Creator is an awesome IDE in its own right, outside of all it Qt specific functionality. – rubenvb Mar 05 '12 at 21:26
  • @Sosukodo: What rubenvb said. Also, note that it doesn't work with *any* other header file I tried (that wasn't Qt-specific). – user541686 Mar 05 '12 at 21:29
  • 1
    You may want to modify the Qt Creator source to fit your needs because Qt Creator was not designed to do what you're asking. –  Mar 09 '12 at 02:01
  • @Sosukodo: I was afraid of that... this is really sad, it looks like such a great editor otherwise! If by any chance you have any idea what part of the source code I should edit, please post it so I can accept it. :) – user541686 Mar 14 '12 at 01:26
  • 6
    @Sosukodo What does that matter? The OP has an issue where the header files are not being located by QT Creator. Who cares what he is trying to do in this case... – Aleks Mar 14 '12 at 23:24

6 Answers6

14

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

Community
  • 1
  • 1
Gui13
  • 12,993
  • 17
  • 57
  • 104
  • You must mean `qmake -project`, not `qmake -pro`. Or your option might have worked in earlier versions of Qt and doesn't in current. – Ruslan Jun 21 '17 at 11:50
1

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.

jordenysp
  • 2,644
  • 22
  • 17
  • Which environmental variables would I need to check? (I **don't** have a **project**, it's a single file... that's why I asked for the **default** directories.) – user541686 Mar 05 '12 at 21:08
  • There are no default toolchain/directories. You need a project for syntax highlighting to find the headers. – Chris Browet Mar 06 '12 at 16:25
1

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.

Drise
  • 4,310
  • 5
  • 41
  • 66
1

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.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21
0
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/
usertc
  • 1
0

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.

Chris Browet
  • 4,156
  • 20
  • 17
  • It's auto-detected Visual Studio 2008 and 2010 all right, but for some reason it hasn't located the headers. :\ – user541686 Mar 05 '12 at 21:09