title: Include issue: 'multiple definition', 'first defined here'
body: <p>I have three files:</p>
  
  <pre><code>main.cpp
  MyClass.cpp
  MyClass.hpp
  </code></pre>
  
  <p>I have a library header file, <code>"testLib.hpp"</code>, that I want to include in <code>MyClass.hpp</code> so that I can have one of testLib's objects be a class attribute. </p>
  
  <p>I include <code>MyClass.hpp</code> in <code>MyClass.cpp</code> and in <code>main.cpp</code>. When attempting to compile the project, I get the following errors</p>
  
  <pre><code>MyClass.cpp   multiple definition of 'testLib::testLib::function1()
  obj/Release/main.o:main.cpp first defined here
  MyClass.cpp   multiple definition of 'testLib::testLib::function2()
  obj/Release/main.o:main.cpp first defined here
  </code></pre>
  
  <p>and so on.</p>
  
  <p>Both <code>main.cpp</code> and <code>MyClass.cpp</code> include <code>MyClass.hpp</code> (which includes <code>testLib.hpp</code>). Judging by the error, it looks like <code>MyClass.cpp</code> is attempting to include the library functions after they've already been included by <code>main.cpp</code>. However, I have include guards present in <code>MyClass.hpp</code> so I don't understand how it's trying to include <code>MyClass.hpp</code> twice. </p>
  
  <p>Here's the code:</p>
  
  <p>MyClass.hpp</p>
  
  <pre><code>#ifndef THIS_HEADER_H
  #define THIS_HEADER_H
  
  #include &lt;stdint.h&gt;
  #include &lt;iostream&gt;
  #include "testLib/testLib.hpp"
  
  class MyClass
  {
  
  public:
      void test();
      int foo;
  
  private:
      uint32_t bar;
      //I want to include an object from the library as part of this class
      //TestLib::Device device;
  };
  
  #endif
  </code></pre>
  
  <p>MyClass.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "MyClass.hpp"
  
  void MyClass::test()
  {
  
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "MyClass.hpp"
  
  using namespace std;
  
  int main()
  {
      cout &lt;&lt; "Hello world!" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>EDIT</strong>
  I tried to hide the actual filenames to make the question more general and clear, but it seems like the problem might be resulting from 'testLib.hpp', which I did not write. That file is actually the following "sweep.hpp" file. I got the 'multiple definition of/first defined here' errors for each of the public functions in this file:</p>
  
  <p>sweep.hpp</p>
  
  <pre><code>#ifndef SWEEP_DC649F4E94D3_HPP
  #define SWEEP_DC649F4E94D3_HPP
  
  /*
   * C++ Wrapper around the low-level primitives.
   * Automatically handles resource management.
   *
   * sweep::sweep  - device to interact with
   * sweep::scan   - a full scan returned by the device
   * sweep::sample - a single sample in a full scan
   *
   * On error sweep::device_error gets thrown.
   */
  
  #include &lt;cstdint&gt;
  #include &lt;memory&gt;
  #include &lt;stdexcept&gt;
  #include &lt;vector&gt;
  
  #include &lt;sweep/sweep.h&gt;
  
  namespace sweep {
  
  // Error reporting
  
  struct device_error final : std::runtime_error {
    using base = std::runtime_error;
    using base::base;
  };
  
  // Interface
  
  struct sample {
    const std::int32_t angle;
    const std::int32_t distance;
    const std::int32_t signal_strength;
  };
  
  struct scan {
    std::vector&lt;sample&gt; samples;
  };
  
  class sweep {
  public:
    sweep(const char* port);
    sweep(const char* port, std::int32_t bitrate);
    void start_scanning();
    void stop_scanning();
    bool get_motor_ready();
    std::int32_t get_motor_speed();
    void set_motor_speed(std::int32_t speed);
    std::int32_t get_sample_rate();
    void set_sample_rate(std::int32_t speed);
    scan get_scan();
    void reset();
  
  private:
    std::unique_ptr&lt;::sweep_device, decltype(&amp;::sweep_device_destruct)&gt; device;
  };
  
  // Implementation
  
  namespace detail {
  struct error_to_exception {
    operator ::sweep_error_s*() { return &amp;error; }
  
    ~error_to_exception() noexcept(false) {
      if (error) {
        device_error e{::sweep_error_message(error)};
        ::sweep_error_destruct(error);
        throw e;
      }
    }
  
    ::sweep_error_s error = nullptr;
  };
  }
  
  sweep::sweep(const char* port)
      : device{::sweep_device_construct_simple(port, detail::error_to_exception{}), &amp;::sweep_device_destruct} {}
  
  sweep::sweep(const char* port, std::int32_t bitrate)
      : device{::sweep_device_construct(port, bitrate, detail::error_to_exception{}), &amp;::sweep_device_destruct} {}
  
  void sweep::start_scanning() { ::sweep_device_start_scanning(device.get(), detail::error_to_exception{}); }
  
  void sweep::stop_scanning() { ::sweep_device_stop_scanning(device.get(), detail::error_to_exception{}); }
  
  bool sweep::get_motor_ready() { return ::sweep_device_get_motor_ready(device.get(), detail::error_to_exception{}); }
  
  std::int32_t sweep::get_motor_speed() { return ::sweep_device_get_motor_speed(device.get(), detail::error_to_exception{}); }
  
  void sweep::set_motor_speed(std::int32_t speed) {
    ::sweep_device_set_motor_speed(device.get(), speed, detail::error_to_exception{});
  }
  
  std::int32_t sweep::get_sample_rate() { return ::sweep_device_get_sample_rate(device.get(), detail::error_to_exception{}); }
  
  void sweep::set_sample_rate(std::int32_t rate) {
    ::sweep_device_set_sample_rate(device.get(), rate, detail::error_to_exception{});
  }
  
  scan sweep::get_scan() {
    using scan_owner = std::unique_ptr&lt;::sweep_scan, decltype(&amp;::sweep_scan_destruct)&gt;;
  
    scan_owner releasing_scan{::sweep_device_get_scan(device.get(), detail::error_to_exception{}), &amp;::sweep_scan_destruct};
  
    auto num_samples = ::sweep_scan_get_number_of_samples(releasing_scan.get());
  
    scan result;
    result.samples.reserve(num_samples);
  
    for (std::int32_t n = 0; n &lt; num_samples; ++n) {
      auto angle = ::sweep_scan_get_angle(releasing_scan.get(), n);
      auto distance = ::sweep_scan_get_distance(releasing_scan.get(), n);
      auto signal = ::sweep_scan_get_signal_strength(releasing_scan.get(), n);
  
      result.samples.push_back(sample{angle, distance, signal});
    }
  
    return result;
  }
  
  void sweep::reset() { ::sweep_device_reset(device.get(), detail::error_to_exception{}); }
  
  } // ns
  
  #endif
  </code></pre>