Basic GCC Compilation Command
The standard command to compile a Raylib program looks like this:Compilation Flags Explained
Compilation Flags Explained
gcc- The GNU C Compilermy_game.c- Your source file(s)-o my_game- Output executable name-lraylib- Link against the Raylib library-lGL- OpenGL library for graphics rendering-lm- Math library for mathematical functions-lpthread- POSIX threads library for multithreading-ldl- Dynamic linking library-lrt- Real-time extensions library-lX11- X Window System library for display
The order of library flags (
-l...) matters in GCC. Libraries should be listed after your source files and in dependency order. The command above uses the correct ordering for Raylib projects.Complete Compilation Workflow
Follow these steps to compile and run your Raylib game:Compile your program
Run the GCC command with all required library flags:If compilation succeeds, you’ll see no output and return to the shell prompt.
Compiling Multi-File Projects
For larger games split across multiple source files, you have two main approaches:Method 1: Compile All Files Together
List all.c files in a single GCC command:
Method 2: Separate Compilation and Linking
Compile each file to an object file first, then link them:Method 3: Using a Makefile
Create aMakefile to automate the build process:
Running Compiled Executables
Once compilation succeeds, run your game with:The
./ prefix tells the shell to execute the file in the current directory. Without it, the shell would search your PATH and likely not find your game.Running Games in Subdirectories
If your project is organized in folders:Common Compilation Errors and Fixes
Error: undefined reference to 'InitWindow'
Cause: Missing -lraylib flag or library not found.
Fix: Ensure -lraylib is included in your compilation command:
Error: undefined reference to 'glXCreateContext'
Cause: Missing OpenGL libraries.
Fix: Add -lGL and -lX11 flags:
Error: undefined reference to 'sin' or 'cos'
Cause: Missing math library.
Fix: Add the -lm flag:
Error: my_game.c: No such file or directory
Cause: File doesn’t exist or you’re in the wrong directory.
Fix: Verify the file exists and you’re in the correct directory:
Error: fatal error: raylib.h: No such file or directory
Cause: Raylib headers not found (shouldn’t happen in the container).
Fix: This indicates an issue with the Raylib installation. The container should have Raylib properly installed at /usr/local. You can verify with:
Warning: implicit declaration of function
Cause: Missing #include "raylib.h" at the top of your source file.
Fix: Add the include at the beginning of your .c file:
Compilation Optimization Flags
For better performance, add optimization flags:-g- Include debug symbols for debugging with GDB-Wall -Wextra- Enable all warnings (helps catch bugs)-O2- Level 2 optimization (good balance)-O3- Maximum optimization (best performance)-march=native- Optimize for your specific CPU-DNDEBUG- Disable assert() statements
Use debug builds during development for better error messages. Switch to release builds when distributing your game for maximum performance.