title: Confusion over inline behavior C++
body: <p>I am learning C++ and I am confused by inline behavior. On <a href="https://en.cppreference.com/w/cpp/language/inline" rel="nofollow noreferrer">cppreference</a> I found that " function included in multiple source files must be inline". Their example is the following:</p>
  
  <pre><code>// header file
  #ifndef EXAMPLE_H
  #define EXAMPLE_H
  // function included in multiple source files must be inline
  inline int sum(int a, int b) 
  {
      return a + b;
  }
  #endif
  
  // source file #2
  #include "example.h"
  int a()
  {
      return sum(1, 2);
  }
  
  // source file #1
  #include "example.h"
  int b()
  {
      return sum(3, 4);
  }
  </code></pre>
  
  <p>This is a bit confusing to me - I thought that the ifndef guard was doing this job exactly, i.e. preventing problems when the same file was included multiple times. In any case, I wanted to test this, so I prepared the following:</p>
  
  <pre><code>// Sum.h
  #ifndef SUM_H
  #define SUM_H
  int sum(int a, int b);
  #endif
  
  // Sum.cpp
  int sum(int a, int b){
      return a + b;
  }
  
  // a.h
  #ifndef A_H
  #define A_H
  int af();
  #endif
  
  // a.cpp
  #include "sum.h"
  
  int af(){
      return sum(3, 4);
  }
  
  // b.h
  #ifndef B_H
  #define B_H
  int bf();
  #endif 
  
  // b.cpp
  #include "sum.h"
  
  int bf(){
      return sum(1, 2);
  }
  
  // main.cpp
  #include "sum.h"
  #include "a.h"
  #include "b.h"
  #include &lt;iostream&gt;
  int main() {
      std::cout &lt;&lt; af() + bf();
  }
  </code></pre>
  
  <p>And this works fine as expected. Then I use define the sum function to be inline in sum.cpp and sum.h, and it fails to compile:</p>
  
  <pre><code>"sum(int, int)", referenced from:
        bf() in b.cpp.o
        af() in a.cpp.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>Can somebody clarify this for me?</p>