-1

I'm doing a project for school in which I am doing all kinds of different calculations involving prime numbers. These numbers tend to go quite big, hence I went looking for an arbitrary precision library. I decided to go for GMP since I had used it earlier in Game Maker (a relatively unknown program) since someone had made a dll for it.

Now, I have followed the install manual and went ahead to compile GMP. I had great difficulty in doing this as I am totally unfamiliar with UNIX and cygwin. Now that I have tried to include gmpxx.h in Netbeans for a test program, things are going wrong. My code is as follows:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <gmpxx.h>
#include <stdarg.h>

using namespace std;

int main()
{
    mpz_t a;
    mpz_init(a);
    //cout << mpz_probab_prime_p(a,20);
    mpz_clear(a);
}

For both mpz_init and mpz_clear I am getting the same error:

relocation truncated to fit: R_X86_64_PC32 against undefined symbol '__gmpz_[init/clear]'

I am just guessing, but the problem could be any of the following:

  • Wrongly compiled
  • Bad code
  • Improper includes/links

It could very well be the latter, although I have experimented with adding directories for the header files and such. How would I fix this error?

Thanks in advance!

Edit: Since this is my first post, could you point out what I need to clarify in order to make this question answerable?

Edit2: This is the compiling log in Netbeans:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe
make[2]: Entering directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++ -Lpath/to/gmp/lib -lgmpxx -lgmp    -o dist/Debug/Cygwin_4.x-Windows/firsttestgmp build/Debug/Cygwin_4.x-Windows/main.o 
build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22: undefined reference to `__gmpz_init'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__gmpz_init'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:24: undefined reference to `__gmpz_clear'
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:24:(.text+0x21): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__gmpz_clear'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/firsttestgmp.exe] Error 1
make[2]: Leaving directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

Edit3: As @rubenvd pointed out, the real error is this:

build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/e/Documents/NetBeansProjects/FirstTestGMP/main.cpp:22: undefined reference to `__gmpz_init'
Jelle
  • 36
  • 6
  • You should include the compilation command and the complete error you get. That will make it a lot easier for us to discover what you're doing wrong. – rubenvb Oct 13 '14 at 19:38
  • @rubenvb I'm very sorry I didn't see your comment. My compilation line is now the following: `-Lpath/to/gmp/lib -lgmpxx -lgmp -g` I think you could see that in the log too, though. – Jelle Oct 13 '14 at 22:41
  • your real error is `undefined reference to '__gmpz_init'`. That's why you need to always show the _full_ error ;-). – rubenvb Oct 14 '14 at 07:14
  • I have changed it in they main post, thanks! – Jelle Oct 14 '14 at 12:47

2 Answers2

0

It's important that linked libraries come after the object files which contain references to them. So Your compile command will need to be:

g++ -o dist/Debug/Cygwin_4.x-Windows/firsttestgmp build/Debug/Cygwin_4.x-Windows/main.o -lgmpxx -lgmp

If you installed GMP in a nonstandard location (which doesn't seem to be the case), you'll need to add the path to the library directory using the -L option.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Thanks so much for your answer! I did have `-lgmpxx -lgmp` but not the Lpath so I added that. It's still not working compiling , though. I will hereby include the compiler log, in case that helps. It is in the main question. Thanks again! – Jelle Oct 13 '14 at 22:21
  • @Jello The `-Lpath/to/gmp/lib` was only a placeholder. In reality it might be something like `-L/opt/gmp/lib`. But as you're using Cygwin you most likely don't need it anyway. – rubenvb Oct 14 '14 at 07:15
  • Thanks, I will try to make it refer to the right directory and see whether this gives better results. – Jelle Oct 14 '14 at 12:45
  • Unfortunately, it hasn't changed anything. I also tested adding the -I flag, which had the same result. – Jelle Oct 14 '14 at 13:47
  • `nm -D /path/to/gmp/lib/libgmp.so|grep mpz_init` (or `libgmp.a` without `-D`) check if you are getting `__gmpz_init` (double-check the number of leading `_`). – Marc Glisse Oct 14 '14 at 13:56
  • 1
    @Jelle did you place the `-lgmpxx -lgmp` at the end of your command? – rubenvb Oct 14 '14 at 13:58
  • There is no directory lib and there is no file libgmp.so. Neither is there a file libgmp.a. I'm pretty sure it is not. If it has to be at the end, may I ask you how to do that? Thanks for the answers! – Jelle Oct 19 '14 at 17:03
  • In my last comment I was referring to what @rubenvb said, namely: "did you place the -lgmpxx -lgmp at the end of your command?" – Jelle Oct 19 '14 at 18:33
  • @rubenvb What does my compilation line have to look like and how do I change it in Netbeans? Could you give an example please? – Jelle Oct 20 '14 at 19:47
0

Thanks everyone! I fixed my problem by going to: Properties > Build > Linker > Libraries > Add Library Then adding gmp.a and gmpxx.a, which I found in the gmp directory that I compiled.

Jelle
  • 36
  • 6