Compile error with lte_lc on nrf Connect SDK v1.9.0 with c++

Hello,

With UDP Sample switched to c++ (main.c -> main.cpp and added CONFIG_CPLUSPLUS=y, CONFIG_LIB_CPLUSPLUS=y) i get compilation error on including lte_lc.h

In file included from d:\Projects\Zen\UDPSampleApp\src\main.cpp:9:
C:\Users\nicolae.georgescu\ncs\v1.9.0\nrf\include\modem\lte_lc.h:640:10: error: 'struct lte_lc_periodic_search_pattern::<unnamed union>::lte_lc_periodic_search_range_cfg' invalid; an anonymous union may only have public non-static data members [-fpermissive]
640 | struct lte_lc_periodic_search_range_cfg {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\nicolae.georgescu\ncs\v1.9.0\nrf\include\modem\lte_lc.h:689:10: error: 'struct lte_lc_periodic_search_pattern::<unnamed union>::lte_lc_periodic_search_table_cfg' invalid; an anonymous union may only have public non-static data members [-fpermissive]
689 | struct lte_lc_periodic_search_table_cfg {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I am using the latest sdk v1.9.0 from Toolchain Manager.

Do you have any suggestions?

Thanks,

Nicolae

Parents
  • Hi,

    It seems to be related to anonymous unions and structs, and how they can be nested, in various versions of C and C++, although I am not 100 % sure what is the exact issue.

    In any case:

    Are the file change from main.c to c++ and added configs CONFIG_CPLUSPLUS and CONFIG_LIB_CPLUSPLUS the only changes you have done to the project?

    What toolchain and compiler (including version numbers) are you using?

    It may be for instance that the compiler is set to use C98 for the extern C portions while the code is only valid for C11, or that you need to add some compiler extensions for it to work.

    Regards,
    Terje

  • Hello! For me it was the HTTPS example. And yes, just added these two config options, renamed the main file to main.cpp, and updated the makefile to point to main.cpp instead of main.c

    I'm also using Windows, and tested with the nRF Connect SDK v1.8.0 and v1.9.0

  • Hi Terje,

    Sorry for the delay.

    I checked a bit more, and it seems that according to C++ declaration of unions (https://en.cppreference.com/w/cpp/language/union) says "Anonymous unions have further restrictions: they cannot have member functions, cannot have static data members, and all their data members must be public."

    This means the error "an anonymous union may only have public non-static data members" is actually against the C++ definition of anonymous unions. Using -fpermissive doesn't sound right.

    The fix seems simple though, and following similar union usage, I have edited lte_lc.h by just declaring struct lte_lc_periodic_search_range_cfg and lte_lc_periodic_search_table_cfg outside the struct lte_lc_periodic_search_pattern, and inside kept:

    union {
    struct lte_lc_periodic_search_range_cfg range;
    struct lte_lc_periodic_search_table_cfg table;
    };

    File attached. Can you please comment on this and update in the sdk?

    Much appreciated,

    lte_lc_v191_withfix.h

    Nicolae

Reply
  • Hi Terje,

    Sorry for the delay.

    I checked a bit more, and it seems that according to C++ declaration of unions (https://en.cppreference.com/w/cpp/language/union) says "Anonymous unions have further restrictions: they cannot have member functions, cannot have static data members, and all their data members must be public."

    This means the error "an anonymous union may only have public non-static data members" is actually against the C++ definition of anonymous unions. Using -fpermissive doesn't sound right.

    The fix seems simple though, and following similar union usage, I have edited lte_lc.h by just declaring struct lte_lc_periodic_search_range_cfg and lte_lc_periodic_search_table_cfg outside the struct lte_lc_periodic_search_pattern, and inside kept:

    union {
    struct lte_lc_periodic_search_range_cfg range;
    struct lte_lc_periodic_search_table_cfg table;
    };

    File attached. Can you please comment on this and update in the sdk?

    Much appreciated,

    lte_lc_v191_withfix.h

    Nicolae

Children
Related