This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

copy assignment not allowed in union

I am attempting to do some development using the usbd_hid_generic example peripheral code. I am using some C++ libraries in this project, however there is syntax in the usbd code (specifically in the app_usbd_class_base.h file) that fails to compile. This is making it difficult for me to be able to integrate my new code with the old code. To simplify things, I will just outline changing the main.c file to main.cpp in the base example program and describe what happens. If this can be fixed, I should be able to work out the rest. I am using gcc-arm-none-eabi for compilation on a Linux OS and I am using SKD v. 13, though I have also tried this under v. 14 with the same results.

Step 1: Rename main.c to main.cpp and change the corresponding entry in the Makefile.

Step 2: Compile the code - fails.

The first error is: [...]/app_usbd_class_base.h:674:5: sorry, unimplemented: non-trivial designated initializers not supported

According to the following, it seems it should be possible to compile using older versions of C++, but designated initializer lists are not supported in current C++ standards. stackoverflow.com/.../why-does-c11-not-support-designated-initializer-lists-as-c99

I attempted a workaround by setting -std=c++98, but that resulted in another error.

There is an additional error: [...]/app_usbd_class_base.h:586:11: error: member 'app_usbd_hid_mouse_u:: app_usbd_hid_mouse_u::specific' with copy assignment operator not allowed in union.

Possible options:

I am looking for how to integrate the usbd libraries within a C++ project. If people have some insights, please share. So far, I figure some workarounds are for me to set up code to implement the C++ libraries I am working with in C, or I can write access functions to wrap C++ code in extern "C" blocks and leave the usbd code calls in a C context. I think I will try the second option first, but it seems that it should be more straightforward to use Nordic usbd libraries within C++ projects.

Parents
  • I will eventually be using some custom C++ code to communicate with some peripheral devices, but in the context of this question, the problem can be demonstrated without introducing any additional code to the usbd_hid_generic peripheral example. Some of the code in the original app_usbd_class_base.h is incompatible with the C++ standards as enforced by the arm-none-eabi-g++ compiler.

    In order to work around the problem, I implemented option 2 from my original post. I reverted main back to a C program and I wrote a new wrapper.h and wrapper.cpp to interface the C++ classes in the libraries that I was using. These wrapper functions took Class::f(x,y,z) and made a C-style function class_f(void* instance, x, y, z) that could bridge the gap. Using extern "C" here provides C-style hooks. Since all calls to the usbd are exclusively in main.c, compiled by gcc, this avoids the problem.

Reply
  • I will eventually be using some custom C++ code to communicate with some peripheral devices, but in the context of this question, the problem can be demonstrated without introducing any additional code to the usbd_hid_generic peripheral example. Some of the code in the original app_usbd_class_base.h is incompatible with the C++ standards as enforced by the arm-none-eabi-g++ compiler.

    In order to work around the problem, I implemented option 2 from my original post. I reverted main back to a C program and I wrote a new wrapper.h and wrapper.cpp to interface the C++ classes in the libraries that I was using. These wrapper functions took Class::f(x,y,z) and made a C-style function class_f(void* instance, x, y, z) that could bridge the gap. Using extern "C" here provides C-style hooks. Since all calls to the usbd are exclusively in main.c, compiled by gcc, this avoids the problem.

Children
No Data
Related