problem with using gnu mp on ubuntu 7.04

*reposted here (also posted in 'general c++ programming') due to confusion

My system has:
- ubuntu 7.04
- code::blocks IDE w/ gcc compiler
- gmp-4.2.2.tar.gz which is downloaded here >> http://gmplib.org/#DOWNLOAD

I installed ubuntu (and has been using this for just a week) for the sake of using the library on a Linux-based system after seeing all the necessary files are built to be used on Linux-based systems (correct me if I'm wrong).

I followed the instructions on this page > http://gmplib.org/manual/Installing-GMP.html#Installing-GMP < on the Konsole and no errors were found.

The header (gmp.h) is then installed in /usr/local/include/gmp.h.

I then assumed that the library was installed successfully, but after I compiled the code below, an error had occurred (error msg is below the code).

1
2
3
4
5
6
7
8
9
10
11
#include <gmp.h>
#include <iostream>

using namespace std;

int main()
{
    mpz_t gmpInt;
    mpz_init(gmpInt); //This is line 9.
    return 0;
}


ERROR:
- line 9 -- undefined reference to '__gmpz_init'


I re-ran the installation commands on the manual but the error has not been fixed.

Is it a problem with these commands (not updated files for 'make' or something else)?

./configure
make

// I ran make check before make install and the output tells me that
// it had passed all tests.
make install

What does the error mean? Was the library miscompiled? If so, why was it miscompiled and what are the proper procedures?

A helpful reply would be VERY much appreciated because I am stuck with this problem for about 4 days now.
Last edited on
I have never used GMP however,
The function you are calling on line 9 will almost certainly be a preprocessor macro for __gmpz_init buried somewhere in the header files. I suspect you are missing some of these header files or more likely the compiler doesn't know where to look for them.

Check to make sure you are passing all the necessary search directories to the compiler (-I option)
Thanks for the reply, bnbertha. Would you kindly show me how it is done because I am not yet familiar with...

Check to make sure you are passing all the necessary search directories to the compiler (-I option)


On which of these will I use the -l option?
1
2
3
4
./configure
make
make check
make install
Last edited on
No, in won't be in any of them, it's when you compile your own code.

Part of the compile command will have one or more -I<some directory> in it. These directories tell the compiler where it can look for header files. If your GMP headers are not in one of the listed directories then you need to add more -I's
When you type g++ -v -c <your-program-name.cpp>. The output will also display the directory which will be searched for the header files.
Here is the output I got on my Linux machine.
$ gcc -v -c test.cpp
.......
......

#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2
/usr/include/c++/4.2/i486-linux-gnu
/usr/include/c++/4.2/backward
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.2.3/include
/usr/include
End of search list.
......
.......
so you can check if the header file gmp.h is there in any of the listed directories.
Alternately you can search your PC for gmp.h file and copy the entire header files to /usr/include as this directory will be used almost all the times by the complier.

Hope this helps.
You problem is not with compiling but linking. You need to tell g++ where to find also the library. You can configure codeblocks to do that for you. The weird things in front the function name is due to C++ code. C++ compilers put things in front of functions names. See this link for more:http://en.wikipedia.org/wiki/Name_mangling

Take care.
+1 for modal's answer. It's a linker problem.
Topic archived. No new replies allowed.