makedepend Command in Linux



When working on projects with multiple C or C++ source files, managing file dependencies can be tricky. The makedepend command helps us manage these dependencies automatically. It checks which header files each source file needs, and then updates the Makefile with this information. This allows make to recompile only the files that have changed, making the build process faster and easier.

Table of Contents

Here is a comprehensive guide to the options available with the makedepend command −

What is makedepend Command in Linux?

makedepend is a command-line utility in Linux that analyzes C and C++ source files and finds dependencies, especially headers (.h files). It appends these dependencies to a Makefile, which is then used by Make to decide which parts of the project need recompiling when changes are detected.

Syntax of makedepend Command

You can use the makedepend command in Linux with the following syntax −

makedepend [options] file1.c file2.c ...

Here, file1.c, file2.c, etc. represent source files for which you want to find dependencies.

makedepend Command Options

The commonly used options for makedepend command are listed below −

Option Description
-I <directory> It adds a directory to the list of paths searched for header files.
-Y It prevents makedepend from searching standard system directories.
-D <name> It defines a macro name.
-o <suffix> It specifies an object file suffix (defaults to .o).
-f <filename> It specifies a different file instead of Makefile for appending dependencies.

Installation of makedepend Command

makedepend is not pre-installed on most Linux distributions. It belongs to the xutils-dev package. Therefore, to use this command in Linux, first, you need to install it with the system package manager. For instance Dabian/Ubuntu users can install it using the following command −

sudo apt install xutils-dev
makedepend Command in Linux1

To install it on Fedora, you must use the dnf package manager as follows −

sudo dnf install xorg-x11-util-macros

Similarly, we can use the yum package manager to install it on CentOS or RHEL −

sudo yum install xorg-x11-util-macros

Finally, you can confirm the makedepend installation on your system by accessing its manual page −

man makedepend
makedepend Command in Linux2

Examples of makedepend Command in Linux

Let's go through a few practical examples to illustrate how makedepend command works in Linux −

Basic Usage with Default Options

Suppose we have the following files for a simple C project: main.c that contains the main function of the program, utils.c contains utility functions used in main.c, and utils.h is a header file that declares the functions in utils.c. Now create a basic Makefile that compiles main.c and utils.c −

CC = gcc
CFLAGS = -Wall
TARGET = my_program

all: $(TARGET)

$(TARGET): main.o utils.o
	$(CC) -o $(TARGET) main.o utils.o

clean:
	rm -f $(TARGET) *.o

This Makefile has −

  • all target to build the program by compiling main.c and utils.c.
  • clean target to remove the executable and object files.

Let's run makedepend to automatically find and add dependencies to the Makefile −

makedepend main.c utils.c

This command scans the files main.c and utils.c, finds their dependencies (e.g., utils.h), and appends the necessary dependency lines to the Makefile. Finally, run the make command to build the project −

make

Specifying Include Paths

To specify a directory path, use the makedepend command with the -I option −

makedepend -I./include main.c utils.c

This command will search the ./include directory for any headers required by main.c and utils.c.

Changing the Output File

You can use the -f option to specify the output dependencies to a custom file other than Makefile −

makedepend -f dependencies.mk main.c utils.c

The above command tells makedepend to write the dependencies to dependencies.mk instead of Makefile.

Using Object File Suffix

If your project uses a different ending for object files (like .obj), you can set this with the -o option −

makedepend -o .obj main.c utils.c

This time, the makedepend command will generate dependency rules that expect object files with an .obj suffix.

Conclusion

The makedepend command is an essential tool for managing dependencies in C and C++ projects. It is especially helpful when multiple source and header files are involved. Makedepend automatically identifies dependencies and updates the Makefile. This ensures that make recompiles only the necessary files, making the build process more efficient.

In this tutorial, we explained how to install and use makedepend in Linux. More specifically, we explained the command syntax, options, and practical examples to illustrate its usage.

Advertisements