Linking Libraries Locally with CMake: A Follow-Up

In my previous post about building C/C++ projects with CMake, I shared a simple CMakeLists.txt setup for compiling a basic project. Today, I’ll dive into a specific need that many developers encounter: linking libraries directly from project folders rather than relying on a system-wide installation. Why Link Libraries Locally?

Sometimes, you might want to keep your dependencies within your project directory, whether for portability, version control, or simply to avoid installing extra packages globally. Here’s how I set this up with CMake. Updated CMakeLists.txt Example for Local Libraries

This is the setup I use to link Raylib (a popular C library for game development) directly from my libs folder.

cmake_minimum_required(VERSION 3.29)
project(Game)

# Set the C++ standard version
set(CMAKE_CXX_STANDARD 20)

# Include and link directories for Raylib (in ./libs folder)
include_directories("./libs/raylib-5.0_linux_amd64/include")
link_directories("./libs/raylib-5.0_linux_amd64/lib")

add_executable(Game main.cpp)

# Link dynamically to Raylib 
# Comment this if you are going with static linking
target_link_libraries(Game PRIVATE raylib) 

# Uncomment the following line if you prefer static linking
# target_link_libraries(cppray PRIVATE raylib.a)

Key Points:

  • include_directories: Adds headers for Raylib.

  • link_directories: Adds the path to the Raylib library files.

  • target_link_libraries: Specifies whether you’re linking dynamically or statically (toggle as needed).

Revisiting the Previous Approach

In the first post, I used target_link_libraries(Game raylib) without specifying local paths, assuming a system-wide installation. While that approach is simpler, linking libraries from subdirectories can be more portable, especially for sharing projects or working across different environments.

0
Subscribe to my newsletter

Read articles from Bhanuka Mallawaarachchi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bhanuka Mallawaarachchi
Bhanuka Mallawaarachchi