title: Checking for existence of a directory with GNU make
body: <p>I am dumping all the object files of my compilation into a separate directory called
  <code>objfiles</code></p>
  
  <p>While compiling I want to check for the existence of the directory objfiles
  and if it does not exist (indicating re-compilation of the whole project)
  I want to create it.</p>
  
  <p>Here is my Makefile and an attempt at the problem</p>
  
  <pre><code>    SHELL := /bin/zsh
      #All object files are dumped into OBJDIR
      OBJDIR=objfiles
      OBJECTS=$(OBJDIR)/main.o \
              $(OBJDIR)/printdata.o \
              #...
              #...
  
     COMPILER=nvcc -arch=sm_20
  
     exec: objdir_exist $(OBJECTS)
          $(COMPILER) $(OBJECTS) -o exec
          @printf "\nExecutable updated!\n\n"
  
      $(OBJDIR)/main.o: main.cu octree.h
          $(COMPILER) -c $&lt; -o $@
  
      $(OBJDIR)/printdata.o: printdata.cu  
          $(COMPILER) -c $&lt; -o $@
      ...    
  
  
      #Clean-up executables and object files
      .PHONY=clean objdir_exist
      clean:
          rm exec 
          rm -r $(OBJDIR)
  
      # Check for the existence of object files. 
      # If it does not exist, then create another object_file directory to dump data to.
      # If it exists do nothing
     objdir_exist: 
          if [ ! -d "$(OBJDIR)" ] then mkdir $(OBJDIR)  fi
  </code></pre>
  
  <p>I get the following error.</p>
  
  <pre><code>if [ ! -d "objfiles" ] then mkdir objfiles  fi
  zsh:[:1: ']' expected
  nvcc -arch=sm_20 -c main.cu -o objfiles/main.o
  Assembler messages:
  Fatal error: can't create objfiles/main.o: No such file or directory
  make: *** [objfiles/main.o] Error 1
  </code></pre>
  
  <p>Where am I going wrong? </p>