简体   繁体   English

Makefile 中的隐式规则中的 -c 标志错误

[英]Error with -c flag in implicit rule in Makefile

I'm learning makefile and I'm trying to reduce my makefile by using implicit rules.我正在学习 makefile 并且我正在尝试通过使用隐式规则来减少我的 makefile。 So far this is what I have:到目前为止,这就是我所拥有的:

CC = /usr/bin/g++
OBJECTS_INTERACTIVE = calcular.o calc_interactive.o
    
calc_interactive: $(OBJECTS_INTERACTIVE)
    $(CC) $(OBJECTS_INTERACTIVE) -o $@              

calcular.o: calcular.c calcular.h
    $(CC) -c calcular.c

calc_interactive.o: calcular.h calc_interactive.c
    $(CC) -c calc_interactive.c

If I run it like that, no errors.如果我这样运行它,没有错误。 However, I would like to use an implicit rule like calcular.o: calcular.h , which AFAIK is performing g++ -c calcular.c under the hood, but apparently it's performing that command without the -c flag, which I think it's the key, and I don't manage to make g++ to use the -c flag when using an implicit rule.但是,我想使用像 calcular.o calcular.o: calcular.h calcular.h 这样的隐式规则,AFAIK 正在执行g++ -c calcular.c引擎盖下,但显然它正在执行该命令而没有-c标志,我认为它关键,我没有设法让 g++ 在使用隐式规则时使用-c标志。 This is what I would like to achieve:这就是我想要实现的目标:

CC = /usr/bin/g++
OBJECTS_INTERACTIVE = calcular.o calc_interactive.o

calc_interactive: $(OBJECTS_INTERACTIVE)
    $(CC) $(OBJECTS_INTERACTIVE) -o $@  

calcular.o: calcular.h

calc_interactive.o: calcular.h 
    

It yields this error:它产生此错误:

jules@desktop:$ make
/usr/bin/g++ -c calc_interactive.c
/usr/bin/g++ calcular.o calc_interactive.o -o calc_interactive
g++: error: calcular.o: No such file or directory 
makefile:11: recipe for target 'calc_interactive' failed 
make: *** [calc_interactive] Error 1 

EDIT : detailed pastebin of all files https://pastebin.com/FZy5kqzj编辑:所有文件https://pastebin.com/FZy5kqzj的详细 pastebin

In the first place, do not compile C source with a C++ compiler, unless you're sure that it is written strictly in the common subset of those two languages.首先,不要使用 C++ 编译器编译 C 源代码,除非您确定它是严格使用这两种语言的公共子集编写的。 C and C++ each have features the other lacks, and it is possible to write code that conforms to both languages but means different things to each. C 和 C++ 各有对方所缺乏的功能,可以编写符合两种语言但对各自含义不同的代码。 Unless you're making a conscious and well-informed effort to write dual-language code, use a C compiler for C code.除非您有意识且消息灵通地努力编写双语言代码,否则请为 C 代码使用 C 编译器。

In the second place, if make , using exactly this makefile:其次,如果make ,使用这个 makefile:

 CC = /usr/bin/g++ OBJECTS_INTERACTIVE = calcular.o calc_interactive.o calc_interactive: $(OBJECTS_INTERACTIVE) $(CC) $(OBJECTS_INTERACTIVE) -o $@ calcular.o: calcular.h calc_interactive.o: calcular.h

produces the output you claim it does:产生您声称的 output :

 jules@desktop:$ make /usr/bin/g++ -c calc_interactive.c /usr/bin/g++ calcular.o calc_interactive.o -o calc_interactive g++: error: calcular.o: No such file or directory makefile:11: recipe for target 'calc_interactive' failed make: *** [calc_interactive] Error 1

then the most likely conclusion is that calcular.o does not already exist, and there is no source file present from which make knows how to build it.那么最可能的结论是calcular.o不存在,并且不存在make知道如何构建它的源文件。 In particular, there is no calcular.c .特别是,没有calcular.c This would imply that the original makefile should no longer work, either.这意味着原来的 makefile 也不应该再工作了。 Perhaps you accidentally deleted the source file, or introduced a spelling error into filename or makefile, or similar.也许您不小心删除了源文件,或者在文件名或 makefile 或类似内容中引入了拼写错误。

I suppose there is also an outside chance that g++ 's error message is misleading.我想还有一个外部机会g++的错误消息具有误导性。 If calcular.o already exists and is up to date, but its access-control properties prevent you from opening it for reading, then make would not try to rebuild it, but g++ might issue a misleading diagnostic, where "No such file or directory" really means "I couldn't open such a file".如果calcular.o已经存在并且是最新的,但它的访问控制属性阻止您打开它进行读取,则make不会尝试重建它,但g++可能会发出误导性诊断,其中“没有这样的文件或目录" 真正的意思是“我无法打开这样的文件”。 In that case, removing calcular.o , so that make needs to rebuild it, might be the cleanest solution.在这种情况下,删除calcular.o以使make需要重建它,这可能是最干净的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM