Skip to main content

Prerequisites

Before building Cub3D, ensure you have the following dependencies installed on your Linux system:

C Compiler

GCC or compatible C compiler (specified as cc in Makefile)

X11 Libraries

X11 and Xext libraries for window management

Math Library

Standard math library (libm)

Make

GNU Make for build automation

Required Libraries

Cub3D depends on two main libraries that are included in the source:
  • libft - Custom C utility library (located in libft/)
  • MiniLibX - Minimal X11 graphics library (located in minilibx-linux/)
Both libraries are automatically built as part of the compilation process.

Build Process

Follow these steps to build Cub3D from source:
1

Clone or download the repository

Ensure you have the complete Cub3D source code with all subdirectories:
ls
# Should show: libft/ minilibx-linux/ maps/ *.c *.h Makefile
2

Build the project

Run the default Make target to compile everything:
make
This command will:
  1. Build the MiniLibX library
  2. Build the libft library
  3. Compile all Cub3D source files
  4. Link everything into the cub3d executable
3

Verify the build

Check that the executable was created successfully:
ls -lh cub3d
You should see the cub3d binary in your current directory.

Compilation Details

Compiler Flags

Cub3D is compiled with the following flags as defined in the Makefile:
CFLAGS = -Wall -Wextra -Werror -O3 -g -g3
The -Wall -Wextra -Werror flags ensure strict compliance with coding standards by treating all warnings as errors.
  • -Wall -Wextra: Enable comprehensive warning messages
  • -Werror: Treat warnings as compilation errors
  • -O3: Enable maximum optimization for performance
  • -g -g3: Include detailed debugging symbols

Linking

The final linking step connects all object files and libraries:
$(CC) $(CFLAGS) -o $(NAME) $(OBJS) -L$(MINILIBX_DIR) -lmlx \
  -L$(LIBFT_DIR) -lft -lm -lX11 -lXext
Linked libraries:
  • -lmlx: MiniLibX graphics library
  • -lft: Custom libft utility library
  • -lm: Math library (for trigonometric functions)
  • -lX11 -lXext: X Window System libraries

Source Files

The following source files are compiled:
main.c utils.c utils2.c utils3.c utils4.c utils5.c 
utils6.c utils7.c freemem.c parselineutils.c 
asignatributes.c raycasting.c drawing.c

Makefile Targets

The Makefile provides several targets for different build operations:

make or make all

Builds the complete project including all dependencies.
make all
This is the default target - running make without arguments executes make all.

make clean

Removes object files (.o) from the Cub3D directory and cleans the libft directory:
make clean
This target:
  • Removes all *.o files from the current directory
  • Runs make clean in the libft subdirectory
  • Leaves executables and libraries intact

make fclean

Performs a full clean, removing all build artifacts:
make fclean
This target:
  • Executes make clean first
  • Removes the cub3d executable
  • Runs make fclean in libft (removes libft.a)
After running make fclean, you’ll need to rebuild everything with make or make all.

make re

Rebuild everything from scratch:
make re
This target:
  1. Executes make fclean to remove all build artifacts
  2. Executes make all to rebuild from scratch
Use make re when you want to ensure a completely clean build, especially after making significant changes to source files or headers.

Troubleshooting

MiniLibX Build Failures

If the MiniLibX library fails to build, ensure you have the X11 development packages installed:
# Debian/Ubuntu
sudo apt-get install libx11-dev libxext-dev

# Fedora/RHEL
sudo dnf install libX11-devel libXext-devel

Missing Math Functions

If you encounter “undefined reference” errors for math functions like cos, sin, or sqrt, verify that the math library is being linked with the -lm flag.

Compilation Warnings

Due to the -Werror flag, any compiler warning will halt the build. Review the error message carefully and fix the indicated issue in the source code.

Build Output Example

A successful build should produce output similar to:
$ make
make -C minilibx-linux/
[MiniLibX compilation messages...]
make -C libft/
[libft compilation messages...]
cc -Wall -Wextra -Werror -O3 -g -g3 -c main.c
cc -Wall -Wextra -Werror -O3 -g -g3 -c utils.c
[... more compilation messages ...]
cc -Wall -Wextra -Werror -O3 -g -g3 -o cub3d main.o utils.o ... \
  -Lminilibx-linux/ -lmlx -Llibft/ -lft -lm -lX11 -lXext

Next Steps

After successfully building Cub3D, you’re ready to run it:

Running Cub3D

Learn how to execute Cub3D and load map files