I have an rule that creates a directory
bin:
-mkdir $@
However after the first time the directory has been generated, I receive this output:
mkdir bin
mkdir: cannot create directory `bin': File exists
make: [bin] Error 1 (ignored)
Is there some way I can only run the rule if the directory doesn't exist, or suppress the output when the directory already exists?
Make assumes the first target is the default target. If that is your complete makefile, then it must be always trying to remake the default target, which is bin. Insert the following lines to the top of your makefile:
The error is ignored already by the leading '
-
' on the command line. If you really want to lose the error messages frommkdir
, use I/O redirection:You will still get the 'ignored' warning from
make
, though, so it might be better to use the option tomkdir
that doesn't cause it to fail when the target already exists, which is the-p
option:The
-p
option actually creates all the directories that are missing on the given paths, so it can generate a a number of directories in one invocation, but a side-effect is that it does not generate an error for already existing directories. This does assume a POSIX-ish implementation ofmkdir
; older machines may not support it (though it has been standard for a long time now).You rule should not be executed unless its target does not exists or is out of date because of its dependencies. In other words, you should never encounter this error.
[Example Added]
Does your folder depend on anything? Is your folder a phony target?
Well I ended up with this construct, maybe someone will find it useful or can comment on it:
The traditional way to handle directory creation is to use a stamp file that is depended on and creates the dir as a side effect. Remove the stamp file when making
distclean
or whatever your "really clean" target is:The reason for this is as follows: whenever a file in
bin
is created/removed, the mtime of the containing directory is updated. If a target depends onbin
, then the next timemake
runs, it will then recreate files that it doesn't need to.Another way to suppress the
make: error ... (ignored)
output is to append|| true
to a command that could fail. Example with grep that checks for errors in a LaTeX log file:Without the
|| true
, make complains when grep fails to find any matches.This works for all commands, not just mkdir; that's why I added this answer.