Building C-program "out of source tree" with GNU make
<p>I would like to build a C-project for my microcontroller with the GNU make tool. I would like to do it in a clean way, such that my source code is not cluttered with object files and other stuff after the build. So imagine that I have a project folder, called "myProject" with two folders in it:</p>

<pre><code>- myProject
     |
     |---+ source
     |
     '---+ build
</code></pre>

<p>The build folder only contains a makefile. The figure below shows what should happen when I run the GNU make tool:</p>

<p><a href="https://i.stack.imgur.com/x5Rbm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/x5Rbm.png" alt="enter image description here"></a></p>

<p>So GNU make should create an object file for each .c source file it can find in the source folder. The object files should be structured in a directory tree that is similar to the structure in the source folder.</p>

<p>GNU make should also make a .d dependency file (in fact, a dependency file is some sort of makefile itself) for each .c source file. The dependency file is described in the GNU make manual chapter 4.14 "Generating Prerequisites Automatically":</p>

<blockquote>
  <p>For each source file <strong>name.c</strong> there is a makefile <strong>name.d</strong> which lists
  what files the object file <strong>name.o</strong> depends on.</p>
</blockquote>

<p>From the following Stackoverflow question <a href="https://stackoverflow.com/questions/39002087/about-the-gnu-make-dependency-files-d">About the GNU make dependency files *.d</a>, I learned that adding the options <code>-MMD</code> and <code>-MP</code> to the <code>CFLAGS</code> of the GNU gcc compiler can help to automate that.</p>

<p>So now comes the question. Has anyone a sample makefile that performs such out-of-source build? Or some good advices on how to get started?</p>

<p>I'm pretty sure that most people who have written such a makefile, are Linux-people. But the microcontroller project should build also on a Windows machine. Anyway, even if your makefile is Linux-only, it provides a good starting point ;-)</p>

<p>PS: I would like to avoid extra tools like CMake, Autotools, or anything that has to do with an IDE. Just pure GNU make.</p>

<p>I would be very grateful :-)</p>

<hr>

<p><strong>Updating the dependency files</strong><br>
Please have a look at this question: <a href="https://stackoverflow.com/questions/47700541/what-is-the-exact-chain-of-events-when-gnu-make-updates-the-d-files">What is the exact chain of events when GNU make updates the .d files?</a></p>