C++ Qt Creator 找不到标题(说:“没有这样的文件或目录”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9572956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:00:33  来源:igfitidea点击:

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

c++cwindowsqt-creator

提问by user541686

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

如何告诉 Qt Creator 2.4.1(基于 Qt 4.7.4 32 位)在默认情况下在哪里查找头文件?

When I open a C file in Qt Creator and say

当我在 Qt Creator 中打开一个 C 文件并说

#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?

我想我也应该问:这甚至可能吗?还是每次我想编辑一个独立的 C++ 文件时都必须创建一个完整的项目?

回答by Gui13

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

我发现自己经常面临这个问题。我现在也可以在我的机器上重现它(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.

看起来 QtCreator 需要有一个项目来正确处理 GCC 路径分析(在 Qt 框架路径之上)。
Qt 用于查找头文件的过程是它使用特殊参数启动 GCC(或 Windows 上的编译器),使其输出编译器找到其头文件的路径。但是,要做到这一点,它必须有一个与您的文件相关联的项目,因为它使用该项目来确定要使用的工具链,从而找到 GCC 的位置。

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.

所以答案是这样的:创建一个项目,始终使用语法分析。
请注意,当您创建此项目时,定义您将使用的 Qt SDK 版本和工具链非常重要,否则语法 control-click 将不起作用。

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

您可能会发现一些有趣的使用 Qt 项目文件的方法,尽管没有使用 Qt SDK 或使用 Creator 来构建您的项目。在此处查看类似问题的答案:https: //stackoverflow.com/a/5817226/389405

Note that I personnally use this method, with a .profile 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!

请注意,我个人使用这种方法,一个.pro文件只列出了我项目的所有子目录,并且禁用了所有构建的键盘快捷键,因此我只使用 Qt Creator 作为编辑器。语法高亮/链接非常棒,而且速度非常快,比 Eclipse 快得多!

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

为此,只需qmake -pro在您的项目目录中发出问题。它将创建一个[DIR].pro您可以随时删除的文件。

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.

一年前,我在此语法分析的一个方面解决了一个让我感到困扰的错误:https: //bugreports.qt.io/browse/QTCREATORBUG-4846,原因是我使用编译器以法语输出其数据英语的。他们在 2.4 中修复了代码,但它可能是特定于 Unix 的(有关更多信息,请参阅问题的注释),因此您可能想要测试此问题是否适用于您的案例。

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

如果您的问题在为您的版本创建项目后仍然存在,请务必将其指向 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).

否则,我看到的唯一解决方案是修改 QtCreator 的源代码。您可以在他们的上一篇文章中找到有关如何在此处做出贡献的一些信息:https: //blog.qt.io/blog/2012/03/15/qt-creator-2-5-beta/(并尝试他们的新测试版支持 C++ lambdas)。

回答by Drise

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.

可以编辑独立的 C++ 文件,但您可能会失去 #include 和自动完成等功能。我个人以某种方式将我的所有项目都保存在 Qt 中,无论它是一个执行一项任务的简单 GUI 程序,因此我可以调试它(gdb 似乎不喜欢命令行程序,至少在我的经验中)或使用“简单 C++”用于非 Qt 依赖项目的命令行”项目。

回答by Tim Kachko

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

还是每次我想编辑一个独立的 C++ 文件时都必须创建一个完整的项目?

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.

希望能帮助到你。

回答by jordenysp

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.

您使用的是哪个操作系统和编译器以及两者的版本?检查安装的环境变量是否正确,例如C++的头文件在哪里?。一种变体是将“包含的标题”的路径添加到左侧面板中的项目配置中,转到“项目 - 程序 - 目标”并添加包含的路径。另一种方法是将 C++ 标准标头位置的完整路径添加到 .pro 文件中的变量“includepath”中。

回答by Chris Browet

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.

没有默认工具链,因此您需要在为其选择的工具链中找到这些标头的项目。

回答by usertc

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/