Hi.
I was trying to compile this in C++:
........................ extern "C" { #include <nettle/rsa.h> }
int main () { exit (0); } ........................
But rsa.h includes gmp.h, and unfortunately, this won't work for C++ (at least not for g++) -- not even if declared 'extern "C"'. The <gmpxx.h> header should be used when using GMP from C++ programs.
If I remove the "include <gmp.h> from rsa.h, then I can compile, so long as I include gmpxx.h nefore rsa.h:
........................ extern "C" { #include <gmpxx.h> #include <nettle/rsa.h> // Removed #include <gmp.h> from this one }
int main () { exit (0); } ........................
It also happens with bignum.h, dsa.h and pkcs1.h.
It would be nice if we could call Nettle's rsa.h from C++. It would work with an #if:
#if defined(__cplusplus) #include <gmpxx.h> extern "C" { #else #include <gmp.h> #endif
. . /* The header declarations are here */ .
#if defined(__cplusplus) } #endif
But then it would be better to change all .h files so they could be directly used from C++, even those that do not include gmp.h:
#if defined(__cplusplus) extern "C" { #endif
Then, from C++, we'd just include all files as if they were C++ include files.
But you may also prefer to have specific C++ header files, like
rsaxx.h dsaxx.h pkcs1xx.h bignumxx.h
However, when calling from C++ we'd still need to have some headers inside an "extern "C"" block, and others not:
................. #extern "C" { include <nettle/yarrow.h> } #include <nettle/bignumxx.h> #include <nettle/dsaxx.h> .................
If there is interest, I could make a patch with either of these two solutions.
Thanks, J.
Jeronimo Pellegrini pellegrini@mpcnet.com.br writes:
But rsa.h includes gmp.h, and unfortunately, this won't work for C++ (at least not for g++) -- not even if declared 'extern "C"'. The <gmpxx.h> header should be used when using GMP from C++ programs.
That sounds kind of broken. The way I understand it, to use the C++ interface to GMP, you should include gmpxx.h, but if you want to use the plain old C interface from within a C++ program, then you should be able to use gmp.h from C++ programs.
What error messages do you get? This really sounds like a bug in gmp or in the gmp installation.
It would be nice if we could call Nettle's rsa.h from C++. It would work with an #if:
The intention is that
extern "C" { #include <nettle/rsa.h> }
should just work. If it doesn't, that's a bug in nettle or gmp or both.
If there is interest, I could make a patch with either of these two solutions.
First, please investigate why including gmp.h fails.
It has been suggested earlier that Nettle's include files should contain their own C++ guards. The main reason I haven't done that, is that I don't personally use C++, and I don't have any testcases for Nettle and C++. So test cases would be the first requirement for explicit C++ support in Nettle.
Regards, /Niels
PS. Sorry for the late reply, it's caused by a combination of computer problems and child birth ;-)
Hi Niels!
On Sun, Nov 19, 2006 at 06:42:14PM +0100, Niels Möller wrote:
Jeronimo Pellegrini pellegrini@mpcnet.com.br writes:
But rsa.h includes gmp.h, and unfortunately, this won't work for C++ (at least not for g++) -- not even if declared 'extern "C"'. The <gmpxx.h> header should be used when using GMP from C++ programs.
That sounds kind of broken. The way I understand it, to use the C++ interface to GMP, you should include gmpxx.h, but if you want to use the plain old C interface from within a C++ program, then you should be able to use gmp.h from C++ programs.
What error messages do you get? This really sounds like a bug in gmp or in the gmp installation.
/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 /usr/include/gmp.h:2130: error: declaration of C function 'std::ostream& operator<<(std::ostream&, const __mpf_struct*)' conflicts with /usr/include/gmp.h:2129: error: previous declaration 'std::ostream& operator<<(std::ostream&, const __mpq_struct*)' here /usr/include/gmp.h:2132: error: declaration of C function 'std::istream& operator>>(std::istream&, __mpq_struct*)' conflicts with /usr/include/gmp.h:2131: error: previous declaration 'std::istream& operator>>(std::istream&, __mpz_struct*)' here /usr/include/gmp.h:2133: error: declaration of C function 'std::istream& operator>>(std::istream&, __mpf_struct*)' conflicts with /usr/include/gmp.h:2132: error: previous declaration 'std::istream& operator>>(std::istream&, __mpq_struct*)' here
And this is, in gmp.h:
__GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpz_srcptr); __GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpq_srcptr); __GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpf_srcptr); __GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpz_ptr); __GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpq_ptr); __GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpf_ptr);
It seems that we can't include that line in gmp.h if you include it as extern "C".
It would be nice if we could call Nettle's rsa.h from C++. It would work with an #if:
The intention is that
extern "C" { #include <nettle/rsa.h> }
Yes... But for some reason, this works in C++:
#include <gmp.h>
And this doesn't:
extern "C" { #include <gmp.h> }
If there is interest, I could make a patch with either of these two solutions.
First, please investigate why including gmp.h fails.
It has been suggested earlier that Nettle's include files should contain their own C++ guards. The main reason I haven't done that, is that I don't personally use C++, and I don't have any testcases for Nettle and C++. So test cases would be the first requirement for explicit C++ support in Nettle.
Right...
Well, I found a (strange) solution to this.
#include <gmpxx.h>
extern "C" { #include <nettle/rsa.h> }
Seems to work, since gmpxx.h will include (properly) gmp.h.
Maybe just a note on the Nettle documentation would be nice? :-)
PS. Sorry for the late reply, it's caused by a combination of computer problems and child birth ;-)
Congratulations! :-)
J.
nettle-bugs@lists.lysator.liu.se