title: When do I need to use/have a makefile?
body: <p>I am trying to learn how to code on a Raspberry PI.
  I am coming from coding using Windows and VS Code.
  Now I am using Linux Mint 19.1 and ssh to access Raspbian 4.14.</p>
  
  <p>The problem is that, after a long battle with downloading the library that I am trying to use, after installing a compiler, creating a file, and finding the right command to run and include paths I get <code>undefined reference to</code> errors.</p>
  
  <p>I am trying to compile the simplest example code from <a href="https://github.com/hzeller/rpi-rgb-led-matrix" rel="nofollow noreferrer">https://github.com/hzeller/rpi-rgb-led-matrix</a>
  because I started with this project. I don't have any other code or example.</p>
  
  <p><strong>Here are the commands that I've wrote in command line:</strong></p>
  
  <pre><code>sudo apt-get install g++
  mkdir 4panel
  cd 4panel
  sudo nano main.cpp
  git clone https://github.com/hzeller/rpi-rgb-led-matrix.git
  sudo g++ -Wall -Irpi-rgb-led-matrix/include  main.cpp -o main
  </code></pre>
  
  <p>The following commands should be:</p>
  
  <pre><code>sudo chmod +x main
  ./main
  </code></pre>
  
  <p><strong>main.cpp contains the same code as</strong> <a href="https://github.com/hzeller/rpi-rgb-led-matrix/blob/master/examples-api-use/minimal-example.cc" rel="nofollow noreferrer">https://github.com/hzeller/rpi-rgb-led-matrix/blob/master/examples-api-use/minimal-example.cc</a></p>
  
  <pre><code>#include "led-matrix.h"
  
  #include &lt;unistd.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;signal.h&gt;
  
  using rgb_matrix::GPIO;
  using rgb_matrix::RGBMatrix;
  using rgb_matrix::Canvas;
  
  volatile bool interrupt_received = false;
  static void InterruptHandler(int signo) {
    interrupt_received = true;
  }
  
  static void DrawOnCanvas(Canvas *canvas) {
    /*
     * Let's create a simple animation. We use the canvas to draw
     * pixels. We wait between each step to have a slower animation.
     */
    canvas-&gt;Fill(0, 0, 255);
  
    int center_x = canvas-&gt;width() / 2;
    int center_y = canvas-&gt;height() / 2;
    float radius_max = canvas-&gt;width() / 2;
    float angle_step = 1.0 / 360;
    for (float a = 0, r = 0; r &lt; radius_max; a += angle_step, r += angle_step) {
      if (interrupt_received)
        return;
      float dot_x = cos(a * 2 * M_PI) * r;
      float dot_y = sin(a * 2 * M_PI) * r;
      canvas-&gt;SetPixel(center_x + dot_x, center_y + dot_y,
                       255, 0, 0);
      usleep(1 * 1000);  // wait a little to slow down things.
    }
  }
  
  int main(int argc, char *argv[]) {
    RGBMatrix::Options defaults;
    defaults.hardware_mapping = "regular";  // or e.g. "adafruit-hat"
    defaults.rows = 32;
    defaults.chain_length = 1;
    defaults.parallel = 1;
    defaults.show_refresh_rate = true;
    Canvas *canvas = rgb_matrix::CreateMatrixFromFlags(&amp;argc, &amp;argv, &amp;defaults);
    if (canvas == NULL)
      return 1;
  
    // It is always good to set up a signal handler to cleanly exit when we
    // receive a CTRL-C for instance. The DrawOnCanvas() routine is looking
    // for that.
    signal(SIGTERM, InterruptHandler);
    signal(SIGINT, InterruptHandler);
  
    DrawOnCanvas(canvas);    // Using the canvas.
  
    // Animation finished. Shut down the RGB matrix.
    canvas-&gt;Clear();
    delete canvas;
  
    return 0;
  }
  </code></pre>
  
  <p><strong>Errors:</strong></p>
  
  <pre><code>/tmp/cci8MGL5.o: In function `main':
  main.cpp:(.text+0x264): undefined reference to `rgb_matrix::RGBMatrix::Options::Options()'
  main.cpp:(.text+0x2a8): undefined reference to `rgb_matrix::CreateMatrixFromFlags(int*, char***, rgb_matrix::RGBMatrix::Options*, rgb_matrix::RuntimeOptions*, bool)'
  collect2: error: ld returned 1 exit status
  
  </code></pre>
  
  <p><strong>Possible fixes that I found:</strong></p>
  
  <ul>
  <li><ol>
  <li>Link .cpp files in a strict order when compiling: <code>sudo g++ main1.cpp main2.cpp</code> </li>
  </ol></li>
  <li><ol start="2">
  <li><a href="https://stackoverflow.com/questions/14557657/linker-error-c-undefined-reference?answertab=votes#tab-top">This</a></li>
  </ol></li>
  <li><ol start="3">
  <li>The need of a <code>makefile</code></li>
  </ol></li>
  </ul>
  
  <p>While searching for similar issues and hopefully finding some magic fix, I ended up more puzzled than before. I have no idea if what I did is what I was supposed to do.</p>
  
  <ul>
  <li><ol>
  <li>What cpp files to link if I have just one ?</li>
  </ol></li>
  <li><ol start="2">
  <li>Do I need to link all the .cpp and .h files that the library has ?</li>
  </ol></li>
  <li><ol start="3">
  <li>I don't understand a thing about <code>makefile</code> or why it even exists.</li>
  </ol></li>
  </ul>
  
  <p>My issue may be specific to my situation but I believe that the answers will help me with future projects.</p>
  
  <p>You are free to answer all of my questions and misunderstandings and help me understand what is going on or answer just the question related to the title: <strong>Why <code>makefile</code> ?</strong></p>