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:
C code can be compiled as "C" and C++ code obviously as "C++". Then the two have to be linked. This use requiresextern "C" {...} around the C code headers included in C++.
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++:
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.)
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
The
extern "C"
directive has been removed from theqpc.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:
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++.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 useextern "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++:
extern "C"
directives already hard-coded in the QP/C header files.)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