This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

errors compiling ustdlib using C++



Example projects that #include ustdlib.h will generate compile errors when using the C++ compiler.

Even though the header file contains a #ifdef __cplusplus block that includes extern "C" declarations for the library functions, the declarations themselves contain errors.

First, several of the declarations make use of the restrict keyword. While this is a C99 keyword, it is not a C++ keyword.

Second, the declaration of unsprintf contains an extraneous pair of braces:

extern "C" {int usnprintf(char * s, size_t n, const char * format, ...)};

Moreover, the code in ustdlib.c contains the restrict keyword in the function definitions. The function definitions need to have #ifdef __cplusplus blocks to either include the restrict keyword or not.

Or perhaps it makes more sense to just remove the use of restrict completely to avoid the conflict between C and C++ options? I just added the following at the beginning of ustdlib.c:

#ifdef __cplusplus

#define restrict

#endif

  • Note also that if you want to fully build the usb_dev_serial project (and probably several other USB-related projects would be my guess) you also need to make a few other changes.

    In startup_ewarm:

    #if defined(__cplusplus) || defined(__embedded_cplusplus)

       extern "C" void USB0DeviceIntHandler(void);

    #else

       extern void USB0DeviceIntHandler(void);

    #endif

    and in usb_dev_serial.c the calls to GetLineCoding() and SetLineCoding() in ControlHandler() need to have casts applied to the pvMsgData parameter:

            // Return the current serial communication parameters.
            //
            case USBD_CDC_EVENT_GET_LINE_CODING:
            {
                GetLineCoding((tLineCoding *)pvMsgData, UART_CLOCK);
                break;
            }

            //
            // Set the current serial communication parameters.
            //
            case USBD_CDC_EVENT_SET_LINE_CODING:
            {
                SetLineCoding((tLineCoding *)pvMsgData, UART_CLOCK);
                break;
            }

  • Hello Dave,

    A silly question to ask, but are these changes that you are seeing on 2.1.1.71 release compounding the issue that you are facing?

    Regards
    Amit
  • Hi, Amit,

    I am not using ustdlib in my application, so there should not be any relationship between the issues in this thread and the problem with the usb not enumerating properly using the latest USB library. This thread is just the result of my trying to get the usb serial dev project running using C++, to see if there is some language-specific problem going on.
  • Hello Dave,

    Thanks for the clarification. I am trying to see why the C++ construct were not done.

    Regards
    Amit