Problem with GMP and a user defined namespace

whatever at buettnerdavid.de whatever at buettnerdavid.de
Fri Jul 17 10:04:39 CEST 2009


Hello all,

my name is David Büttner and I am trying to use the GMP Library in a  
program I am writing.
Due to the fact that existing code which I am using, includes gmp.h  
and gmpxx.h inside a new defined namespace I need to do the same in my  
code:

namespace GMP{
#include "gmp.h"
#include "gmpxx.h"
}


While this works with other libraries I installed it gives me trouble  
with gmp.
Before I describe the problem here my "surroundings":
I am using a Debian Linux PC and installed gmp 4.3.1 in my  
home-directory (/home/buettner/installs/gmp/)
make check on gmp ran through fine and the use of gmp without the  
namespace works fine.

The test code with which I already have the compilation problems is this:


---test.hh-------------
ifndef __test_hh__
#define __test_hh__

#include <iostream>
using namespace std;

namespace GMP{
#include "gmp.h"
#include "gmpxx.h"
}
int test(int a);
int main();

#endif
------------------------

---test.cc-----------
#include "test.hh"
#include <iostream>
int test(int a){
     std::cout << "Hello2" << std::endl;
     return 1;
}

int main(){
     std::cout << "Hello1" << std::endl;
     return 0;
}

---------------------

I am using GCC 4.1.2 and am trying to compile with:

g++ -I /home/buettner/installs/gmp/include/ test.cc

I get a whole bunch of error messages all having to to with strings  
and the functions defined on them. The first few lines of error  
messages are:

/home/buettner/installs/gmp/include/gmpxx.h: In destructor  
?GMP::__gmp_alloc_cstring::~__gmp_alloc_cstring()?:
/home/buettner/installs/gmp/include/gmpxx.h:1367: error: ?strlen? is  
not a member of ?GMP::std?
/home/buettner/installs/gmp/include/gmpxx.h: At global scope:
/home/buettner/installs/gmp/include/gmpxx.h:1584: error: expected  
unqualified-id before ?&? token
/home/buettner/installs/gmp/include/gmpxx.h:1584: error: expected ?,?  
or ?...? before ?&? token
/home/buettner/installs/gmp/include/gmpxx.h:1584: error: ISO C++  
forbids declaration of ?parameter? with no type

In addition if I comment out the line "using namespace std;" in  
test.hh, I also get the following error (before the errors mentioned  
above):

/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:54:  
error: expected class-name before ?{? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:55:  
error: ?string? does not name a type
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:60:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:76:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:83:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:91:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:108:  
error: expected class-name before ?{? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:109:  
error: ?string? does not name a type
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:114:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:129:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:136:  
error: expected ?,? or ?...? before ?&? token
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:143:  
error: expected ?,? or ?...? before ?&? token


Why is the compiler looking for the string stuff in GMP::std and not in std?
The reason I know that the problem is with anything concerning string is:
If I edit gmpxx.h in a way that removes std:: before everything  
concerning strings and leave std:: in front of all the exception  
statements, the program compiles just fine.

Am I missing something here? I just don't know where to look any more  
or even what I should be looking for. All my research in the internet  
explained to me that, yes, I can create namespaces around a library I  
am using to reduce effects of what happens inside the library.
Is there a compiler option I can use to help this along? Is there a  
problem in my code? Is this something gmp-specific? Or is there  
something with my environment not right?

Attached is a textfile ouput.test containing the output of the  
compilation of the code as printed above.
Also attached the textfile output_no_using_namespace_std.test  
containing the output of the compilation of the code without the using  
namespace std; in the .hh file.

One other thing I realized here is that while the first error messages  
in both cases concerning gmpxx.h are in the same lines of code, the  
last ones point to quite different lines in the gmpxx.h code.


It would be great if someone could help me with this problem! Please  
excuse if this problem is trivial and I have missed something. I do  
not have much experience with namespaces and I have been looking for  
an answer myself for over 2 days now...


Thanks for your help,
best regards,
David


-------------- next part --------------
A non-text attachment was scrubbed...
Name: output.test
Type: application/octet-stream
Size: 12116 bytes
Desc: not available
URL: <http://gmplib.org/list-archives/gmp-discuss/attachments/20090717/3cabd727/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: output_no_using_namespace_std.test
Type: application/octet-stream
Size: 20450 bytes
Desc: not available
URL: <http://gmplib.org/list-archives/gmp-discuss/attachments/20090717/3cabd727/attachment-0003.obj>


More information about the gmp-discuss mailing list