Skip to main content

Prerequisites

Before building Push Swap, ensure you have the following tools installed:
  • C Compiler: cc or gcc
  • Make: GNU Make build system
  • POSIX-compliant system: Linux, macOS, or Unix-like environment

Build System Overview

Push Swap uses a Makefile-based build system with standard 42 School conventions. The build process compiles all source files with strict error checking enabled.

Compiler Configuration

The project is compiled with the following flags:
CC = cc
CFLAGS = -Wall -Wextra -Werror
  • -Wall: Enable all common warning messages
  • -Wextra: Enable additional warning messages
  • -Werror: Treat all warnings as errors (ensures clean code)

Source Files

The project consists of 14 C source files:
push_swap.c       # Main sorting algorithm and entry point
main.c            # Program entry and argument parsing

Building the Project

1

Clone or navigate to the source directory

Navigate to the directory containing the Makefile and source files.
cd /path/to/push_swap
2

Run the default build target

Compile the project using the all target (default):
make
Or explicitly:
make all
This will:
  1. Compile each .c file into a .o object file
  2. Link all object files into the push_swap executable
3

Verify the build

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

Makefile Targets

The Makefile provides several targets for different build operations:

make all

Default target. Compiles all source files and creates the push_swap executable.
make all

make clean

Removes all object files (.o files) but preserves the executable.
make clean
Useful for:
  • Cleaning up intermediate build artifacts
  • Reducing disk space usage
  • Preparing for a fresh compilation

make fclean

Removes all object files AND the executable. Complete cleanup.
make fclean
This target:
  1. Calls clean to remove object files
  2. Removes the push_swap executable

make re

Performs a complete rebuild from scratch.
make re
Equivalent to:
make fclean && make all
Use this when:
  • You’ve made changes to header files
  • You want to ensure a completely fresh build
  • You’re troubleshooting build issues

Build Output

Successful Build

During compilation, you’ll see output similar to:
cc -Wall -Wextra -Werror -c push_swap.c -o push_swap.o
cc -Wall -Wextra -Werror -c ft_error_exit.c -o ft_error_exit.o
cc -Wall -Wextra -Werror -c ft_split.c -o ft_split.o
# ... (more compilation steps)
cc -Wall -Wextra -Werror push_swap.o ft_error_exit.o ft_split.o \
   init_a_to_b.o init_b_to_a.o sort_big_stacks.o stack_utils.o \
   ft_fill_stack.o main.o ft_push.o ft_reverse_rotate.o \
   ft_rotate.o ft_swap.o min_on_top.o -o push_swap
The final executable push_swap will be located in the project root directory.

Build Artifacts

After building, your directory will contain:
  • Object files: *.o (one for each source file)
  • Executable: push_swap
  • Source files: *.c and *.h (unchanged)

Troubleshooting

Compilation Errors

If you encounter compilation errors:
The project uses -Werror, which treats warnings as errors. This ensures code quality but means even minor issues will prevent compilation.
  1. Check compiler version: Ensure you have a modern C compiler
    cc --version
    
  2. Clean and rebuild: Try a fresh build
    make fclean && make
    
  3. Check for missing files: Verify all source files are present
    ls *.c *.h
    

Make Not Found

If make command is not available:
# On Ubuntu/Debian
sudo apt-get install build-essential

# On macOS
xcode-select --install

Manual Compilation

If you need to compile manually without Make:
cc -Wall -Wextra -Werror \
   push_swap.c ft_error_exit.c ft_split.c init_a_to_b.c \
   init_b_to_a.c sort_big_stacks.c stack_utils.c ft_fill_stack.c \
   main.c ft_push.c ft_reverse_rotate.c ft_rotate.c ft_swap.c \
   min_on_top.c -o push_swap

Next Steps

After successfully building the project:

Build docs developers (and LLMs) love