1

I write a makefile and I want it to execute the program for some reasons after compiling.
But it is too slow when I only use the command 'make'.So I add argument '-j' to speed up.
I just want it to compile all files first and run the program compiled automatically,but it run the program first before the compilation.
How can I control the order partly?

makefile

all:test run
test:test1.o test2.o test3.o
    gcc test1.o test2.o test3.o -o test
test1:test1.c
    gcc -c test1.c
test2:test2.c
    gcc -c test2.c
test3:test3.c
    gcc -c test3.c
run:
    ./test

It may run ./test first before it compile completely.

Pudron
  • 66
  • 1
  • 7
  • 1
    The problem you're seeing is almost certainly due to dependencies being incorrectly specified (or just incomplete). Please edit your question to include the makefile. – G.M. Feb 27 '20 at 10:57

1 Answers1

1

How can I control the order partly?

You control order by specifying dependencies between targets. A target doesn't get built until its prerequisites have been built.

run should depend on test:

run: test
    ./test

.PHONY: run

Since run is not a file or directory, it should be marked as .PHONY target.

I normally have:

run_% : %
    ./$<
.PHONY: run_%

So that you can use make run_this and make run_that and it first builds this and that.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271