GMP, C++ and extern "C"

Niels Möller nisse at
Sun Nov 19 22:15:25 CET 2006


I have a pure C library, nettle, that uses GMP. To use it from within
a C++ program, the recommended way is to wrap the inclusion of any
nettle header files within extern "C", for example

  extern "C" {
  #include <nettle/rsa.h>
  int main (int argc, char **argv) {
    return 0;

However, when compiled with a C++ compiler, this fails with error
messages such as

  /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++/4.1.1/i686-pc-linux-gnu/bits/c++locale.h:55: error: template with C linkage


  /usr/local/include/gmp.h:2129: error: declaration of C function 'std::ostream& operator<<(std::ostream&, const __mpq_struct*)' conflicts with
  /usr/local/include/gmp.h:2128: error: previous declaration 'std::ostream& operator<<(std::ostream&, const __mpz_struct*)' here

The reason is that nettle/rsa.h includes gmp.h, and

  extern "C" {
  #include <gmp.h>

doesn't work, since it checks __cplusplus and decides to do C++
specific things, which then doesn't work very well within extern "C".

Is there some simple way to include gmp.h, from a C++ program, and say
that "I want the good old C API only, no C++ specifics"? Or can gmp.h
somehow be fixed so that

  extern "C" {
  #include <gmp.h>

is supported?

I'd prefer if the nettle include files (and other GMP-using C
libraries, for that matter) can stay ignorant of C++ details, since
this is a C library with no C++ specific features.

When testing this, I'm using GMP 4.1.4, and g++ 4.1.1, on an x86
debian gnu/linux box.


More information about the gmp-bugs mailing list