-1

I create an ugly Makefile, it worked beyond my expectations. When main.o is not built successfully, the executable is built every time I make. In my opinion, after the main.o target command is executed, make will compare the timestamp of the main.o file with the main file. There is no main.o file at this time, so the main file should be the latest. Why is the command of the main target executed. In my case main is always present after the first build and it is rebuilt each time make is executed, Although the content is the same.

When I add a main.o file that is older than main by shell, I change the main.c file and use the make command to update only main1.o, but not main, indicating that make compares main and main.o timestamp.

Is there a problem with my understanding? Is it my misunderstanding of the execution process of make.

main: main.o
    gcc -o main1 main1.o
    echo generate

main.o: main.c
    gcc -c main.c -o main1.o

I don't have enough reputation so I've inserted an image link below

https://i.sstatic.net/VAX6j.png

2
  • 2
    The target in the Makefile is main but the rule builds main1. The image is different from your text but it has the target main.o and the rule builds main1.o. I don't see how you can ever get a clean build with this Makefile.
    – doneal24
    Commented Jul 20, 2023 at 17:04
  • I've read through your text three times, but I cannot make sense of it. Try to restructure your thoughts and restructure the text accordingly. Also: do not add pictures of text but add the text to the question. Inserting a picture of text is actually bad for your reputation, so the remark that you added a picture because you don't have enough reputation is wrong. Commented Jul 29, 2023 at 22:46

1 Answer 1

0

I understand that you have a probleem with the Makefile

main: main.o
    gcc -o main1 main1.o
    echo generate

main.o: main.c
    gcc -c main.c -o main1.o

There are a number of issues with this Makefile, most of them created by the confusion between main and main1.

When you type make, it will try to create the target main. Main depends on main.o, so make will try to create main.o. There is a rule for that in the Makefile, so make will use that and execute

gcc -c main.c -o main1.o

Note that this creates main1.o and not main.o. After the execution of this rule, there is still no main.o.

make doesn't care about that and will execute the rules to create main:

gcc -o main1 main1.o

This will create the executable main1, not mai that was the original target. However, main1 will be there.

The next time you run make, make will go through the same procedure. It will try to create the target main, because main is not there. main1 is there, but that is not the target that make wants to create.

main depends on main.o. There is no main.o, so make will try to create it. There is a main1.o, but main does not depend on that. As we've seen, there is a rule for main.o that is then executed.

and so on.

A correct version would be:

main: main.o
    gcc -o main main.o
    echo generate

main.o: main.c
    gcc -c main.c -o main.o

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .