title: Problem calling a function when it is in a .lib
body: <p>I have a class with a static method that looks roughly like:</p>
  
  <pre><code>class X {
      static float getFloat(MyBase&amp; obj) {
          return obj.value();  // MyBase::value() is virtual
      }
  };
  </code></pre>
  
  <p>I'm calling it with an instance of MyDerived which subclasses MyBase:</p>
  
  <pre><code>MyDerived d;
  float f = X::getFloat(d);
  </code></pre>
  
  <p>If I link the obj file containing X into my executable, everything works as expected. If I'm expecting to get 3.14, I get it.</p>
  
  <p>If I create a .lib that contains the X.obj file and link in the .lib, it breaks. When I call getFloat(), it's returning <b>-1.#IND00</b>. Is this some type of sentinel value that should tell me what's wrong here?</p>
  
  <p>Is anything different when you link in a lib rather than an obj directly? </p>
  
  <p>I don't get any compiler warnings or errors.</p>
  
  <p><b>Edit:</b><br>
  I'm using Visual Studio 2005 on Windows XP Pro SP3. To make sure I wasn't linking old files, I cloned the value() method into a new value2() method and called that instead. The behavior was the same.</p>
  
  <p><b>Edit #2:</b><br>
  So, if I trace into the call with my debugger, I'm finding that it isn't going into my value() method at all. Instead it's going into a different (unrelated) method. This makes me think my vtable is corrupted. I think the behavior I'm seeing must be a side effect of some other problem. </p>
  
  <hr>
  
  <p><b>Solved!</b> (thanks to Vlad)<br>
  It turns out I was violating the one definition rule (ODR) although it wasn't evident from the code I posted. <a href="http://blogs.msdn.com/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx" rel="nofollow noreferrer">This</a> is a great article from the Visual C++ guys that explains the problem and one way to track it down. The <i>/d1reportSingleClassLayout</i> compiler flag is a fantastic learning tool.</p>
  
  <p>When I dumped out the my class layout for MyBase and MyDerived in the two different projects, I found differences between the calling code and the library code. It turns out I had some <i>#ifdef</i> blocks in my header files and the corresponding <i>#define</i> statement was in the precompiled header for the main project but not in the subproject (the library). Have I mentioned how evil I think preprocessor macros are?</p>
  
  <p>Anyway, I'm only posting this stuff because it might be helpful to somebody else. <a href="https://stackoverflow.com/questions/302446/how-can-one-inspect-a-vtable-in-visual-c">This question</a> was also very helpful to me.</p>