英文:
Why doesn't make produce a .o file?
问题
我正在学习Makefile,但似乎无法弄清楚为什么这个规则生成没有扩展名的可执行文件,而不是.o
文件。
CC = clang
CFLAGS=-c
%.o: %.c
$(CC) $(CFLAGS) %.c
我尝试查阅GNU make文档,但也没有帮助。
我尝试使用其他自动变量,但也没有效果。
CC = clang
%.o: %.c
$(CC) $< -c
这也不起作用,我尝试的每个方法都生成没有扩展名的可执行文件。
CC = clang
%.o: %.c
$(CC) %.c -o %.o
我需要这个规则像这样工作:
假设我想编译c_filename.c
:
$ make c_filename
生成的文件应该是c_filename.o
。
英文:
I'm learning Makefile and I can't seem to figure out why this rule is producing an executable with no extension instead of a .o
file.
CC = clang
CFLAGS=-c
%.o: %.c
$(CC) $(CFLAGS) %.c
I've tried going through the GNU make docs, but that didn't help either.
I tried using other Automatic Variables but that didn't work either.
CC = clang
%.o: %.c
$(CC) $< -c
This doesn't work either, everything I've tried produces an executable with no extension.
CC = clang
%.o: %.c
$(CC) %.c -o %.o
I need the rule to work like this:
Suppose I want to compile c_filename.c
:
$ make c_filename
The resulting file should be c_filename.o
答案1
得分: 1
.SUFFIXES
是一个用于关闭 make 的隐式规则的指令。如果要生成 c_filename.o
,可以执行 make c_filename.o
。当不加后缀调用 make
时,它会默认生成可执行文件,这是通过一个隐式规则处理的(除非你像上面那样禁用了它们)。通常,如果你想生成目标文件,可以添加一个 all
目标,如下所示:
all: c_filename.o
这将在你运行 make
时生成对象文件,不需要任何参数。如果你想指定生成可执行文件的规则,可以使用类似这样的规则:
%: %.o
$(CC) -o $@ $^
然后,你可以使用以下方式让 make 生成可执行文件和对象文件:
all: c_filename.o c_filename
更多信息可以参考这里。
英文:
So make has a bunch of implicit rules that are supposed to make things easier. You can turn them off by adding this line:
.SUFFIXES:
If you want it to make c_filename.o
you can call make c_filename.o
. When you call it without the suffix, it assumes you want the executable, which is handled by an implicit rule (unless you disable them as above). Generally, if what you want is the object files, you'd add an all
target like this:
all: c_filename.o
That will then make the object file whenever you run make without any arguments. If you want to specify the rule to build the executable, you might use something like this:
%: %.o
$(CC) -o $@ $^
Then you could have make create the executable and object files like this:
all: c_filename.o c_filename
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论