Need help with C++ compile/linker

Hi,
Basic lack of knowledge problem I installed gmp on my Raspberry pi and am having trouble getting an executable. Here's what I've done....

Raspberry pi install and test of gmp

1) Downloaded gmp-6.2.1.tar.xz
2) Extracted files to ~/source/c++/gmp-6.2.1
3) installed m4
4) ran ./configure -enable-cxx
5) make
6) sudo make install
7) make check (22 test ran, 22 test passed
- no errors, no skip, no xfail, no fail, no xpass)

directory structure
~/source|
|- c++
|- gmp-6.2.1 (contains gmp.h, libgmp.la, and libgmpxx.la)
|- gmp_test1

In gmp_test1
g++ -c gmp_test1.cpp -lgmp
compiles with no errors displayed

g++ gmp_test1.o
messages
error: ld returned 1 exit status
get messages like
undefined reference to `__gmpz_init'

Seems like a path problem to the gmp-6.2.1 directory, but gosh
darn can I find this simple error/omission. What am I omitting/doing wrong etc.?
Where's a good place to read up on this? (Been a long time since my C++ days with OS/2.)

Code - copied from some poor soul on the web
1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;

int main()
{
	mpz_class a,b,c;
	a = 1234;
	b = "-5678";
	c = a + b;
	cout << "sum is " << c << endl;
	cout << "absolute value is " << abs(c) << endl;
	return 0;
}	
> g++ gmp_test1.o
Try
g++ gmp_test1.o -lgmp

> g++ -c gmp_test1.cpp -lgmp
-c and -l together make no sense.
-c means compile only, and -l is a linker option.
... and you need to include the gmp header in your code...
#include <gmpxx.h>

The origin of your code is from the gmp manual, above the code on that page is the includes and compile instructions. https://gmplib.org/manual/C_002b_002b-Interface-General

Also, in the terminal, double check that the header is in the correct place. On Ubuntu it is in /usr/include/gmpxx.h
I'm pretty sure the raspi should have it in the same place.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <gmpxx.h>
#include <iostream>
using namespace std;

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

g++ test.cpp -lgmpxx -lgmp
Last edited on
Topic archived. No new replies allowed.