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

Please can you add extern "C" as appropriate to your header files?

Hi,

Many of us use C++ rather than C (as it is superior in every way). When including many of the Nordic SDK header files it is necessary to wrap them in

extern "C" {
}

to get the proper linkage. Otherwise you get cryptic error messages like "impossible constraint in ‘asm’".

The standard practice to avoid this necessity is to add the extern wrapper to the header code where appropriate like this:

#ifdef __cplusplus
extern "C" {
#endif

// ...

#ifdef __cplusplus
}
#endif

But you haven't done that in any of the headers. Please can you do so?

Parents
  • The header files don't have the extern "C" wrapping because the SDK isn't designed with C++ in mind. As long as that is the decision I don't think the header files should include C++ directives for mere convenience.

    I'm a bit unsure how you actually wrap from the question, but I see nothing wrong with something like this in a .cpp file:

    extern "C" {
    #include "sdklib.h"
    }
    

    Is that so difficult? I would prefer it like this this; it is a nice reminder that sdklib is in pure C, not C++ as the rest of the code.

Reply
  • The header files don't have the extern "C" wrapping because the SDK isn't designed with C++ in mind. As long as that is the decision I don't think the header files should include C++ directives for mere convenience.

    I'm a bit unsure how you actually wrap from the question, but I see nothing wrong with something like this in a .cpp file:

    extern "C" {
    #include "sdklib.h"
    }
    

    Is that so difficult? I would prefer it like this this; it is a nice reminder that sdklib is in pure C, not C++ as the rest of the code.

Children
Related