Working with CMake - raysan5/raylib GitHub Wiki

Requiring raylib to be installed

If you installed raylib with a package manager, or used make install, a simple CMakeLists.txt could look like this:

cmake_minimum_required(VERSION 3.15)
project(my_project)

find_package(raylib 3.0 REQUIRED) # Requires at least version 3.0

set(CMAKE_C_STANDARD 11) # Requires C11 standard

add_executable(${PROJECT_NAME} main.c)

target_link_libraries(${PROJECT_NAME} raylib)

# Checks if OSX and links appropriate frameworks (only required on MacOS)
if (APPLE)
    target_link_libraries(${PROJECT_NAME} "-framework IOKit")
    target_link_libraries(${PROJECT_NAME} "-framework Cocoa")
    target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
endif()

To generate build files in separate directory and build the project, use these commands:

cmake -B build && cmake --build build

Loading raylib inside cmake

If you want someone who builds your project to be able to download and build raylib with it, the CMakeLists.txt at projects/CMake will help you.

Use from within an IDE

CMake supports a range of generators, which can be used to generate project files for IDEs/Build-Tools such as Visual Studio, Ninja or Sublime Text 2. e.g. for Xcode you can run cmake -G 'Xcode' .. to have it generate project files for import into Xcode.