Menu

qpc 7.3.4 compile fail with C++ code

faba
2024-05-31
2024-05-31
  • faba

    faba - 2024-05-31

    I am try to work with qpc 7.3.4, but it compile fail with C++ code, link fail because lack of extern "C" directive in qpc.h

     
  • Quantum Leaps

    Quantum Leaps - 2024-05-31

    The extern "C" directive has been removed from the qpc.h header file in the recent QP/C uploads to GitHub. The reasons are explained below:

    C and C++ code can be mixed in two different ways:

    1. C code can be compiled as "C" and C++ code obviously as "C++". Then the two have to be linked. This use requires extern "C" {...} around the C code headers included in C++.

    2. C code can be compiled as "C++" (and C++ code obviously as "C++"). Then the two have to be linked. This use does NOT require extern "C" {...}. In fact, the linking will FAIL when you use extern "C".

    Option 2 (compiling C code as C++) is attractive because the C++ compiler is more picky and applies stricter type checking rules than C. For this to be an option, the C code must be strict and clean enough so that it acutally compiles as C++, but QP/C code meets this requirement. Of course, to use this option, the C code must NOT use the extern "C" directive.

    So, now you have the following two options for mixing QP/C with C++:

    1. Compile QP/C as C++ (preferable). In this case you don't need to make any changes to QP/C or to your C++ code. The only change is in your build process. (Please note that this option would be previously impossible with the extern "C" directives already hard-coded in the QP/C header files.)
    2. Compile QP/C as C and apply the extern "C" directive around QP/C header file explicitly.

    The options are illustrated in the qpc/examples/posix-win32/dpp directory, where you can find three Makefiles:
    1. Makefile -- no-mixing of C with C++ (QP/C compiles as C and links with other C code)
    2. Makefile_mixed - mixing QP/C (compiled as C) with other C++ code (requires explicit extern "C") directive, as illustrated in the mixed.cpp file.
    3. Makefile_cpp - no mixing (QP/C compiled as C++) with other C++ code (no need for extern "C")

    --MMS

     

    Last edit: Quantum Leaps 2024-06-28

Log in to post a comment.