我使用的是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。
我的问题是:如何为注释编译器配置路径,或者以其他方式解决这个问题?
我一直无法弄清楚如何为注释编译器设置选项,甚至不知道它使用的是哪个编译器二进制。在Tools > Options > C++ > Code >诊断配置下,它允许我添加-W标志,但不允许我添加-I标志,会弹出一条红色消息,表示该选项无效。
在Tools > Options > 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如果我使用clang在QtCreator之外编译相同的源代码,它将在没有诊断的情况下正确编译和运行。因此注释编译器显然与命令行clang不一样。
我在QtCreator中选择的工具包是自动检测的桌面Qt MinGW-w64位(MSYS2)
如果我创建一个简单的C++项目并尝试包含stdbool.h ( C++标准要求存在,尽管不推荐),则会出现完全相同的症状,但有趣的是,它确实接受<cstdbool>。
我找到了一种解决办法:包括在.pro文件中的行:
INCLUDEPATH += F:/Prog/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/9.3.0/include/使注释编译器正常工作,但是这是不可取的,因为每当我切换工具包时,我都必须不断地更改它,因为它还会将它传递给实际的构建编译器,而不仅仅是注释编译器。
发布于 2021-11-06 18:44:38
在stdbool.h中创建文件C:\msys64\mingw64\x86_64-w64-mingw32\include并复制粘贴以下代码:
/* 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对我来说是可行的,但它目前只是一个粗略的临时解决方案。如果你觉得太简略的话,不要用这个。如果存在,我宁愿使用另一种解决方案,也不愿使用这种攻击。这个解决方案可能不是很好,但对我仍然有效。
https://stackoverflow.com/questions/61633336
复制相似问题