title: error LNK2005 already defined - Compiler error when building after running
body: <p>I'm trying to follow <a href="http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/" rel="nofollow noreferrer">Riemer's DirectX with C++ tutorial</a>.</p>
  
  <p>I have made a few changes, for example here I have made my <code>InitializeDevice()</code> function in a separate file (<code>tdirect.cpp</code> / <code>tdirect.h</code>).</p>
  
  <p>When I press F5 to compile and run, the program works perfectly. But when I make a change to a value (e.g. <code>0xff00ffff</code> to <code>0xff0000ff</code> - cyan to blue) <em>without</em> choosing the "Rebuild" option I get a stream of errors in my console which prevent me from compiling the program. This is pretty annoying as you can imagine. It's as if it's compiling <code>tdirect.cpp</code> twice.</p>
  
  <p>Here's the source code of <code>tdirect.cpp</code>, <code>tdirect.h</code>, and the relevant part from <code>main.cpp</code> (<code>tdirect.h</code> is <strong><em>only</em></strong> included from <code>main.cpp</code>, and <code>basicvertex.h</code> is only included from <code>tdirect.cpp</code>)</p>
  
  <p><strong><code>tdirect.cpp</code></strong>:</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>#include "tdirect.h"
  #include "basicvertex.h"
  
  IDirect3D9 *pD3D;
  D3DPRESENT_PARAMETERS D3DParams; 
  
  LPDIRECT3DDEVICE9 InitializeDevice(HWND Wnd)
  {
      pD3D = Direct3DCreate9(D3D_SDK_VERSION);
      if (pD3D == NULL)
      {
          MessageBox(Wnd, "DirectX is not installed.", "No DirectX!", MB_OK);
          return NULL;
      }
  
      ZeroMemory(&amp;D3DParams, sizeof(D3DPRESENT_PARAMETERS));
  
      D3DParams.Windowed = TRUE;
      D3DParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
      D3DParams.BackBufferFormat = D3DFMT_UNKNOWN;
  
      LPDIRECT3DDEVICE9 pDevice;
      if (FAILED(pD3D-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Wnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &amp;D3DParams, &amp;pDevice)))
      {
          if (FAILED(pD3D-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, Wnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &amp;D3DParams, &amp;pDevice)))
          {
              MessageBox(Wnd, "Failed to create reference device.", "No software device!", MB_OK);
          }
          else {
              MessageBox(Wnd, "Falling back to software mode.", "No hardware device!", MB_OK);
          }
      }
  
      return pDevice;
  }
  
  LPDIRECT3DVERTEXBUFFER9 vBuffer;
  void Init(LPDIRECT3DDEVICE9 pDevice)
  {
      BASICVERTEX Vertices[3];
      Vertices[0].x = 150;
      Vertices[0].y = 100;
      Vertices[0].weight = 1;
      Vertices[0].colour = 0xffff0000;
  
      Vertices[1].x = 350;
      Vertices[1].y = 100;
      Vertices[1].weight = 1;
      Vertices[1].colour = 0xff00ff00;
  
      Vertices[2].x = 250;
      Vertices[2].y = 300;
      Vertices[2].weight = 1;
      Vertices[2].colour = 0xff00ffff;
  
      if (FAILED(pDevice-&gt;CreateVertexBuffer(sizeof(BASICVERTEX)*3, 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &amp;vBuffer, NULL)))
      {
          MessageBox(NULL, "Failed to create vertex buffer", "Fail", MB_OK);
      }
      else {
          void* pVertices;
          if (FAILED(vBuffer-&gt;Lock(0, sizeof(BASICVERTEX)*3, (void**)&amp;pVertices, 0)))
          {
              MessageBox(NULL, "Failed to lock vertex buffer", "Fail", MB_OK);
          }
          else {
              memcpy(pVertices, Vertices, sizeof(BASICVERTEX)*3);
              vBuffer-&gt;Unlock();
          }
      }
  }
  
  void DrawScene(LPDIRECT3DDEVICE9 pDevice)
  {
      pDevice-&gt;Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
      pDevice-&gt;BeginScene();
  
      pDevice-&gt;SetStreamSource(0, vBuffer, 0, sizeof(BASICVERTEX));
      pDevice-&gt;SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
      pDevice-&gt;DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  
      pDevice-&gt;EndScene();
      pDevice-&gt;Present(NULL, NULL, NULL, NULL);
  }
  </code></pre>
  
  <p><strong><code>tdirect.h</code></strong>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  #include &lt;Windows.h&gt;
  </code></pre>
  
  <p><strong><code>main.cpp</code> include part</strong>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Windows.h&gt;
  
  #include "dxheader.h"
  #include "tdirect.cpp"
  </code></pre>
  
  <p>Compiler Errors:</p>
  
  <pre class="lang-none prettyprint-override"><code>1&gt;Debug\tdirect.obj : warning LNK4042: object specified more than once; extras ignored
  1&gt;main.obj : error LNK2005: "struct IDirect3DDevice9 * __cdecl InitializeDevice(struct HWND__ *)" (?InitializeDevice@@YAPAUIDirect3DDevice9@@PAUHWND__@@@Z) already defined in tdirect.obj
  1&gt;main.obj : error LNK2005: "void __cdecl Init(struct IDirect3DDevice9 *)" (?Init@@YAXPAUIDirect3DDevice9@@@Z) already defined in tdirect.obj
  1&gt;main.obj : error LNK2005: "void __cdecl DrawScene(struct IDirect3DDevice9 *)" (?DrawScene@@YAXPAUIDirect3DDevice9@@@Z) already defined in tdirect.obj
  1&gt;main.obj : error LNK2005: "struct IDirect3DVertexBuffer9 * vBuffer" (?vBuffer@@3PAUIDirect3DVertexBuffer9@@A) already defined in tdirect.obj
  1&gt;main.obj : error LNK2005: "struct _D3DPRESENT_PARAMETERS_ D3DParams" (?D3DParams@@3U_D3DPRESENT_PARAMETERS_@@A) already defined in tdirect.obj
  1&gt;main.obj : error LNK2005: "struct IDirect3D9 * pD3D" (?pD3D@@3PAUIDirect3D9@@A) already defined in tdirect.obj
  1&gt;C:\Users\me\Documents\Visual Studio 2010\Projects\DirectX\Debug\DXStuff.exe : fatal error LNK1169: one or more multiply defined symbols found
  </code></pre>
  
  <p>Thanks in advance :)</p>