This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Keil C99 & C++ mix

Hello,

I have a base project that uses C99. (compiler option --c99)

Basically c99 was used so that an aggregate type could be initialized with non-constant data as such:

#define WRITE_OP_CODE (34)

void test_fn(unsigned char param1, unsigned char param2)
{
    unsigned char writebuff[3] = {WRITE_OP_CODE, param1, param2};
    // no error on one compiler
    // and a "expression must have a constant value" error on another

    ...
}

The problem is I am now mixing in some c++ (third part library) and I get all sort of errors for the C linkage (extern "c")

Does anyone have a suggestion as to how to mix C99 and C++ with various extern c linkage statements?

Parents
  • C99 uses the same linkage as C90 (aka C89) so the extern "C" issues should be the same.

    I can't tell what errors you are getting. Are the compiler errors or linker errors? Are you feeding 'extern "C"' to C99? You can't do that. The usual pattern for a header file is:

    ...
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    extern int myfn(int, int);
    
    ...
    #ifdef __cplusplus
    }
    #endif
    ...
    

    That way the 'extern "C"' is not seen in C or C99.

    Another style is

    #ifdef __cplusplus
    #define EXTERN_C extern "C"
    #else
    #define EXTERN_C extern
    #endif
    
    EXTERN_C int myfn(int, int);
    ...
    

Reply
  • C99 uses the same linkage as C90 (aka C89) so the extern "C" issues should be the same.

    I can't tell what errors you are getting. Are the compiler errors or linker errors? Are you feeding 'extern "C"' to C99? You can't do that. The usual pattern for a header file is:

    ...
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    extern int myfn(int, int);
    
    ...
    #ifdef __cplusplus
    }
    #endif
    ...
    

    That way the 'extern "C"' is not seen in C or C99.

    Another style is

    #ifdef __cplusplus
    #define EXTERN_C extern "C"
    #else
    #define EXTERN_C extern
    #endif
    
    EXTERN_C int myfn(int, int);
    ...
    

Children