please wait

  • Naveen Shastri Mar-13-2019 07:05:40 AM ( 2 months ago )

    I have a C header as part of a C++ library.

    This C header would only make sense compiled by a C compiler, or by a C++ compiler within an extern "C" { ... } block, otherwise unresolved link errors would happen.

    I thought to add a block such as:

    #ifdef __cplusplus
    #error "Compiling C bindings with C++ (forgot 'extern \"C\"'?)"

    in the C header, but unfortunately the __cplusplus macro is defined also within an extern "C" { ... } block.

    Is there another way to detect this condition correctly?

  • Sarah Jones Mar-13-2019 07:06:58 AM ( 2 months ago )

    The common practice is not to demand client code wraps your header in extern "C", but to do so conditionally yourself. For instance:

    #ifdef __cplusplus
    extern "C" {
      // Header content
    #ifdef __cplusplus

    That way client code is automatically correct without doing anything beyond including the header.

Please login

Similar Discussion

Recommended For You