Fading Coder

An Old Coder’s Final Dance

Home > Tech > Content

GCC dependency generation: -M, -MM, -MD, -MMD, -MF, -MT

Tech 2

These options drive how GCC emits Makefile-style dependency rules. They’re useful for incremental builds that only recompile what chenged.

Example source

Create a tiny program to reference in the examples:

// deps.c
#include <stdio.h>

int main(void) {
    int value = 21 * 2;
    printf("%d\n", value);
    return 0;
}

-M

Emit dependency information including system headers to stdout. No object files or executables are produced.

$ gcc -M deps.c
deps.o: deps.c /usr/include/stdio.h \
  /usr/include/features.h \
  /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
  /usr/lib/gcc/x86_64-linux-gnu/12/include/stddef.h \
  ...

-MM

Like -M, but omit system headers from the dependency list.

$ gcc -MM deps.c
deps.o: deps.c

-MMD

Compile as usual and write dependencies (excluding system headers) to a .d file named after the output. If you don’t specify -o and link, GCC still builds the default executable (a.out) and writes the .d file.

$ gcc -MMD deps.c -o app
$ ls
app  deps.c  deps.d

$ cat deps.d
deps.o: deps.c

$ rm -f app deps.d
$ gcc -MMD deps.c
$ ls
a.out  deps.c  deps.d
$ ./a.out
42

-MD

Same as -MMD but includes system headers in the dependency list. The target in the .d file matches the final output name: if you link to an executable with -o, that name is used; otherwise the object name is used.

$ gcc -MD deps.c -o app
$ cat deps.d
app: deps.c /usr/include/stdio.h \
  /usr/include/features.h \
  ...

$ rm -f app deps.d
$ gcc -MD deps.c
$ ls
a.out  deps.c  deps.d
$ cat deps.d
deps.o: deps.c /usr/include/stdio.h \
  /usr/include/features.h \
  ...

-MF

Select the file to receive dependency rules. With -M or -MM it redirects stdout to the file; with -MD/-MMD it overrides the default .d path.

  • Using -MF without -M/-MM/-MD/-MMD is an error:
$ gcc -MF deps.mk deps.c -o app
cc1: error: to generate dependencies you must specify either -M or -MM
  • Proper use with -MM:
$ gcc -MM -MF deps.mk deps.c
$ cat deps.mk
deps.o: deps.c
  • With -MMD (and compiling only):
$ gcc -MMD -MF deps.mk -c deps.c -o deps.o
$ ls
deps.c  deps.mk  deps.o

Note: -M or -MM suppress compilation/linking. If you also pass -o, GCC may create an empty file at that path because no code is produced.

$ gcc -MM -MF deps.mk deps.c -o placeholder
$ ls -l placeholder
-rw-r--r-- 1 user user 0 ... placeholder

-MT

Ovreride the target name(s) written in the dependency rule. You can supply -MT multiple times too emit multiple targets for the same prerequisite list.

  • Requires -M/-MM or -MD/-MMD:
$ gcc deps.c -MT out.o
cc1: error: to generate dependencies you must specify either -M or -MM
  • Basic use:
$ gcc -MM -MT mytarget deps.c
mytarget: deps.c
  • Multiple targets and writing to a file:
$ gcc -MM -MF deps.mk -MT deps.d -MT deps.o deps.c
$ cat deps.mk
deps.d deps.o: deps.c
Tags: GCCc

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.