在makefile中使用gcc在终端调用make编译时却得到cc?
Question:
当我不指定CC=gcc时,Makefile这样写:
#Makefile: src:=$(wildcard *.c) test3.c obj:=$(patsubst %.c,%.o,$(src)) main:$(obj) gcc -o main $(obj) .Phony:clean clean: rm *.o *~
调用make编译,在终端得到输出结果如下:
cc -c -o main.o main.c cc -c -o test1.o test1.c cc -c -o test2.o test2.c cc -c -o test3.o test3/test3.c gcc -o main main.o test1.o test2.o test3.o
附问题:我知道在Linux下cc指向gcc。问题是为什么在源文件编译为目标文件的时候使用的是cc,而链接的时候又变成了gcc ?
Answer:
自己想不出来问题出在哪里,于是上 stack overflow 问了,得到如下回答:
You changed one rule: the one that links the program main
from the object files. And when make did that link, you can see it used gcc
.
You didn't do anything to change the built-in rules that make is using to compile the object files, so they use the default (the value of the variable CC
) which is cc
.
意思大概是:
你修改了一个规则,即:将目标文件链接成为可执行程序时指定了make的编译器的值为gcc〔gcc –o main ...〕,所以你调用make的时候,看到的是gcc。
当将源文件编译成为目标文件时,你并没有修改make的内建规则(默认编译器的值为cc),因此调用make的时候,输出的是cc。
为了方便理解,分析如下:
默认情况,make使用cc作为编译器的标识,当我链接的时候,用gcc修改了这个标识,于是输出的就是gcc。但是编译是由系统在展开源文件的时候完成的,并不直接调用gcc,而是使用cc(虽然在linux上cc指向gcc),于是编译的过程输出的就是cc。