[SOLVED] SES 4.20a: g++ compiler error from "offsetof"

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] SES 4.20a: g++ compiler error from "offsetof"

    Hello, I am evaluating SES 4.20a. I had a similar issue some weeks ago with SES 4.18 but didn't investigate further because there was no free time then.
    Trying to compile the following source code:

    C Source Code

    1. #include <cstddef>
    2. struct MyStruct
    3. {
    4. int x;
    5. int y;
    6. };
    7. enum EOffsets
    8. {
    9. OfsX = offsetof(MyStruct, x) // compiler error
    10. , OfsY = offsetof(MyStruct, y) // compiler error
    11. };
    12. const int ofsx = offsetof(MyStruct, x); // no error
    13. int main()
    14. {
    15. return 0;
    16. }
    Display All

    I get this from the compiler:

    Source Code

    1. Building ‘Hello’ from solution ‘Hello’ in configuration ‘Debug’
    2. 2> Compiling ‘main.cpp’
    3. 2> "C:/Program Files/SEGGER/SEGGER Embedded Studio for ARM 4.20a/gcc/arm-none-eabi/bin/cc1plus" -fmessage-length=0 -fno-diagnostics-show-caret -fno-exceptions -fno-rtti -mcpu=cortex-m3 -mlittle-endian -mfloat-abi=soft -mthumb -mtp=soft -munaligned-access -nostdinc "-isystemC:/Program Files/SEGGER/SEGGER Embedded Studio for ARM 4.20a/include" "-isystemC:/Users/achim.oetringhaus/AppData/Local/SEGGER/SEGGER Embedded Studio/v3/packages/include" -I. -D__SIZEOF_WCHAR_T=4 -D__ARM_ARCH_7M__ -D__SES_ARM -D__HEAP_SIZE__=1024 -D__SES_VERSION=42001 -D__SEGGER_LINKER -DDEBUG -MD "C:/Users/achim.oetringhaus/Documents/SEGGER Embedded Studio for ARM Projects/Hello/Output/Hello Debug/Obj/main.d" -MQ "Output/Hello Debug/Obj/main.o" -quiet -std=c++11 -g3 -gpubnames -fomit-frame-pointer -fno-dwarf2-cfi-asm -fno-builtin -ffunction-sections -fdata-sections -fshort-enums -fno-common "C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp" -o "C:/Users/achim.oetringhaus/Documents/SEGGER Embedded Studio for ARM Projects/Hello/Output/Hello Debug/Obj/main.asm"
    4. 2> In file included from C:/Program Files/SEGGER/SEGGER Embedded Studio for ARM 4.20a/include/cstddef:47,
    5. 2> from C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:1:
    6. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:11:9: error: dereferencing a null pointer in '*0'
    7. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:11:9: error: conversion from pointer type 'int*' to arithmetic type 'size_t' {aka 'unsigned int'} in a constant expression
    8. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:11:9: error: enumerator value for 'OfsX' is not an integer constant
    9. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:12:11: error: dereferencing a null pointer in '*0'
    10. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:12:11: error: conversion from pointer type 'int*' to arithmetic type 'size_t' {aka 'unsigned int'} in a constant expression
    11. 2> C:\Users\achim.oetringhaus\Documents\SEGGER Embedded Studio for ARM Projects\Hello\main.cpp:12:11: error: enumerator value for 'OfsY' is not an integer constant
    12. Build failed
    Display All

    The project settings are default except for -std=c++11. When I use the default language standard (gnu98), it doesn't compile either, however, the error messages are different.

    Curiously, the compiler complains about lines 11 and 12, but not 15. With SES 4.12 (which I still use as my productive tool) and all versions before, the compiler does not complain at all.

    The effective definition of offsetof is apparently this one, from stddef.h, with all versions of SES.

    C Source Code

    1. #define offsetof(s,m) ((size_t)&(((s *)0)->m))

    What is wrong with using offsetof this way? What can I do to compile this? I have this phrase in lots of places in my existing code. Rewriting them all would be quite a piece of work.

    Thank you for your help,
    Achim
  • After some research-and-try, I found out that using

    C Source Code

    1. #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
    helps out which is apparently widely recommended in recent writing and which is actually supported by the GCC version included in SES 4.20. One question left: Is there a reason why your environment still delivers the old definition of offsetof?

    Regards,
    Achim.
  • Hi,

    Sorry for the delay in response. This should be fixed since ES V4.24.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.