01 / 20

Library Linking
in C++

Understand how include paths and link flags connect your code to external libraries: the essential bridge between writing code and building programs.

C++

What Is a Library?

A library is a collection of pre-written, pre-compiled code that you can reuse in your own programs. Instead of writing everything from scratch, you link against a library to borrow its functionality.

Header Files
Declarations that tell your code what functions exist (the menu).
Library Files
Compiled code that provides the actual implementation (the kitchen).

The Two-Phase Build Process

Building a C++ program happens in two distinct phases. Understanding these phases is the key to understanding library linking.

main.cpp Source Code Compiler Needs: headers -I (include path) main.o Object File Phase 1: Compilation main.o Object File Linker Needs: .a / .so / .lib -L / -l (link flags) program Executable Phase 2: Linking

Why Does Linking Matter?

Without linking, the compiler can check your code's syntax but it has no idea where to find the actual machine code for library functions. This creates the dreaded "undefined reference" error.

Code Reuse
Use thousands of tested functions without writing them yourself.
Modularity
Break huge projects into independent, compilable units.
Efficiency
Only recompile the parts that changed, then re-link.

Include Paths: -I Flag

The include path tells the compiler where to search for header files when your code uses #include. By default it only knows about system headers.

// In your source code: #include <SFML/Graphics.hpp> // The compiler needs to know where SFML/ lives! // Tell it with -I: g++ -I/usr/local/include -c main.cpp

Link Flags: -L and -l

Link flags tell the linker where to find compiled library files and which libraries to use. There are two flags that work together:

-L (Library Path)
"Search in this folder for library files." Like -I but for the linker.
-l (Library Name)
"Link against this specific library." Drops the lib prefix and file extension.
// Link against libsfml-graphics.so g++ main.o -L/usr/local/lib -lsfml-graphics // -L says WHERE to look // -l says WHAT to link (strips "lib" prefix and extension)

The Naming Convention

Library filenames follow a pattern. The -l flag maps to a filename by adding lib as a prefix:

-lsfml-graphics maps to libsfml-graphics.so lib sfml-graphics .so prefix (auto) your -l name extension (auto) .so = Linux | .dylib = macOS | .dll / .lib = Windows

Static vs Dynamic Libraries

Static (.a / .lib) Dynamic (.so / .dll)
When linked At compile time At run time
Bundled into exe? Yes: code is copied in No: loaded from disk
File size Larger executable Smaller executable
Portability Self-contained Needs .so/.dll present
Updates Must recompile Swap the .so/.dll

Step-by-Step: Linking a Library

1
Install the library. Download or use a package manager (apt, brew, vcpkg).
2
Find the headers. Locate the folder containing .hpp / .h files (e.g., /usr/local/include).
3
Find the library files. Locate the .so / .a / .lib files (e.g., /usr/local/lib).
4
Add -I for headers. Tell the compiler where to find them: -I/usr/local/include
5
Add -L and -l for linking. Tell the linker the path and name: -L/usr/local/lib -lsfml-graphics

Example 1: Simple Math Library

Link against the standard math library libm to use functions like sqrt() and sin().

#include <cmath> #include <iostream> int main() { std::cout << sqrt(144) << std::endl; return 0; }
# Compile and link in one step: g++ main.cpp -lm -o program # -lm links against libm.so (the math library) # No -I or -L needed: cmath is a system header # and libm is in a default search path.

Example 2: SFML Graphics

SFML is installed in a non-standard location. You need both include and link flags.

#include <SFML/Graphics.hpp> int main() { sf::RenderWindow window( sf::VideoMode(800, 600), "Hello"); return 0; }
# Compile (Phase 1): g++ -I/opt/sfml/include -c main.cpp # Link (Phase 2): g++ main.o -L/opt/sfml/lib -lsfml-graphics -lsfml-window -lsfml-system -o game

Example 3: All-in-One Command

In practice, you can compile and link in a single g++ call. The compiler runs both phases for you.

# Compile + link in one command: g++ main.cpp \ -I/opt/sfml/include \ -L/opt/sfml/lib \ -lsfml-graphics \ -lsfml-window \ -lsfml-system \ -o game

The Full Picture

main.cpp SFML/Graphics.hpp Header (declarations) -I Compiler g++ -c main.o libsfml-graphics.so Library (compiled code) -L -l Linker ld / g++ program -I: Include path (compiler) -L / -l: Link path + name (linker)

Common Mistakes

✕ Forgetting -I
Error: "file not found" on #include. The compiler cannot locate header files without the include path.
✕ Forgetting -l
Error: "undefined reference". Code compiles fine, but the linker cannot find the function implementations.
✕ Wrong -l Name
Writing -llibsfml-graphics instead of -lsfml-graphics. Don't include the "lib" prefix or file extension in the -l flag.

Summary of Flags

Flag Phase Purpose Example
-I Compilation Add header search path -I/opt/sfml/include
-L Linking Add library search path -L/opt/sfml/lib
-l Linking Name the library to link -lsfml-graphics
-c Compilation Compile only (no linking) -c main.cpp
-o Output Name the output file -o myprogram

Question 1 of 5

What does the -I flag tell the compiler to do?

Question 2 of 5

A library file is named libboost_filesystem.so. What is the correct -l flag to link it?

Question 3 of 5

You see the error: "undefined reference to `sf::RenderWindow::RenderWindow(...)`". Which flag is most likely missing?

Question 4 of 5

What is the difference between -L and -l?

Question 5 of 5

Which of the following commands correctly compiles main.cpp with a library installed at /home/user/mylib?