开发者

Problem using GMP with Eclipse C++ (CDT Helios)

I would like to compile my code in CDT:

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

using namespace std;

int main (void) {
    mpz_class a, b, c;
    a = 1234;
    b = "-5678";
    c = a+b;
    cout << "sum is " << c << "\n";
    cout << "absolute value is " << abs(c) << "\n";
    cin >> a;
    return 0;
}

When I compiled, there were some errors like this:

**** Build of configuration Debug for project test ****

**** Internal Builder is used for build               ****
g++ -LC:\Dev-Cpp\lib -lgmp -lgmpxx -otest.exe src\test.o
src\test.o(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_ED1Ev[__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr()]+0xd): In function `_gmpz_abs':
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefi开发者_开发问答ned reference to `__gmpz_clear'
src\test.o(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EaSEPKc[__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::operator=(char const*)]+0x55):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_set_str'
src\test.o(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EaSEi[__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::operator=(int)]+0x14):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_set_si'
src\test.o(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EC1Ev[__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr()]+0xd):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_init'
src\test.o(.text$_ZlsIA1_12__mpz_structERSoS2_RK10__gmp_exprIT_S4_E[std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1]>(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&)]+0x1c):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
src\test.o(.text$_ZlsIA1_12__mpz_struct16__gmp_unary_exprI10__gmp_exprIS1_S1_E18__gmp_abs_functionEERSoS7_RKS3_IT_T0_E[std::basic_ostream<char, std::char_traits<char> >& operator<< <__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> >(std::basic_ostream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0x6b):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
src\test.o(.text$_ZrsIA1_12__mpz_structERSiS2_R10__gmp_exprIT_S4_E[std::basic_istream<char, std::char_traits<char> >& operator>><__mpz_struct [1]>(std::basic_istream<char, std::char_traits<char> >&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)]+0x1c):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `operator>>(std::istream&, __mpz_struct*)'
src\test.o(.text$_ZN10__gmp_exprIA1_12__mpz_structS1_EC1IS1_16__gmp_unary_exprIS2_18__gmp_abs_functionEEERKS_IT_T0_E[__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> >(__gmp_expr<__mpz_struct [1], __gmp_unary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, __gmp_abs_function> > const&)]+0xd):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_init'
src\test.o(.text$_ZN17__gmp_binary_plus4evalEP12__mpz_structPKS0_S3_[__gmp_binary_plus::eval(__mpz_struct*, __mpz_struct const*, __mpz_struct const*)]+0x1b):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_add'
src\test.o(.text$__gmpz_abs[___gmpz_abs]+0x1d):C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/gmpxx.h: undefined reference to `__gmpz_set'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1514  ms.

What must I do? I've added libgmp.a and libgmpxx.a in GCC Linker but still get those problem. Thanks a lot for your answer. :)


Here is the correct procedure for setting up the current (as of 7/2/13) GNU bignum libraries with Eclipse CDT, MinGW, and msys for C++. To get through this, you should have used Unix or Linux before, as well as Windows, and you should have a vague recollection of programming and compiling programs. This is the culmination of over a week of research and hardcore frustration, so if I messed something up note it politely or I will blow you up with the power of my mind!

1) I assume you have already downloaded and installed Eclipse and MinGW and have installed msys into MinGW. You must install MinGW before msys!

2) Download the tarball for the GMP libraries from gmplib.org to ${gmp_download}. I downloaded the gmp-5.1.2.tar.xz because I have never used lzip and didn't know if it was available in msys.

3) Open up an msys window (essentially a bash shell). cd ${gmp_buid} and tar -Jxvf ${gmp_download}/gmp-x.x.x.tar.xz

Those tar options are different from what you may find elsewhere on the web! -Jxvf is right for xz (and I think lzip), but for gzip you use -xzvf.

4) cd gmp-x.x.x and run ./config.guess. Write down the output. You will need it next.

5) Run ./configure --prefix=${gmp_build} --build= --enable-cxx --with-gnu-ld

Apparently if you don't explicitly tell GMP to build for your platform it builds everything, which is bad. The cxx option builds the C++ libraries and --with-gnu-ld allows it to work with ld. Pretty straightforward.

6) make

7) make install

EX: suppose you installed to C:/gmp. You should have gmp/include/gmp.h and gmpxx.h. You should also have gmp/lib/libgmp.a, libgmp.la, libgmpxx.a, libgmpxx.la. You should also have a share directory with stuff in it.

8) Set up eclipse:

Go to project --> properties Under C/C++ build --> Environment edit the PATH variable and add ${gmp_build}/include;${gmp_build}/lib Under C/C++ build --> settings --> tool settings --> GCC Assembler --> general add ${gmp_build}/include as an include path. Same place but --> GCC C++ compiler --> Includes add ${gmp_build}/include as an include path. Same place --> GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line. THE END OF THE LINE! Same place --> GCC C compiler Add the same include paths and miscellaneous options as before. Same place --> MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER! Now add ${gmp_build}/lib to "LIbrary Search Path (-L)" Under C/C++ General --> Paths & Symbols --> Incudes Tab check that you have ${gmp_build}/include in your include directories for Assembly, C, and C++. If they aren't there you may have messed up an earlier step. They should be auto populated by Eclipse. Same place --> Libraries Tab check that you have gmp and gmpxx IN THAT ORDER. It should already be populated. Same Place --> Library Paths Tab Check for ${gmp_build}/lib which should already be there. Hit "Apply" and make sure you rebuild the index or the changes won't take. Hit OK to close out. 9) Run this short program to verify your setup:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>

using namespace std;

int main ()
{
    mpz_t p;
    mpz_init_set_ui (p,3);

    return 0;
}

Your compile commands should look similar to this:

g++ "-IC:\gmp\include" -O0 -g3 -Wall -c -fmessage-length=0 -lgmp -lgmpxx -o main.o "..\main.cpp" g++ "-LC:\gmp\lib" -o GMPDebug.exe main.o -lgmp -lgmpxx

Notes:

1) The order of the options is important. I don't know all of the whys, but if the second command line (which links the program) has the -lgmp -lgmpxx flags before the -o option, the linking will fail miserably.

2) The -l flag is a tricky one. It actually says "Go look in -L for liblibrary.a". In this case "Go look in C:\gmp\lib for libgmp.a and libgmpxx.a".

3) I have heard of bugs involving cout and the 64 bit version of eclipse, so I am using the 32 bit version, where I am seeing the same bug. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜