Visual Studio Code、gcc、Mac 的 C/C++ 扩展:“变量 uint32_t 不是类型名称”

Eli*_*iot 6 c gcc visual-studio-code

我已经开始在 Mac 上使用 VSC 和 gcc for ARM 来完成我的嵌入式 C 项目。在 中设置包含路径后c_cpp_properties.json,我的大部分路径#includes现在正在工作。然而,像这样的一行:

uint32_t m_ttff_seconds = 0;
Run Code Online (Sandbox Code Playgroud)

产生红色波浪下划线和错误:

variable uint32_t is not a type name
Run Code Online (Sandbox Code Playgroud)

有问题的源文件包括 stdint:

#include <stdint.h>
Run Code Online (Sandbox Code Playgroud)

其中includePath包括:

"${HOME}/dev/gcc-arm-none-eabi-4_9-2015q3/lib/gcc/arm-none-eabi/4.9.3/include"
Run Code Online (Sandbox Code Playgroud)

和:

"intelliSenseMode": "clang-x64"
Run Code Online (Sandbox Code Playgroud)

(唯一的其他选择是msvc-x64)。

当我使用 make 和 gcc 时,代码库编译得很好。如何显示 C/C++ 扩展在哪里uint32_t

编辑:

stdint.h看起来像这样:

#ifndef _GCC_WRAP_STDINT_H
#if __STDC_HOSTED__
# if defined __cplusplus && __cplusplus >= 201103L
#  undef __STDC_LIMIT_MACROS
#  define __STDC_LIMIT_MACROS
#  undef __STDC_CONSTANT_MACROS
#  define __STDC_CONSTANT_MACROS
# endif
# include_next <stdint.h>
#else
# include "stdint-gcc.h"
#endif
#define _GCC_WRAP_STDINT_H
#endif
Run Code Online (Sandbox Code Playgroud)

stdint-gcc.h包含:

/* 7.8.1.1 Exact-width integer types */

#ifdef __INT8_TYPE__
typedef __INT8_TYPE__ int8_t;
#endif
#ifdef __INT16_TYPE__
typedef __INT16_TYPE__ int16_t;
#endif
#ifdef __INT32_TYPE__
typedef __INT32_TYPE__ int32_t;
#endif
#ifdef __INT64_TYPE__
typedef __INT64_TYPE__ int64_t;
#endif
#ifdef __UINT8_TYPE__
typedef __UINT8_TYPE__ uint8_t;
#endif
#ifdef __UINT16_TYPE__
typedef __UINT16_TYPE__ uint16_t;
#endif
#ifdef __UINT32_TYPE__
typedef __UINT32_TYPE__ uint32_t;
#endif
#ifdef __UINT64_TYPE__
typedef __UINT64_TYPE__ uint64_t;
#endif
Run Code Online (Sandbox Code Playgroud)

这表明__UINT32_TYPE__在 VSC 解析我的代码时未定义,但在我使用 make 和 gcc 构建时已定义。

编辑:

根据 @mbmcavoy 的回答,我将我的c_cpp_properties.json文件包含在此处:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${HOME}/dev/gcc-arm-none-eabi-4_9-2015q3/lib/gcc/arm-none-eabi/4.9.3/include",
                "${HOME}/dev/nRF5_SDK_14.0.0_3bcc1f7/components/libraries/util",
                "${HOME}/dev/nRF5_SDK_14.0.0_3bcc1f7/config",
                [many more of these omitted]
                "${HOME}/dev/wisol_SDK_SFM20Rx_master/development/sigfox_cfg2/source",
                "${workspaceRoot}"
            ],
            "browse": {
                "path": [
                    "${HOME}/dev/gcc-arm-none-eabi-4_9-2015q3/lib/gcc/arm-none-eabi/4.9.3/include",
                    "${HOME}/dev/nRF5_SDK_14.0.0_3bcc1f7/components/libraries/util",
                    "${HOME}/dev/nRF5_SDK_14.0.0_3bcc1f7/config",
                    [many more of these omitted]
                    "${workspaceRoot}"
                ],
                "databaseFilename": "${workspaceRoot}/.vscode/browse.vc.db"
            },
            "intelliSenseMode": "clang-x64",
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "defines": [
                "__UINT_LEAST16_MAX__=65535",
                "__UINT_LEAST8_TYPE__=unsigned char",
                "__UINT8_MAX__=255",
                "__UINT_FAST64_MAX__=18446744073709551615ULL",
                "__UINT_FAST8_MAX__=4294967295U",
                "__UINT_LEAST64_MAX__=18446744073709551615ULL",
                "__UINT_LEAST8_MAX__=255",
                "__UINTMAX_TYPE__=long long unsigned int",
                "__UINT32_MAX__=4294967295UL",
                "__UINT16_C(c)=c",
                "__UINT16_MAX__=65535",
                "__UINT8_TYPE__=unsigned char",
                "__UINT64_C(c)=c ## ULL",
                "__UINT_LEAST16_TYPE__=short unsigned int",
                "__UINT64_MAX__=18446744073709551615ULL",
                "__UINTMAX_C(c)=c ## ULL",
                "__UINT_FAST32_MAX__=4294967295U",
                "__UINT_LEAST64_TYPE__=long long unsigned int",
                "__UINT_FAST16_TYPE__=unsigned int",
                "__UINT_LEAST32_MAX__=4294967295UL",
                "__UINT16_TYPE__=short unsigned int",
                "__UINTPTR_MAX__=4294967295U",
                "__UINT_FAST64_TYPE__=long long unsigned int",
                "__UINT_LEAST32_TYPE__=long unsigned int",
                "__UINT8_C(c)=c",
                "__UINT64_TYPE__=long long unsigned int",
                "__UINT32_C(c)=c ## UL",
                "__UINT_FAST32_TYPE__=unsigned int",
                "__UINTMAX_MAX__=18446744073709551615ULL",
                "__UINT32_TYPE__=long unsigned int",
                "__UINTPTR_TYPE__=unsigned int",
                "__UINT_FAST16_MAX__=4294967295U",
                "__UINT_FAST8_TYPE__=unsigned int"
            ]
        }
    ],
    "version": 3
}
Run Code Online (Sandbox Code Playgroud)

编辑:

经过更深入的挖掘,我发现gcc-arm-none-eabi-4_9-2015q3/lib/gcc/arm-none-eabi/4.9.3/include/stdint.h已经__STDC_HOSTED__定义了,因此stdint-gcc.h实际上并没有被包含在内。相反,该标头会执行一个“ include_next <stdint.h>”,它会查找gcc-arm-none-eabi-4_9-2015q3/arm-none-eabi/include/stdint.h. 我仍然看不到 unint32_t 是在哪里定义的,无论是对于 gcc 和 make 还是对于 VSC。

mbm*_*voy 1

我已经能够在我的计算机(Windows)上解决这个问题,只需三个步骤:

  1. 将编译器定义应用于 C/C++ 扩展

__UINT32_TYPE__当它指出“这表明在 VSC 解析我的代码时未定义,但在我使用 make 和 gcc 构建时已定义”时,问题是正确的。ARM 交叉编译器有许多未包含在 clang-x64 解析器中的内置定义。

首先,找出您的 gcc 编译器定义的定义以及选项-dM -E。在 Windows 上,我能够将输出转储到文件中echo | arm-none-eabi-gcc -dM -E - > gcc-defines.txt

#define __DBL_MIN_EXP__ (-1021)
#define __HQ_FBIT__ 15
#define __UINT_LEAST16_MAX__ 0xffff
#define __ARM_SIZEOF_WCHAR_T 4
#define __ATOMIC_ACQUIRE 2
#define __SFRACT_IBIT__ 0
#define __FLT_MIN__ 1.1754943508222875e-38F
#define __GCC_IEC_559_COMPLEX 0
(etc. - I have 344 defines)
Run Code Online (Sandbox Code Playgroud)

其次,将定义添加到您的c_cpp_properties.json文件中。请注意,#define 设置值的地方需要使用符号=。(您可能可以根据需要添加单独的定义,但我使用 Excel 根据需要对它们进行格式化并排序。第一个定义适用于我的项目,与我的 Makefile 中的定义相匹配。)

"defines": [
    "STM32F415xx",
    "USE_FULL_LL_DRIVER",
    "__USES_INITFINI__",
    "__ACCUM_EPSILON__=0x1P-15K",
    "__ACCUM_FBIT__=15",
    (...)
    "__UINT32_TYPE__=long unsigned int",
    (etc.)
Run Code Online (Sandbox Code Playgroud)
  1. 设置符号数据库

在对各个定义进行了一些实验之后,我可以看到定义正在处理中stdint-gcc.h,任何类型的使用仍然会产生错误。我在我的c_cpp_properties.json文件中意识到我有"databaseFilename": ""这用于“生成的符号数据库”,但没有正确配置。我将其设置为:

"databaseFilename": "${workspaceRoot}/.vscode/browse.vc.db"
Run Code Online (Sandbox Code Playgroud)
  1. 重新启动 Visual Studio 代码

退出并重新启动 Visual Studio Code 后,声明不会导致错误,并且将鼠标悬停在变量上时,它会显示适当的类型。