2

In the following code snippet (C, for ARM embedded chips)

typedef struct __attribute__((__packed__)) MyStruct_S
{
    int MyFirstField;
    int MySecondField;
} MyStruct_Type;

VS Code complains on the MyStruct_S identifier, following the attribute. The complaint says "expected a ';'C/C++(65)".

And yet this code compiles and runs just fine even with -Wall and -Wextra compiler flags set.

What is VS Code doing wrong? Can we fix the IntelliSence to see that this is correct syntax?

The extensions I have for this workspace are the following:

  • Arm Assembly
  • C/C++
  • Doxygen Documentation Generator
  • GNU Linker Map files
  • Makefile Tools
  • WSL

cheers!

Gregory Fenn
  • 460
  • 2
  • 13
  • https://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed – abelenky Jun 22 '23 at 09:07
  • 2
    @abelenky OP is not using Visual C++. – Ian Abbott Jun 22 '23 at 09:09
  • @abelenky How is your comment related to my question? – Gregory Fenn Jun 22 '23 at 09:20
  • 2
    Given the structure definition, `__attribute__((__packed__))` seems useless. – chqrlie Jun 22 '23 at 09:23
  • @chqrlie you're right, this is an example... did you think I had a structure called MyStruct_Type with a field called MyFirstField in a real project? – Gregory Fenn Jun 22 '23 at 09:31
  • @GregoryFenn VS Code is just the IDE. The message is given by the compiler. Which compiler suite are you using? – Serkan Jun 22 '23 at 09:31
  • 1
    @Serkan it is mentioned "And yet this code compiles and runs just fine even with -Wall and -Wextra compiler flags set." Therefore that message seems to come from Intellisense, not from the compiler. – Gerhardh Jun 22 '23 at 09:33
  • @Serkan - the compiler is arm-none-eabi-gcc v8.3.1. As I said in my question, the compilation and execution run just fine. So the compiler is not the problem, the IDE is the issue. – Gregory Fenn Jun 22 '23 at 09:33
  • 1
    @GregoryFenn: my comment was meant as a joke :) The problem seems to be a configuration issue: VS Code uses a language server that does not support the gcc extension `__attribute__`. You might want to add `#ifdef _MSC_VER #define __attribute__(x) #endif` – chqrlie Jun 22 '23 at 09:37
  • The Intellisense engine is assuming MSVC extensions, where gcc extension `__attribute__((__packed__))` makes no sense. – abelenky Jun 22 '23 at 10:10

1 Answers1

4

You should change the C/C++ Extension Configurations. Select a suitable one under the Intellisense Mode. On Windows, it is by default set to windows-msvc-x64, and on Linux, linux-gcc-x64. Create a new C/C++ configuration (C/C++: Edit Configurations command), and set the Intellisense Mode to linux-gcc-arm. Then for your project, using the "C/C++: Select a configuration..." command change your configuration.

enter image description here

Serkan
  • 1,148
  • 8
  • 8