【问题标题】:QtCreator annotation compiler does not find stdbool.hQtCreator 注解编译器找不到 stdbool.h
【发布时间】:2020-05-06 10:52:04
【问题描述】:

我正在使用 QtCreator 4.11.2 ,通过 MSYS2 安装,启用了 ClangCodeModel。

这是我的程序(这是创建新的非 QT 纯 C 应用程序的结果):

#include <stdio.h>
#include <stdbool.h>

_Bool a;
bool b;

int main()
{
    printf("Hello World!\n");
    return 0;
}

.pro 文件与默认值保持一致:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
    main.c

注解编译器突出显示一个错误,指出找不到stdbool.h

但它不会为 _Bool a; 提供错误,因此它显然在 C99 模式下运行,但包含路径存在一些问题。 “跟随光标下的符号”选项有效,打开 stdbool.h。

我的问题是:如何为注解编译器配置包含路径或以其他方式解决此问题?

我一直无法弄清楚如何为注解编译器设置选项,甚至无法弄清楚它使用的是哪个编译器二进制文件。在工具 > 选项 > C++ > 代码模型 > 诊断配置下,它允许我添加 -W 标志,但不允许我添加 -I 标志,弹出一条红色消息,说明该选项无效。

在工具 > 选项 > C++ 代码模型检查器下,没有诊断消息,代码模型检查日志显示 stdbool.h 被正确找到并解析为 msys64/mingw64/lib/gcc/x86_64-w64-mingw32/9.3.0/include/stdbool.h

如果我禁用 ClangCodeModel 插件,则不会出现错误,但我想使用 clang 版本,如果它可以正常工作,因为它具有良好的诊断功能。

clang --version 在 shell 提示符中的结果是:

clang version 10.0.0 (https://github.com/msys2/MINGW-packages.git 3f880aaba91a3d9cdfb222dc270274731a2119a9)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: F:\Prog\msys64\mingw64\bin

如果我在 QtCreator 之外使用 clang 编译相同的源代码,它会编译并正确运行而无需诊断。那么注解编译器显然和命令行clang不一样?

我在 QtCreator 中选择的 Kit 是自动检测的 Desktop Qt MinGW-w64 64bit (MSYS2)

如果我创建一个普通 C++ 项目并尝试包含 stdbool.h(C++ 标准要求它存在,尽管已弃用),则会出现完全相同的症状,但有趣的是它确实接受 &lt;cstdbool&gt;


我找到了一种解决方法:在 .pro 文件中包含以下行:

INCLUDEPATH += F:/Prog/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/9.3.0/include/

使注解编译器正常工作,但是这是不可取的,因为每当我切换 Kits 时我都必须不断更改它,因为它还将它传递给实际的构建编译器,而不仅仅是注解编译器。

【问题讨论】:

  • 我没有使用 QtCreator 的经验,但是您可以使用 Process Monitor 查看是否调用了哪个 Clang 以及使用了哪些参数。
  • 我在导入的 CMake 项目中找不到 boolstdbool.h 的完全相同的问题。此外,我在NULLstddef.h 也看到了同样的情况。

标签: clang qt-creator msys2


【解决方案1】:

C:\msys64\mingw64\x86_64-w64-mingw32\include 中创建文件stdbool.h 并复制粘贴此代码:

/* Copyright (C) 1998-2017 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
/*
 * ISO C Standard:  7.16  Boolean type and values  <stdbool.h>
 */
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool        _Bool
#define true        1
#define false        0
#else /* __cplusplus */
/* Supporting _Bool in C++ is a GCC extension.  */
#define _Bool        bool
#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension.  */
#define bool        bool
#define false        false
#define true        true
#endif
#endif /* __cplusplus */
/* Signal that all the definitions are present.  */
#define __bool_true_false_are_defined        1
#endif        /* stdbool.h */

注意

创建手动文件stdbool.h 对我有用,但目前它是一个粗略的临时解决方案。如果您觉得它太粗略,请不要使用它。如果存在,我宁愿使用替代解决方案而不是这种 hack。这个解决方案可能不太好,但它仍然对我有用。

【讨论】:

  • 看起来很粗略。该文件应该已经以/mingw64/include/c++/11.2.0/tr1/stdbool.h 的形式存在,无需自己滚动。
  • 不是我自己的,我只是从网上复制的,因为它在C:\msys64\mingw64\x86_64-w64-mingw32\include 中不存在。这是 MSYS2 qtcreator 使用的 Clang 代码模式 路径。 qtcreator 可以在没有stdbool.h 的情况下正常编译,因为它使用另一个包含stdbool.h 的路径
  • 你说得对,我也认为它粗略。我将不胜感激替代解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 2019-01-24
  • 2012-01-14
  • 2014-03-25
  • 1970-01-01
相关资源
最近更新 更多