简体   繁体   English

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

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

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? 如何判断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 ++文件时都必须创建整个项目吗?

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

So the answer is this: create a project, always , to use the syntax analyzis. 所以答案是这样的: 总是创建一个项目来使用语法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. 您可能会发现使用Qt Project文件的一些有趣方法,尽管不使用Qt SDK或使用Creator来构建项目。 See an answer to a similar question here: https://stackoverflow.com/a/5817226/389405 请在此处查看类似问题的答案: 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. 请注意,我个人使用此方法,使用.pro文件只列出我项目的所有子目录,并使用所有键盘快捷键来构建禁用,以便我只使用Qt Creator作为编辑器。 The syntax highlighting/linking is awesome and exceptionnally quick, far, far quicker than Eclipse! 语法高亮/链接非常棒,比Eclipse快得多,远远快得多!

To do that, simply issue qmake -pro in the directory of your project. 为此,只需在项目目录中发出qmake -pro即可。 It will create a [DIR].pro file that you can remove any time. 它将创建一个[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. 我在一年前填写了一个关于这个语法analyzis的一个方面的错误,这个问题困扰我: https ://bugreports.qt.io/browse/QTCREATORBUG-4846,原因是我使用的编译器用法语输出数据而不是英语的。 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. 他们修复了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. 否则,我看到的唯一解决方案是修改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 ). 您可以在他们的上一篇文章中找到有关如何贡献的一些信息: https//blog.qt.io/blog/2012/03/15/qt-creator-2-5-beta/ (并尝试他们的新测试版,支持C ++ lambdas )。

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++?. 检查安装的环境变量是否正确,例如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 ++标准头文件位置的完整路径添加到.pro文件中的变量“includepath”。

Standalone C++ files can be edited, but it is likely you will loose the functionality of #include's and autocomplete, among other thigns. 可以编辑独立的C ++文件,但是你可能会失去#include和autocomplete等功能。 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. 我亲自把我的所有项目保存在Qt中,无论是一个简单的GUI程序执行一项任务,所以我可以调试它(gdb似乎不喜欢命令行程序,至少根据我的经验)或使用“简单的C ++”命令行“非Qt依赖项目的项目。

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. 希望能帮助到你。

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/

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. 没有默认的工具链,因此您需要一个项目,以便在为其选择的工具链中找到这些标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM