title: Arduino library: multiple definitions of a function
body: <p>Today I encountered a weird problem when trying to use IRremote library, and I managed to strip down the problem as following. If you have a folder in libraries, with <code>Foo.h</code> and <code>Foo.cpp</code> inside, and write a <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow noreferrer">sketch</a> to include Foo.h:</p>
  
  <h3>Foo.h</h3>
  
  <pre><code>#ifndef Foo_H
  #define Foo_H
  
  int AAA() {
      return 0;
  }
  
  #endif
  </code></pre>
  
  <h3>Foo.cpp</h3>
  
  <pre><code>#include "Foo.h"
  </code></pre>
  
  <h3>Sketch</h3>
  
  <pre><code>#include &lt;Foo.h&gt;
  
  void setup(){
  
  }
  
  void loop(){
  
  }
  </code></pre>
  
  <p>The error message is:</p>
  
  <pre><code> Foo\Foo.cpp.o: In function `AAA()':
  
   E:\workShop\Arduino\libraries\Foo\/Foo.h:5: multiple definition of `AAA()'
  
   includeTest.cpp.o:E:\workShop\Arduino\libraries\Foo/Foo.h:5:
  
   first defined here
  </code></pre>
  
  <p>I'm using a Windows 7 32-bit machine. Tested on Arduino 1.0.5, 1.0.4, and 21, 22.</p>
  
  <hr>
  
  <p>So with some research I figured out the problem comes from my confusion of preprocessor and linking. This question explains how preprocessor includes file and include guard:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/333889/in-c-why-have-header-files-and-cpp-files">In C++ why have header files and cpp files?</a></li>
  </ul>
  
  <p>These are some of the pages helped me understand linking:</p>
  
  <ul>
  <li><p><em><a href="http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/" rel="nofollow noreferrer">1.8 — Programs with multiple files</a></em></p></li>
  <li><p><em><a href="http://www.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html" rel="nofollow noreferrer">GCC and Make - A Tutorial on how to compile, link and build C/C++ applications</a></em> </p></li>
  <li><p><em><a href="http://www.delorie.com/djgpp/doc/ug/larger/multisrc.html" rel="nofollow noreferrer">Guide: Multiple sources</a></em> </p></li>
  </ul>
  
  <p>And this is a better explanation of inline specifier:</p>
  
  <ul>
  <li><em><a href="http://en.cppreference.com/w/cpp/language/inline" rel="nofollow noreferrer">inline specifier</a></em> </li>
  </ul>