Raylib with CLion and vcpkg
Raylib with CLion and vcpkg
Raylib is one of the cleanest ways to start learning game programming in C or C++. It gives you a simple API for windows, drawing, keyboard input, textures, audio, and basic 2D or 3D experiments without forcing you into a heavy game engine.
In this guide, you will set up Raylib in CLion using vcpkg and CMake. By the end, you will have a working C++ project that opens a window and lets you move a rectangle with the arrow keys.
The goal is simple: get the development environment working correctly so you can focus on learning game logic instead of wasting hours fighting build errors.
Why Use Raylib?
Raylib is designed to be beginner-friendly, but it is not a toy. It is lightweight, direct, and great for learning how game loops, input handling, drawing, and basic real-time systems work.
Compared with large engines like Unreal Engine or Unity, Raylib gives you fewer built-in systems. That is actually the point. You are closer to the code, closer to the loop, and closer to the fundamentals.
Raylib is especially useful for:
- learning C and C++ game programming
- building small 2D games
- creating prototypes quickly
- testing math, physics, input, or rendering ideas
- understanding how a game loop works
- building simple tools or visual experiments
If you want to understand game development below the level of full engines, Raylib is a strong place to start.
Why Use vcpkg?
Installing C++ libraries manually can become annoying fast. You download a library, build it, point CMake to it, fix missing dependencies, fight platform differences, and eventually forget what you did.
vcpkg solves a lot of that pain by acting as a package manager for C and C++ libraries. Instead of manually installing Raylib and its dependencies, you can install it with one command.
For this tutorial, vcpkg gives us:
- simple Raylib installation
- CMake integration
- cross-platform dependency management
- cleaner project setup inside CLion
Brutal truth: C++ dependency management is still worse than Rust Cargo or Node npm. But vcpkg makes it much less painful.
What You Will Build
This tutorial builds a very small Raylib project with:
- a window sized 800 by 600
- a rectangle drawn on screen
- keyboard input using the arrow keys
- a basic game loop
- simple drawing with Raylib functions
It is intentionally simple. The point is not to build a complete game yet. The point is to prove that Raylib, CLion, vcpkg, and CMake are working together correctly.
Prerequisites
Before starting, make sure you have the following installed:
- CLion or another CMake-compatible C++ IDE
- CMake, usually bundled with CLion
- Git, needed to clone vcpkg
- A C++ compiler, such as MSVC on Windows, Clang on macOS, or GCC/Clang on Linux
- vcpkg, which we will install in this guide
If your compiler setup is broken, fix that first. Raylib will not save you from a bad C++ toolchain.
Step 1: Install vcpkg
Open a terminal and clone the vcpkg repository:
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
Now bootstrap vcpkg.
On Windows, run:
.\bootstrap-vcpkg.bat
On macOS or Linux, run:
./bootstrap-vcpkg.sh
This builds the vcpkg executable so you can start installing packages.
Step 2: Integrate vcpkg with Your Development Workflow
After bootstrapping, run:
./vcpkg integrate install
On Windows, depending on your shell, you may use:
.\vcpkg integrate install
This command helps vcpkg integrate with supported build environments.
For CLion and CMake, the most important part is usually the vcpkg toolchain file. You may need to point CLion or CMake to it manually depending on your setup.
The toolchain file is usually located at:
/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
On Windows, it may look like:
C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake
Step 3: Install Raylib with vcpkg
From inside the vcpkg folder, run:
vcpkg install raylib
Or if you are using the executable directly from the current folder:
./vcpkg install raylib
On Windows:
.\vcpkg install raylib
vcpkg will download, configure, build, and install Raylib and its dependencies.
This may take a few minutes the first time. Do not panic if it looks busy. C++ libraries often need to compile locally.
Step 4: Create a New C++ Project in CLion
Open CLion and create a new C++ project.
Use a normal CMake-based project. Name it something like:
RaylibExample
CLion will create a basic project with a CMakeLists.txt file and a main.cpp file.
If CLion does not detect your compiler correctly, fix your toolchain settings before continuing.
Step 5: Configure CLion to Use the vcpkg Toolchain
This is the step many beginners miss.
Installing Raylib through vcpkg is not enough. Your CMake project must also know where vcpkg is.
In CLion, open your CMake profile settings:
Settings or Preferences > Build, Execution, Deployment > CMake
In the CMake options field, add:
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
Replace /path/to/vcpkg with the actual path on your machine.
Example on Windows:
-DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake
Example on macOS or Linux:
-DCMAKE_TOOLCHAIN_FILE=/Users/yourname/dev/vcpkg/scripts/buildsystems/vcpkg.cmake
Apply the settings and reload the CMake project.
Step 6: Configure CMakeLists.txt
Replace or update your CMakeLists.txt with this setup:
cmake_minimum_required(VERSION 3.10)
project(RaylibExample)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(raylib CONFIG REQUIRED)
add_executable(RaylibExample main.cpp)
target_link_libraries(RaylibExample PRIVATE raylib::raylib)
This CMake file does a few important things:
- sets the minimum CMake version
- creates the project
- uses C++17
- finds Raylib through vcpkg
- builds your executable from main.cpp
- links Raylib to your project
If find_package(raylib CONFIG REQUIRED) fails, your vcpkg toolchain path is probably wrong or Raylib was not installed correctly.
Step 7: Write the First Raylib Program
Now open main.cpp and add this code:
#include <raylib.h>
int main()
{
const int screenWidth = 800;
const int screenHeight = 600;
InitWindow(screenWidth, screenHeight, "Raylib Example");
Rectangle player = { 350.0f, 250.0f, 100.0f, 100.0f };
float speed = 5.0f;
SetTargetFPS(60);
while (!WindowShouldClose())
{
if (IsKeyDown(KEY_RIGHT))
{
player.x += speed;
}
if (IsKeyDown(KEY_LEFT))
{
player.x -= speed;
}
if (IsKeyDown(KEY_DOWN))
{
player.y += speed;
}
if (IsKeyDown(KEY_UP))
{
player.y -= speed;
}
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangleRec(player, BLUE);
DrawText("Move the rectangle with arrow keys", 10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
This is a simple Raylib game loop. It opens a window, reads keyboard input, updates the rectangle position, clears the screen, draws the rectangle, and repeats until the window closes.
Understanding the Code
Let’s break down what the program is doing.
Creating the Window
InitWindow(screenWidth, screenHeight, "Raylib Example");
This creates a window with the size 800 by 600 and sets the title.
Creating the Rectangle
Rectangle player = { 350.0f, 250.0f, 100.0f, 100.0f };
A Raylib Rectangle contains x position, y position, width, and height.
Setting the Frame Rate
SetTargetFPS(60);
This tells Raylib to target 60 frames per second. For simple tutorials, this keeps movement speed more predictable.
The Game Loop
while (!WindowShouldClose())
{
// update
// draw
}
This loop runs until the user closes the window or presses the platform-specific close shortcut.
Almost every real-time game or simulation has this basic shape:
- process input
- update game state
- draw the frame
- repeat
Keyboard Input
if (IsKeyDown(KEY_RIGHT))
{
player.x += speed;
}
Raylib checks whether a key is currently held down. If the right arrow key is down, the rectangle moves right.
Drawing
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangleRec(player, BLUE);
DrawText("Move the rectangle with arrow keys", 10, 10, 20, DARKGRAY);
EndDrawing();
Drawing in Raylib happens between BeginDrawing and EndDrawing. You clear the background first, then draw your objects and text.
Step 8: Build and Run the Project
In CLion, reload CMake if needed, then build the project.
Common shortcuts:
- Build: Ctrl + F9
- Run: Shift + F10
If everything is configured correctly, a window should open. You should see a blue rectangle and be able to move it using the arrow keys.
Common Setup Problems
CLion Cannot Find Raylib
If CMake says it cannot find Raylib, check these:
- Did you install Raylib with vcpkg?
- Did you pass the vcpkg toolchain file to CMake?
- Did you reload the CMake project after changing settings?
- Are you using the correct vcpkg path?
find_package Fails
If this line fails:
find_package(raylib CONFIG REQUIRED)
the problem is usually that CMake is not using vcpkg’s toolchain file.
Build Works but the Program Does Not Run
Check your CLion run configuration. Make sure it is running the correct executable target.
Compiler Is Not Configured
CLion still needs a working compiler. On Windows, that usually means Visual Studio Build Tools or MSVC. On macOS, install Xcode Command Line Tools. On Linux, install GCC or Clang.
Why This Setup Is a Good C++ Workflow
This setup gives you a clean foundation:
- CLion handles editing, debugging, and project navigation
- CMake handles building
- vcpkg handles external libraries
- Raylib handles windowing, input, and drawing
That is a solid workflow for learning native game programming.
It is not as simple as clicking “new project” in a full engine, but that is part of the value. You learn how a C++ game project is actually wired together.
What to Build Next
After the rectangle movement works, you can expand the project with:
- delta time based movement
- collision with screen borders
- sprite textures
- player animation
- basic enemies
- sound effects
- menus and game states
- a small 2D arcade game
The next smart step is replacing fixed movement speed with delta time. Fixed speed per frame is fine for a beginner test, but real games should account for frame time.
Conclusion
In this tutorial, you installed Raylib with vcpkg, configured a CLion CMake project, linked Raylib correctly, and built a simple program that opens a window and moves a rectangle with keyboard input.
This is a small project, but it proves the complete workflow works. From here, you can start building real Raylib experiments without fighting the setup every time.
Raylib is a great way to learn the fundamentals of game programming in C++ because it keeps the API simple and lets you focus on the loop, input, drawing, and gameplay logic.
References
Frequently Asked Questions
Is Raylib good for beginners?
Yes. Raylib is one of the best C and C++ libraries for beginners because it has a simple API and avoids unnecessary engine complexity.
Can I use Raylib with CLion?
Yes. CLion works well with Raylib when the project is configured with CMake and the Raylib package is installed through vcpkg or another supported method.
Why use vcpkg for Raylib?
vcpkg makes installing and linking Raylib easier by handling the dependency installation and exposing the package to CMake.
Do I need CMake for Raylib?
You do not strictly need CMake, but it is a strong choice for CLion and cross-platform C++ projects.
What should I build after this tutorial?
Start with a small 2D project: player movement, collision, sprites, enemies, sound effects, and simple game states.