GMP, C++ and extern "C"

Niels Möller nisse at lysator.liu.se
Mon Nov 20 19:13:41 CET 2006


Jeronimo Pellegrini <pellegrini at mpcnet.com.br> writes:

> The file in question was Nettle's rsa.h, which includes gmp.h.
> 
> The problem is that I'd already do:
> 
> extern "C" {
> #include <nettle/rsa.h>
> }
> 
> But then when rsa.h included gmp.h it broke:
> 
> /usr/include/gmp.h:2129: error: declaration of C function 'std::ostream& operator<<(std::ostream&, const __mpq_struct*)' conflicts with
> /usr/include/gmp.h:2128: error: previous declaration 'std::ostream& operator<<(std::ostream&, const __mpz_struct*)' here
> (more errors)

I think Emmanuel's point is that if nettle/rsa.h contained it's own
C++ guards, then it could do the right thing:

  nettle/rsa.h:

  /* Note: C++ linkage when included in a C++ program */
  #include <gmp.h>

  #ifdef __cplusplus
  extern "C" {
  #endif

  /* Plain C linkage */
  void rsa_foo(...);

You would have to remove the extern C from your C++ source files that
uses Nettle, which is inconvenient, but then it would just work.

I've more or less decided that this is the best way to solve the
problem.

/Niels


More information about the gmp-bugs mailing list