Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the gnu make command default to compiling c files with the same name

environment

os: macos 12.6.3 (intel)

gnu make: 3.81

execute command: make -v get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
 
This program built for i386-apple-darwin11.3.0

steps

The directory structure is as follows

./
├── Makefile
└── abc.c

There are only two files in the directory: Makefile and abc.c, where the abc.c file is empty file and has no content

The content of the Makefile is as follows:

test: abc.o
    @echo 'execute test.'
clean:
    $(RM) test abc.o
.PHONY: test clean

Then, execute command: make clean && make test , It will output:

rm -f test abc.o
cc    -c -o abc.o abc.c
execute test.

So, My question is, why does the make command automatically execute the cc -c -o command in the second line of output? Is this make command default behavior?

I tried renaming the abc.c file to abc.java and executing the command again, which was output as I expected. My original intention was to check if the abc.o file exists, if file not exist, throw an error or output something

I cannot simply describe this issue, so I don’t know how to use Google search. Can you provide me with an official document link to explain this behavior, or are there parameters that can avoid this default behavior?

>Solution :

make has implicit rule that will compile a .c file into a .o file. For more details take a look at the make manual here: https://www.gnu.org/software/make/manual/make.html#Implicit-Rules

The --no-builtin-rules flag will prevent this behaviour.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading