title: g++ compile source files existing in another directory
body: <p>I am trying to setup a build process using makefiles, for a C++ plugin that I am working on. I am curious to know if there is any way to make g++ compile <em>source</em> files found in another directory. My motivation for doing this is to avoid having to specify the relative path for each source file, as I explain below.</p>
  
  <p>My project directory structure looks like:</p>
  
  <pre><code>MyPlugin --&gt; src   --&gt; Foo.cpp
                     --&gt; Foo.h
                     --&gt; Bar.cpp
                     --&gt; Bar.cpp
           --&gt; build --&gt; Makefile
  </code></pre>
  
  <p>Following is a stripped down version of my current Makefile:</p>
  
  <pre><code>SRC_PATH=../src
  OUT_PATH=../bin
  VPATH=${SRC_PATH}
  FILES=Foo.cpp Bar.cpp
  
  CC=g++
  CFLAGS=-Wall -shared
  
  all:
      mkdir -p ${OUT_PATH}
      ${CC} ${CFLAGS} -I${SRC_PATH} ${FILES} -o ${OUT_PATH}/MyPlugin.so
  </code></pre>
  
  <p>By doing this, I am trying to avoid defining the <code>FILES</code> variable as below:</p>
  
  <pre><code>FILES=../src/Foo.cpp ../src/Bar.cpp
  </code></pre>
  
  <p>When I tried running <code>make all</code>, g++ gave me an error. It looks like the path specified through <code>-I</code> flag is only used to search for <code>#include</code>d files.</p>
  
  <pre><code>g++: Foo.cpp: No such file or directory
  g++: Bar.cpp: No such file or directory
  </code></pre>
  
  <p>I cannot use wildcards (<code>*.cpp</code>) because I do not always want all the files to be picked up for compilation. Another alternative is to <code>cd</code> to the src directory, as mentioned <a href="https://stackoverflow.com/questions/1139271/makefiles-with-source-files-in-different-directories">here</a>, and run g++ from there, but that only works for me if all the files are in the same directory (I need the output to be a single <code>.so</code> file). I also tried setting the environment variable <code>PATH</code>, but that didn't seem to have any effect. </p>
  
  <p>I have looked into the g++ help, the make documentation, and looked at StackOverflow posts such as <a href="https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class-in-a-separate-directory">https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class-in-a-separate-directory</a> and <a href="https://stackoverflow.com/questions/12919081/gcc-g-no-such-file-or-directory">gcc/g++: &quot;No such file or directory&quot;</a> , but could not find any solution which I could use. Could you please suggest me an suitable approach to this problem? </p>
  
  <p><strong>Edit</strong> The example may have been misleading. In this stripped down example, I have all the source files in one directory, but in my actual project, I have several subdirectories, and multiple files in each directory. Thus, although <code>cd</code>ing to the <code>src</code> directory works in my above example, it won't work in my actual project (or at least, I would like to know how it would work).</p>