C++ linking to libraries with makefile (newbe)

Sebastian Dusza picture Sebastian Dusza · Oct 25, 2010 · Viewed 26.4k times · Source

I'm trying to understand how to use non standard libraries in my C++ projects. I have a few questions.

Lets say I want to use POCO library. So I downloaded it and build it using make (static build). Now I have bunch of .o files and .h files. There is a Path.h file and a Path.o file in different directories.

Now I want to use this module in my code. So i include the file using #include "Poco/Path.h". Do I have to modify makefile and add Path.o to my target ?

What happens when I use standard library ? Are those available only in header files ? I know that template code cannot be precompiled. What about the rest ?

Answer

Bart van Ingen Schenau picture Bart van Ingen Schenau · Oct 25, 2010

Besides the .h and .o files, you will probably also have one or more libXXX.a and/or libXXX.so files. These are the actual library files that your application should link against.

To use the library, you include the relevant headers in your source file, and you change your makefile to tell the linker that it should also link your application to the XXX library. The typical linker-command for that is -lXXX and the linker will look for both libXXX.a and libXXX.so and use whichever seems most appropriate.

The standard library is not really different from external libraries, except that you don't have to specify it explicitly to the linker.