1

I installed gmp library (version 5.1.2) from source using mingw and msys.

This sample program, taken from Wikipedia:

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

int main()
{
    mpz_class x("7612058254738945");
    mpz_class y("9263591128439081");    

    std::cout << "\n    " << x << "\n*\n    " << y;
    std::cout << "\n--------------------\n" << x * y << "\n\n";

    return 0;
}

If I compile with mingw:

g++ -Wall -pedantic -O3 -I/c/Libs/GMP/include -L/c/Libs/GMP/lib hello_gmp.cpp -o hellocpp_gmp -lgmpxx -lgmp

it compiles and runs.

If I compile with visual studio 2010/2012 gives me the following error:

error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct __mpz_struct const *)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@PBU__mpz_struct@@@Z)

If I modify the code like this:

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

int main()
{
    mpz_class x("7612058254738945");
    mpz_class y("9263591128439081");
    mpz_class z("0");

    //std::cout << "\n    " << x << "\n*\n    " << y;
    //std::cout << "\n--------------------\n" << x * y << "\n\n";

    z = x * y;
    std::cout << "\n    " << x.get_str() << "\n*\n    " << y.get_str();
    std::cout << "\n--------------------\n" << z.get_str() << "\n\n";
    return 0;
}

it compiles and runs.

In the file "gmpxx.h" the << operator is defined as follows:

line 2054 about:

/ **************** I / O operators **************** /

// These Should (and will) be provided separately

template <class T, class U>
inline std :: ostream & operator <<
(std :: ostream & o, const __ gmp_expr U> & T, expr)
{
    __gmp_expr <T, T> const & temp (expr);
    return o << temp.__get_mp();
}

template <class T>
inline std :: istream & operator >> (std :: istream & i, __ gmp_expr <t & T, expr)
{
    return i >> expr.__get_mp ();
}

line 2880 about:

#define __GMP_DEFINE_BINARY_FUNCTION_UI(fun, eval_fun)                 \
                                                                   \
template <class T, class U>                                            \
inline __gmp_expr                                                      \
<T, __gmp_binary_expr<__gmp_expr<T, U>, mp_bitcnt_t, eval_fun> > \
fun(const __gmp_expr<T, U> &expr, mp_bitcnt_t l)                 \
{                                                                      \
    return __gmp_expr<T, __gmp_binary_expr                               \
    <__gmp_expr<T, U>, mp_bitcnt_t, eval_fun> >(expr, l);        \
}

line 3080 about:

__GMP_DEFINE_BINARY_FUNCTION_UI(operator<<, __gmp_binary_lshift)

Thank you very much.

1 Answers1

0

You're missing the gmpxx library when linking with VS2012. You probably remembered to include the gmp library in your VS library list. Put gmpxx there as well and you'll be fine.

us2012
  • 16,083
  • 3
  • 46
  • 62
  • I've included the reference in "Configuration properties/Linker/Input": odbccp32.lib;libgmpxx.a;libgmp.a;%(AdditionalDependencies) but the problem remains. – Vincenzo Lo Cicero Sep 08 '13 at 16:06
  • Next thing I would try is the top rated answer from this question ( http://stackoverflow.com/questions/2096519 ), that is, linking against mingw's libmsvcrt.a. If that doesn't help, I'm out of ideas unfortunately... – us2012 Sep 09 '13 at 11:10