Search code examples
c++iocodeblocksgmp

CodeBlocks with gmp, segfault with << operator and mp*_class


I am using CodeBlocks with gcc 4.7.2 and gmp 5.0.5 on a Win 7 64 machine. After starting to use gmpxx I see a strange segfault which doesn't occur with the +,-,etc operators, but with << when trying to cout an mp*_class variable. I have to say that gmpxx works perfectly so far except for this.

For example:

#include <iostream>
#include <gmpxx.h>

using namespace std;

int main()
{
    mpz_class c = 21;
    cout << c << endl;
}

gives a segfault on the line with the cout, whereas the below code works fine:

#include <iostream>
#include <gmpxx.h>

using namespace std;

int main()
{
    mpz_class a = 3, b = 8, c;
    c = a + b;
    cout << c.get_str() << endl;
}

What is even stranger is that this code:

#include <iostream>
#include <gmpxx.h>

using namespace std;

int main()
{
    mpz_class a = 3, b = 8, c, d = 21;
    c = a + b;
    cout << c.get_str() << endl;
    cout << d << endl;
}

doesn't segfault when run, but shows only the first result (11) and then exits normally. On the other hand in debug it segfaults on: cout << d << endl.

I've googled for the last couple of days and found nothing similar to only some of the overloaded operators not working.

I would be thankful for an explanation.

I linked the two gmp libraries in codeblocks like this: Settings->Compiler and Debugger->Global Compiler Settings->Linker Settings and there I've added: C:\mingw\lib\libgmpxx.dll.a and C:\mingw\lib\libgmp.dll.a (in that order).

Nothing else was needed in order to compile c++ code with gmpxx.

Finally my CodeBlocks build log looks like this:

g++.exe -pg -g -pg -g -c "C:\Temp\test.cpp" -o .objs\test.o

g++.exe -o test.exe .objs\test.o -pg -lgmon -pg -lgmon C:\mingw\lib\libgmpxx.dll.a C:\mingw\lib\libgmp.dll.a

I honestly don't know why there are two switches of each.

If you need any more info, I'd be glad to provide. Thank you.


Solution

  • Alright, time to mark this as answered. The fact is that I installed gmp via mingw-get whereas almost everywhere in googleland it states to build it yourself for your own system. A silly mistake and thanks to the comment by @Lol4t0 it works fine now. So for all the new guys like me:

    1) Install MinGW with MSYS

    2) Download gmp source and extract to some folder in mingw\msys\1.0\home\

    3) open mingw shell and navigate to gmp folder

    4) ./configure --enable-cxx --prefix=/home/newgmpinstall

    5) make

    6) make install

    7) make check

    If it checks ok then in newgmpinstall you'll find the headers gmp.h and gmpxx.h and libraries libgmp.a and libgmpxx.a which work for your system.

    You can move them to a new folder if you wish. Then in your IDE Project properties add the *.a files to your link libraries and the folder with the *.h files to the compiler search directories.

    Write code

    Note: At first ./configure exited with an error about M4 missing because I was missing the M4 package. Just download the source for M4 and do the above steps first for M4 and then install gmp.