Linking External Libraries in C/C++

Hey guys I'm a noob with C/C++ and as I've been learning the language I struggle with the process of compiling C/C++ code that is dependent on external libraries. I've been using the language for a while but only the extend of the standard library. The process is much different from in C than it is in languages like Python where there is a package manager for you to install external modules.
How could I go about compiling code that uses external libraries with gcc and g++?
Note I am on a Mac and I could use Xcode but I wanted to learn the proper way of completing this task. Thank you for your help!
Start with
 
int main(){}

Then type
g++ -v foo.cpp

What you'll see (with lots of other stuff omitted) will be something along the lines of

<<snipped>>
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
<<snipped>>
-L/usr/lib/gcc/x86_64-linux-gnu/5
-L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu
-L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib
-L/lib/x86_64-linux-gnu
-L/lib/../lib
-L/usr/lib/x86_64-linux-gnu
-L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/5/../../..
-lstdc++
-lm
-lgcc_s
-lgcc
-lc
-lgcc_s
-lgcc

The -L paths are where the compiler looks for libraries
The -lfoo are the shortened library filenames (would actually be called libfoo.a or libfoo.so)

.a libraries are statically linked libraries and .so are dynamically linked libraries.

So when you say #include <iostream>, the directories listed under "#include <...> search starts here" are where the compiler looks.

When you install (or compile for yourself) a 3rd party library, you need to figure out 4 things from the documentation.
- the name(s) of header files you can write in #include statements
- the location(s) of those header files - usually /some/path/include
- the location(s) of the libraries - usually /some/path/lib
- the name(s) of the libraries - usually the library name written as libname.a and/or libname.so

Knowing these things, your g++ command line becomes
g++ -I/some/path/include program.cpp -L/some/path/lib -lname
It might be worth adding that the answer is highly dependent on your OS.


Unix / Linux 
On *nixen, just use the system package manager to install external libraries and they will work just fine. Often enough a google search with the name of your package manager and the library you wish to install will suffice.

For example, if you were trying to install NCurses on Ubuntu, you could google "ubuntu apt-get ncurses devel" and you will usually find an answer in one of the top results. In this case, it would be:

    sudo apt-get install libncurses5-dev libncursesw5-dev

After that completes you can compile and link an application that uses it like any other:

    g++ my_app.cpp -lncurses


Windows
Windows is an entirely different beast. If you are using MSYS2 or the like, open your MSYS shell and follow the *nixen instructions above to frob pacman and install the desired library/libraries. (The OS you can google around for MSYS2 is "Arch Linux".)

MSVC does not automatically know where all your libraries are and make them available to your project. (There are good reasons for this, too.)

Find and download your library and unzip it somewhere you don’t mind existing forever. I find it useful to keep a top-level directory named, tellingly, “C:\MSVC” where I put all the external libraries I use. So if you download Boost Libraries, unzip it so that there is a “C:\MSVC\boost_1_72_0” directory.

If it needs compilation you are at the mercy of the library’s CONFIG/INSTALL/README files and whatever dependencies you need to build it (these days it is often Python or CMake). (Boost has gotten easier to build on Windows, BTW. It still takes forever, though.)

Once compiled, you need to manually add the libraries you wish to use to your projects.

If you are using the IDE, open your project and mess with the project properties from the Solution Explorer. (Right click your project’s name and select “Properties”.) Under the C/C++ Directories you can add your external library’s include and lib paths.

From the command line you add them by setting some environment variables. Continuing with the Boost example, before compiling you add your libboost directories:

    set INCLUDE=C:\MSVC\boost_1_72_0\include
    set LIBS=C:\MSVC\boost_1_72_0\build

After that you can compile as usual, making sure to link with the correct .lib file. For example, to link with the dynamic Boost Locale library:

    cl /EHsc ... /link boost_locale-vc142-mt-x64-1_72.lib

The final thing you must do on Windows is make sure that any DLLs your application uses are available to the final executable. Sadly, this usually means simply placing a copy of the DLL in the same directory as your EXE.


Sorry it is such a pain, but you get used to it after making it work a couple of times.

Good luck!

[edit] BTW, I am not entirely sure that the LIB directory is entirely correct in my example. It has been a while since I built Boost, and I actually copy my completed libraries into a final directory tree so I can get rid of all the source files and extra junk that I don’t need to keep around from the boost source tree. It might be something like “C:\MSVC\boost_1_72_0\build\bin64” or similar.

This is, unfortunately, where you need to just pay attention when installing libraries on Windows.
Last edited on
Topic archived. No new replies allowed.