This the makefile that I use to build my C/C++ code. I compiled this over a few years, with some help from LLMs.
- It assumes C is used.
- Defines
src,include,bin,obj,libdirectories. - Finds all
.cfiles insrcdirectory and its subdirectories. - Generates object file paths by replacing
srcwithobjand.cwith.o. - Finds all subdirectories in
includedirectory and adds-Iflags. - Automatically detects and links static libraries from
libdirectory. - Generates dependency files for automatic rebuilds.
- Compiles
.cfiles into.ofiles, preserving directory structure. - Links object and library files into executable files.
- Supports debug and release build modes.
- Runs the executable file.
- Put your code in
srcdirectory. - Put your header files in
includedirectory. - Put your library files in
libdirectory. - Run
make runto compile and run the executable file. - Run
make cleanto clean up the object files and executable files. - Run
make BUILD=releaseto build optimized version. - Run
make distcleanto remove all generated files and directories.
Refer to make help to see all the commands.