GCC -M,-MM,-MMD,-MF,-MT

GCC的命令参数有很多,下面主要介绍几个M相关的:

-M
生成文件关联的信息。包含目标文件所依赖的所有源代码,下面是测试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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-MM
和上面的那个一样,但是它将忽略由#include造成的依赖关系。

jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MM test.c
test.o: test.c
1
2
-MMD
和-MM相同,但是输出将导入到.d的文件里面:

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 7月  17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7月  17 10:42 test*
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack   13 7月  17 10:42 test.d //test.d为导出文件
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 7月  17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ gcc -MMD test.c//注意这里的定法,即使没有指明目标-o,编译器也会自动生成a.out,这与前面-M,-MM有所不同。
jack@jxes-VirtualBox:~/samba_share/tmp$ ll
total 24
drwxrwxr-x 2 jack jack 4096 7月  17 10:42 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7月  17 10:42 a.out*
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack   15 7月  17 10:42 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ ./a.out 
100
jack@jxes-VirtualBox:~/samba_share/tmp$ 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
test.d文件里的内容与-MM是类似的,只是导出到文件里而已:

jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ 
1
2
3
-MD
与-MMD类似,只是导出的内容有所不一样,内容与-M是一样的。具体可以看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 7月  17 10:48 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7月  17 10:48 test*
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack  624 7月  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 7月  17 10:48 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rwxrwxr-x 1 jack jack 7344 7月  17 10:48 a.out*
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack  626 7月  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$ 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-MF
man帮助里 
指一个文件用于存放生成文件的关联信息,这些信息与-M或-MM是一样的,所以要与-M或-MM一些使用,否则会报错,看下面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 7月  17 10:53 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rw-rw-r-- 1 jack jack    0 7月  17 10:53 test
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack   15 7月  17 10:53 test.d
jack@jxes-VirtualBox:~/samba_share/tmp$ cat test.d
test.o: test.c
jack@jxes-VirtualBox:~/samba_share/tmp$ 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
注意:单独使用-M或-MM时,生成的目标文件没有可执行属性:

-rw-rw-r-- 1 jack jack    0 7月  17 10:53 test
1
注意: 
-MF与-MMD或-MD混用,会覆盖-MD或-MMD的特性,简单说,它们功能是等效的,下面是man帮助里的介绍:

       -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.
1
2
3
4
5
-MT
指定目标文件名

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 7月  17 10:58 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rw-rw-r-- 1 jack jack   87 7月  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 7月  17 10:58 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rw-rw-r-- 1 jack jack   87 7月  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 7月  17 11:00 ./
drwxr-xr-x 6 jack jack 4096 7月  17 10:38 ../
-rw-rw-r-- 1 jack jack   87 7月  17 10:39 test.c
-rw-rw-r-- 1 jack jack   22 7月  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$ 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
总结
-M,-MM,-MMD,-MF,-MT这几个用法,主要是用来生成关联信息,以及指定输出文件名,所以本人一般习惯用-MMD这一个,其他几个很少会用。另外加上-D,编译器会自动生成.d的的文件保存关联信息,同时最终生成的目标文件也不会没有可执行属性。
--------------------- 
原文:https://blog.csdn.net/linuxandroidwince/article/details/75221300 
 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值