简体   繁体   中英

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. 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. 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

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 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. Unless you're making a conscious and well-informed effort to write dual-language code, use a C compiler for C code.

In the second place, if make , using exactly this 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:

 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. In particular, there is no calcular.c . This would imply that the original makefile should no longer work, either. Perhaps you accidentally deleted the source file, or introduced a spelling error into filename or makefile, or similar.

I suppose there is also an outside chance that g++ 's error message is misleading. 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". In that case, removing calcular.o , so that make needs to rebuild it, might be the cleanest solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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