Installing GMP on MacOS X with Xcode

12,210

Solution 1

I have done it this way:

Download GMP from https://gmplib.org/ (gmp-6.0.0a.tar.lz) Download lzip from http://www.nongnu.org/lzip/lzip.html (lzip-1.16.tar.gz)

  1. install lzip
    • extract it: tar -xvf lzip-1.16.tar.gz
    • cd lzip-1.16
    • ./configure
    • make
    • make check
    • make install (run it with sudo if you receive permission denied message)
  2. install gmp
    • copy the gmp-6.0.0a.tar.lz file to /usr/local/lib (do it with sudo)
    • extract the lz: lzip -d gmp-6.0.0a.tar.lz
    • extract the tar: tar -xvf gmp-6.0.0a.tar
    • cd gmp-6.0.0/
    • ./configure
    • make
    • make check
    • make install (run it with sudo if you receive permission denied message)

Solution 2

  1. If you install the library in /usr/local, you will be able to simply #include <gmp.h> and add -lgmp to your linker settings (Linking > Other Linker Flags).

  2. If you insist on putting the library in your home directory, say ~/local, then you will need to add ~/local/include to your header search paths (Search Paths > Header Search Paths), ~/local/lib to your library search paths (Search Paths > Library Search Paths). These are under the project or target settings. You will also need to add -lgmp as above.

Note about architectures: LibGMP is rather unique in that it will choose the target architecture at compile-time, and it usually chooses a 64-bit target where available. If your project is 32-bit and your GMP is 64-bit, linking will fail.

LibGMP does this because the kind of operations it does are much faster on 64-bit architectures. Multiplying large integers can be around 4x as fast on 64-bit as 32-bit.

Warning: Your project will not run on other people's computers unless they install GMP first.

Warning 2: If you statically link with GMP to simplify installation, you are required to open-source your application. Don't statically link unless you are okay with that.

Share:
12,210
user1483799
Author by

user1483799

Updated on June 08, 2022

Comments

  • user1483799
    user1483799 almost 2 years

    I'm trying to use the GMP library in my C and C++ programs. I do code using the Xcode. I followed the instructions to install the GMP on my machine and I end up with a folder "~/usr/local/gmp-5.0.5" which contains the gmp.h file.

    How can I make my programs to see this library? if I decided to save my programs on desktop for example???

    Where is the best place to install the gmp-5.0.5??? Using include "gmp.h" will show the error "file not found".

  • Mihai Todor
    Mihai Todor over 11 years
    Regarding the warning: he could just link statically to GMP, in which case people will not need to install it in order to run the application.
  • Dietrich Epp
    Dietrich Epp over 11 years
    @MihaiTodor: If he statically links to the GMP, he has to open-source his application.
  • William
    William over 8 years
    We could download the gmp-6.0.0a.tar.bz2, rather than the *.lz version. The command to extract bz2 is bzip2 -d -k gmp-6.0.0a.tar.bz2. The following steps are the same.