2

I want to edit the smt32 code in Visual Studio 17. Evry paths I configured and all needed defined were added, but I still have one problem:
I have number of structures that looks in this way:

typedef struct __attribute__((packed)) __struct_name
{
    uint16_t id;
    uint8_t code;

} STRUCT_NAME;

The Visual Studio editor highlights the __struct_name and shows error tooltip with the next error:

expected a ';'

Of course, in Keil everything is ok.

Please, your help with the error - what I'm doing wrong?

:::UPDATE:::

Thanks to @vlk for the answer!
Visual Studio code should be packed in the different way.
To check the packed vs. non-packed structures, please, run the next code:

#include <stdio.h>
#include <stdint.h>


typedef struct __tt__
{
    uint16_t u16;
    uint8_t u8;
} TT;

typedef struct __attribute__((packed)) __pp__
{
    uint16_t u16;
    uint8_t u8;
} PP;

int main()
{
    TT tt;
    PP pp;

    printf("TT size: %d\n", sizeof(tt));

    printf("\nPP size: %d\n", sizeof(pp));

    return 0;
}
Mike
  • 21
  • 2
  • please, change `__attribute` to `__attribute__` – vlk Jun 10 '18 at 10:56
  • @vlk - Thanks! Error in copy-paste :) The problem still exists... – Mike Jun 10 '18 at 11:00
  • ok, probably VS has problem with __attribute__, so try it without `__attribute__((packed))`. exactly this structure will be always aligned as you expect although there will be no packed flag. – vlk Jun 10 '18 at 11:03
  • You are right, removing __attribute__((pack)) do the work, but why do you think that the structure will be packed without it too? The MCU is 32 bit architecture... – Mike Jun 10 '18 at 11:07
  • in this case will be packed correctly, because 16 bit type is aligned to 16 bit and 8bit type is aligned to 8 bits.. – vlk Jun 10 '18 at 12:54
  • Visual studio uses different language for packing structures and probably don't know GCC way, and this is reason why is this code marked as error. I don't have VS, so I'm not sure, it is only my opinion. look here: https://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed – vlk Jun 10 '18 at 12:59
  • About packed/non-packed - you're not right, in this case. Difference in 1-byte in size :) About the __attribute__((packed)) that not exists in VS, you're right! :) Thank you very much! If you post the answer, I mark it as the answer! ;) – Mike Jun 10 '18 at 13:14
  • Ah, yes, that is true, I forget for size, but location of items will be correct. sorry for confusion. – vlk Jun 11 '18 at 06:34

0 Answers0