title: Makefile : Build in a separate directory tree
body: <p>My project (an interpreted language) has a standard library composed by multiple files, each of them will be built into an .so dynamic library that the interpreter will load upon user request (with an import directive).
  Each source file is located into a subdirectory representing its "namespace", for instance :</p>
  
  <p><a href="https://i.stack.imgur.com/LRRC2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LRRC2.png" alt="libraries tree"></a>
  </p>
  
  <p>The build process has to create a "build" directory, then when each file is compiling has to create its namespace directory inside the "build" one, for instance, when compiling</p>
  
  <pre><code>std/io/network/tcp.cc
  </code></pre>
  
  <p>he run an mkdir command with </p>
  
  <pre><code>mkdir -p build/std/io/network
  </code></pre>
  
  <p>The Makefile snippet is :</p>
  
  <pre><code>STDSRC=stdlib/std/hashing/md5.cc \
         stdlib/std/hashing/crc32.cc \
         stdlib/std/hashing/sha1.cc \
         stdlib/std/hashing/sha2.cc \
         stdlib/std/io/network/http.cc \
         stdlib/std/io/network/tcp.cc \
         stdlib/std/io/network/smtp.cc \
         stdlib/std/io/file.cc \
         stdlib/std/io/console.cc \
         stdlib/std/io/xml.cc \
         stdlib/std/type/reflection.cc \
         stdlib/std/type/string.cc \
         stdlib/std/type/matrix.cc \
         stdlib/std/type/array.cc \
         stdlib/std/type/map.cc \
         stdlib/std/type/type.cc \
         stdlib/std/type/binary.cc \
         stdlib/std/encoding.cc \
         stdlib/std/os/dll.cc \
         stdlib/std/os/time.cc \
         stdlib/std/os/threads.cc \
         stdlib/std/os/process.cc \
         stdlib/std/pcre.cc \
         stdlib/std/math.cc
  
  STDOBJ=$(STDSRC:.cc=.so)
  
  all: stdlib
  
  stdlib: $(STDOBJ)
  
  .cc.so: 
      mkdir -p `dirname $&lt; | sed -e 's/stdlib/stdlib\/build/'`
      $(CXX) $&lt; -o `dirname $&lt; | sed -e 's/stdlib/stdlib\/build/'`/`basename $&lt; .cc`.so $(CFLAGS) $(LDFLAGS)
  </code></pre>
  
  <p>I have two questions :</p>
  
  <p>1 - The problem is that the make command, i really don't know why, doesn't check if a file was modified and launch the build process on ALL the files no matter what, so if i need to build only one file, i have to build them all or use the command :</p>
  
  <pre><code>make path/to/single/file.so
  </code></pre>
  
  <p>Is there any way to solve this?</p>
  
  <p>2 - Any way to do this in a "cleaner" way without have to distribute all the build directories with sources?</p>
  
  <p>Thanks </p>