There are many command parameters for GCC. Here are a few M-related ones.
-M
Generate the information associated with the file. Contains all source code that the target file depends on. Here is the test log:
jack@jxes-VirtualBox:~/samba_share/tmp$ vim test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -M test.c
test.o: test.c /usr/include/stdc-predef.h /usr/include/stdio.h \
/usr/include/features.h /usr/include/i386-linux-gnu/sys/cdefs.h \
/usr/include/i386-linux-gnu/bits/wordsize.h \
/usr/include/i386-linux-gnu/gnu/stubs.h \
/usr/include/i386-linux-gnu/gnu/stubs-32.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stddef.h \
/usr/include/i386-linux-gnu/bits/types.h \
/usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stdarg.h \
/usr/include/i386-linux-gnu/bits/stdio_lim.h \
/usr/include/i386-linux-gnu/bits/sys_errlist.h
-MM
It's the same as the one above, but it ignores the dependencies caused by # include.
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MM test.c
test.o: test.c
-MMD
The same as - MM, but the output will be imported into the. d file:
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MMD test.c -o test
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 24
drwxrwxr-x 2 jack jack 4096 7month 17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7month 17 10:42 test*
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 13 7month 17 10:42 test.d //test.d is the export file
jack@jxes-VirtualBox:~/samba_share/tmp$ rm test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ rm test
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 12
drwxrwxr-x 2 jack jack 4096 7month 17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MMD test.c//Note that even if the target - o is not specified, the compiler will automatically generate a.out, which is different from the previous - M,-MM.
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 24
drwxrwxr-x 2 jack jack 4096 7month 17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7month 17 10:42 a.out*
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 15 7month 17 10:42 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ ./a.out
100
jack@jxes-VirtualBox:~/samba_share/tmp$
The content of the test.d file is similar to that of - MM, but it is exported to the file:
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$
-MD
Similar to - MMD, the content exported is different, and the content is the same as - M. Specifically, you can see log:
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MD test.c -o test
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 24
drwxrwxr-x 2 jack jack 4096 7month 17 10:48 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7month 17 10:48 test*
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 624 7month 17 10:48 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
**test: test.c /usr/include/stdc-predef.h /usr/include/stdio.h \
/usr/include/features.h /usr/include/i386-linux-gnu/sys/cdefs.h \
/usr/include/i386-linux-gnu/bits/wordsize.h \
/usr/include/i386-linux-gnu/gnu/stubs.h \
/usr/include/i386-linux-gnu/gnu/stubs-32.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stddef.h \
/usr/include/i386-linux-gnu/bits/types.h \
/usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stdarg.h \
/usr/include/i386-linux-gnu/bits/stdio_lim.h \
/usr/include/i386-linux-gnu/bits/sys_errlist.h**
jack@jxes-VirtualBox:~/samba_share/tmp$ rm test
jack@jxes-VirtualBox:~/samba_share/tmp$ rm test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MD test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 24
drwxrwxr-x 2 jack jack 4096 7month 17 10:48 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7month 17 10:48 a.out*
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 626 7month 17 10:48 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.o: test.c /usr/include/stdc-predef.h /usr/include/stdio.h \
/usr/include/features.h /usr/include/i386-linux-gnu/sys/cdefs.h \
/usr/include/i386-linux-gnu/bits/wordsize.h \
/usr/include/i386-linux-gnu/gnu/stubs.h \
/usr/include/i386-linux-gnu/gnu/stubs-32.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stddef.h \
/usr/include/i386-linux-gnu/bits/types.h \
/usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h \
/usr/lib/gcc/i686-linux-gnu/5/include/stdarg.h \
/usr/include/i386-linux-gnu/bits/stdio_lim.h \
/usr/include/i386-linux-gnu/bits/sys_errlist.h
jack@jxes-VirtualBox:~/samba_share/tmp$
-MF
man help
Refers to a file used to store the associated information of the generated file, which is the same as - M or - MM, so use it with - M or - MM, otherwise it will make an error. Look at the following log:
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MF test.c -o test
gcc: fatal error: no input files
compilation terminated.
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MF"test.d" test.c -o test
cc1: error: to generate dependencies you must specify either -M or -MM
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MM -MF"test.d" test.c -o test
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 16
drwxrwxr-x 2 jack jack 4096 7month 17 10:53 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rw-rw-r-- 1 jack jack 0 7month 17 10:53 test
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 15 7month 17 10:53 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$
Note: When using - M or - MM alone, the generated target file has no executable properties:
-rw-rw-r-- 1 jack jack 0 7month 17 10:53 test
Be careful:
When - MF is mixed with - MMD or - MD, it will cover the features of - MD or - MMD. In short, they are equivalent. Here is an introduction in man's help.
-MF file
When used with -M or -MM, specifies a file to write the dependencies to. If no -MF switch is given the
preprocessor sends the rules to the same place it would have sent preprocessed output.
When used with the driver options -MD or -MMD, -MF overrides the default dependency output file.
-MT
Specify the target file name
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc test.c -MT"test.o"
cc1: error: to generate dependencies you must specify either -M or -MM
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc test.c -MT"test.o" -MM
test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 12
drwxrwxr-x 2 jack jack 4096 7month 17 10:58 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc test.c -MT"test.d" -MT"test.o" -MM
test.d test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 12
drwxrwxr-x 2 jack jack 4096 7month 17 10:58 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc test.c -MF"test.d" -MT"test.d" -MT"test.o" -MM
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 16
drwxrwxr-x 2 jack jack 4096 7month 17 11:00 ./
drwxr-xr-x 6 jack jack 4096 7month 17 10:38 ../
-rw-rw-r-- 1 jack jack 87 7month 17 10:39 test.c
-rw-rw-r-- 1 jack jack 22 7month 17 11:00 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.d test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$
summary
M,-MM,-MMD,-MF,-MT are mainly used to generate association information and specify the output file name, so I usually use - MMD, the other few will use. Additionally, with - d, the compiler will automatically generate. d files to save the associated information, and the final generated target files will not have no executable attributes.