0

I'm new to c++ and don't understand how to install a library on Linux (Mint). I want to use the GNU GMP library:https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library I downloaded the tar.lz file and installed it with

./configure
make
sudo make install

If I try to compile it, I get the error message that the header file "gmpxx.h" wasn't found. Where can I find this file? How do I compile it with the -lgmpxx -lgmp flags? I tried something like:

g++ test.cpp -o test -lgmpxx -lgmp
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
llTtoOmMll
  • 67
  • 2
  • 6
  • 3
    On linux, for most software, you should not build it yourself with configure/make/make install, you should use your package manager and tell it you want libgmp-dev. – Marc Glisse Jul 22 '18 at 21:57

1 Answers1

1

If the library is using the Autoconf system (which your does) then the default installation prefix is /usr/local.

That means libraries are installed in /usr/local/lib, and header files in /usr/local/include. Unfortunately few Linux systems have those added for the compiler to search by default, you need to explicitly tell the compiler to do it.

Telling the compiler to add a header-file path is done using the -I (upper-case i) option. For libraries the option is -L.

Like so:

g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp

The above command will allow your program to build, but it's unfortunately not enough as you most likely won't be able to run the program you just built. That's because the run-time linker and program loader doesn't know the path to the (dynamic) libraries either. You need to add another linker-specific flag -rpath telling the build-time linker to embed the path inside your finished program. The front-end program g++ doesn't know this option, so you need to use -Wl,-rpath:

g++ test.cpp -I/usr/local/include -L/usr/local/lib -lgmpxx -lgmp -Wl,-rpath=/usr/local/lib

The options can be found in the GCC documentation (for the -I and -L and -Wl options), and the documentation for ld (the compile-time linker) for the -rpath option.


If you install a lot of custom-build libraries, you might add the path /usr/local/lib to the file /etc/ld.so.conf and then run the ldconfig command (as root). Then you don't need the -rpath option.


Now with all of that said, almost all libraries you would usually use for development will be available in your distributions standard repository. If you use them the libraries will be installed with paths that means you don't have to add flags.

So I recommend you install your distributions development packages for the libraries instead.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621