Overview
Makedepend is a command‑line utility for Unix‑like operating systems that analyzes C source files and produces Makefile dependency lines. The generated dependencies specify which header files each source file includes, enabling make to rebuild only the files affected by changes in those headers.
Historical Context
The program originated in the early 1990s as part of the X Window System distribution (X11R5). It was later bundled with various BSD and Linux development toolsets. Its primary purpose was to automate the maintenance of header‑file dependencies in large C projects, a task that was previously performed manually.
Functionality
-
Input: One or more C source files (or files with other extensions when invoked with appropriate options).
-
Processing: The utility parses
#includedirectives, follows the include search path (including-Idirectories specified on the command line), and resolves nested includes. -
Output: Lines suitable for inclusion in a Makefile, typically of the form:
file.o: file.c header1.h header2.h subdir/header3.hThese lines can be appended to a Makefile directly or incorporated via an
includedirective.
Typical Usage
makedepend -I/usr/include -I../include *.c
This command scans all C files in the current directory, searching for headers in /usr/include and ../include, and writes the resulting dependency information to standard output (or to the file specified with -f).
Common options include:
-f <file>– Write dependencies to the specified file.-Y– Suppress generation of dependencies for system headers (those found in standard include directories).-D<macro>– Define a macro for the preprocessor, mirroring the behavior ofgcc -D.
Relation to Other Tools
Makedepend predates and has largely been superseded by compiler‑based dependency generation options such as gcc -M and clang -M. These modern alternatives integrate directly with the compiler’s preprocessor, providing more accurate results for complex macro usage. Nonetheless, makedepend remains available in many legacy build environments and is included in packages like xorg-x11-util-makedepend on various Linux distributions.
Availability
- Source code is publicly accessible in the X.Org repository and mirrored in numerous open‑source distribution archives.
- Binary packages are provided by most BSD, Linux, and macOS package managers (e.g.,
apt,yum,pkg,brew).
Limitations
- The parser does not evaluate conditional compilation directives (
#if,#ifdef) beyond simple macro definitions supplied via-D. - It may miss dependencies introduced by compiler‑specific extensions or generated headers not present at analysis time.
Example Integration in a Makefile
# Generate dependencies
depend:
makedepend -Iinclude -f .depend *.c
# Include the generated dependencies
-include .depend
Running make depend updates the .depend file, after which make will automatically rebuild objects affected by header changes.
See Also
gcc/clangcompiler driver options for dependency generation (-M,-MM,-MF).makeutility and its handling of prerequisites.cpp(C preprocessor) for underlying include resolution.
References
- X.Org Project, makedepend source repository.
- GNU Make manual, section on automatic dependency generation.
- POSIX specifications for makefile syntax and prerequisites.