How can I tell gnu make not to build some recipe in parallel. Let's say I have the following makefile :
sources = a.xxx b.xxx c.xxx
target = program
all : $(target)
$(target) : $(patsubst %.xxx,%.o,$(sources))
$(CXX) -o $@ $<
%.o : %.cpp
$(CXX) -c -o $@ $<
%.cpp : %.xxx
my-pre-processor -o $@ $<
However, the my-pre-processor
command create temporary files with fixed name (I cannot change this). This is working fine if I just use make without the -j
parameter. However, if the -j
option is used, the build sometimes fails because two concurrent invocation of my-pre-processor
overwrite their temporary files.
I'd like to know if there is a way to tell make that it must not build the try to parallelize the execution of the %.cpp : %.xxx
recipes.
Normally, the make command runs commands sequentially for only one target at a time, waiting for the command to finish before running the next. However, the make command can also run in a parallel run mode, where it can run many concurrent jobs to build independent targets.
PHONY: allows to declare phony targets, so that make will not check them as actual file names: it will work all the time even if such files still exist. You can put several . PHONY: in your Makefile : .
GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files.
-j [jobs], --jobs[=jobs] Specifies the number of jobs (commands) to run simultaneously. If there is more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously.
GNU Make contains the special built-in pseudo-target .NOTPARALLEL
Example:
.PHONY: all clean
.NOTPARALLEL:
anotherTarget: dependency1
You can also use the -j <n>
,--jobs[=<n>]
flag on the command line where n
is the number of recipies allowed to run in parallel.
Usage:
make -j <n>
or make --jobs=<n>
Example:
make -j 1
or make --jobs=1
note: omitting <n>
will allow an arbitrary number of recipes to be executed, only limited by your system's available resources
Finally, you can assign the command line flag in solution 2 to the MAKEFLAGS
variable from within your Makefile
Example:
MAKEFLAGS := -j 1
or
MAKEFLAGS := --jobs=1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With