[PLUGINS] Add 'extern "C"' wrappers to several header files
Ian Lance Taylor
iant@google.com
Tue Aug 25 15:20:00 GMT 2009
Duncan Sands <baldrick@free.fr> writes:
> I am writing a gcc plugin in C++. In order to use gcc's functions
> and objects from C++, they need to be declared as having C linkage.
> The attached patch wraps the body of various gcc header files with
> #ifdef __cplusplus
> extern "C" {
> #endif /* __cplusplus */
> ...
> #ifdef __cplusplus
> }
> #endif /* __cplusplus */
This approach fits poorly with --enable-build-with-cxx.
> I made no attempt to do this systematically: these are the headers
> I needed to modify for my plugin to link. At first sight it might
> appear that there is no need to modify gcc, since the plugin can
> include header files like this:
> extern "C" {
> #include "flags.h"
> }
> But in fact it cannot: some gcc header files include C++ aware
> system headers. For example, flags.h includes real.h, and real.h
> includes gmp.h, and gmp.h includes C++ system headers if compiled
> from C++. The result is that if I wrap the inclusion of flags.h in
> an 'extern "C"' wrapper then C++ code ends up with C linkage, and
> compilation fails (templates particularly don't like having C linkage).
One way to avoid this issue is to #include <gmp.h> before including any
gcc header files.
I agree that the current situation is less than idea but I don't think
that using extern "C" in the gcc header files is the right approach.
Ian
More information about the Gcc-patches
mailing list