title: Unsdefined reference to library class members errors from caller program
body: <p><strong>Additional Questions added below, 4/11/2011</strong></p>
  
  <p>I am developing a cross-platform set of shared libraries DLLs/Sos and tester programs in C++ though I have to be able to support C.  The libraries will ship as object code only, but the tester program(s) will ship with source so our customers can have example code.  For this reason I am designing the libraries to be loaded at runtime, i.e. dynamic linking using dlopen()/LoadLibraryA().</p>
  
  <p>I am using g++ 4.4.3-4 on Umbutu 10.04 and VC++ 2008 on Vista/64 (in 32 bit mode).</p>
  
  <p>Everything seems to work just fine on Windows (right now).  However, when I compile on Linux I am getting some errors I can't figure out.</p>
  
  <p>The tester and the library have several classes coded in several .cpp's and .h's.  The classes and most everything in the library except the main entry points are in a namespace DISCOVER_NS.</p>
  
  <p>Here is a brief sketch of the project:</p>
  
  <p>First, an admission, I've shortened a bunch of names so the code is more readable.</p>
  
  <p><strong>discover.cpp</strong></p>
  
  <ul>
  <li><p>Creates a class object with a pointer to it called theMainObject of type DiscoverObject.</p></li>
  <li><p>Has an extern "C" function that returns theMainObject to the caller program as void*.</p></li>
  <li><p>DiscoverObject has several methods and instantiates other classes found in separate cpp's and .h's.  One particular method is named Hello(), which does what you'd expect, it prints a "hello" test message..</p></li>
  </ul>
  
  <hr>
  
  <p><strong>tester.cpp</strong></p>
  
  <ul>
  <li><p>Gets a handle to the library</p></li>
  <li><p>gets the function pointer to the function that returns theMainObject.</p></li>
  <li><p>Executes the function (pointer) and casts the returned address from void* to DISCOVER_NS::DiscoverObject* aDiscoverObject. </p></li>
  <li><p>Run aDiscoverObject->Hello().</p></li>
  </ul>
  
  <hr>
  
  <p>I compile with:</p>
  
  <p>CC = @g++</p>
  
  <p>gflags = -g3</p>
  
  <p>cflags = -fPIC -Wall -pedantic</p>
  
  <p>lib_linkflags := -shared -fPIC -lstdc++ -lrt -lpthread -rdynamic</p>
  
  <p>tester_linkflags := -ldl  -lpthread</p>
  
  <p>defines = -D_<em>linux</em>_   -D_DEBUG   -D_IPC_ARCH_INTEL=1   -D_THREAD_SAFE</p>
  
  <hr>
  
  <p>Now, when I compile I get these errors:
  *Tester.cpp:142: undefined reference to `Discover_NS::DiscoverObject::hello()'*</p>
  
  <p>I also get a bunch of other undefined reference errors from discover.so saying, for example:
  *discover.so: undefined reference to `Discover_NS::DeviceList::~DeviceList()*</p>
  
  <hr>
  
  <p>I have tried making virtually everything in the SO extern "C".  No difference.</p>
  
  <p>I tried putting statements into discover.cpp that look like:
         extern void Discover_NS::OtherClass::method( args ); 
  but that gives me errors about" declaration outside of class is not deffinition" errors.</p>
  
  <hr>
  
  <p>I know it will help to see code, but I need time to whack out something small for posting.</p>
  
  <p>Can anyone offer ideas to solve this mess?</p>
  
  <p>Thanks,</p>
  
  <p>Wes</p>
  
  <p>Dmitry's solution was not quite all of the fix, but was a necessary element in the solution.  Upon examination of my makefile I found a couple of unintentionally duplicated lines, which I removed, and two "typos" where I had the wrong path for -o's  coded into compile steps.  The broken steps compiled logger.cpp and RemException.cpp:</p>
  
  <pre><code>./common/logger.o : ./common/logger.cpp
      $(CC) $(gflags)  $(cflags) -c  $(defines)  -I ./common  
          -I ./EdgeIO  -I ./Discover   
          -o ./common/Debug/logger.o   &lt;+++++++++ path to .o was wrong
          ./common/logger.cpp   2&gt;&amp;1  | tee ./RemKonTester/logger.ERR
  </code></pre>
  
  <p>Then I found the real bug.  I completely missed the fact that I wasn't compiling all of my .cpp's in the Discover directory!.  It took a good hour to get all the nit-pics removed but now she comiles from a makefile just fine.</p>
  
  <p>NEW VERSION OF THE ORIGINAL QUESTION:  NOw that I know it WILL work via makefile, how do I talk Eclipse into doing the same thing the makefile is doing?</p>
  
  <p>Thanks Dmitry.</p>
  
  <p>Wes</p>
  
  <h2><strong>Well, my problem is still here.</strong></h2>
  
  <p>I have my code compiling with Dmitry's (@Dmitry) suggestions in place.  Only, they appear to be causing a separate problem.  I want my libraries to link to he main test program dynamically, at run time.  Adding the <em>-l Discover -l EdgeIO</em> to the link gets everything to compile, but it give me static linking.</p>
  
  <p><em>FYI, The unused "pi"s are so the SO has a floating point number in it and will thus compile with floating point support.  Required if caller ever wants to use floating point numbers.  Anybody got a better way to force g++ to comkpile with floating point included?</em></p>
  
  <p>After fixing the many bugs Dmitry helped me find, I now get this output:</p>
  
  <pre><code>make
  ./Discover/dllmain.cpp: In function ‘void InitalizeLibraryServices()’:
  ./Discover/dllmain.cpp:175: warning: unused variable ‘pi’
  
  
  ./EdgeIO/dllMain.cpp: In function ‘void InitalizeLibraryServices()’:
  ./EdgeIO/dllMain.cpp:158: warning: unused variable ‘pi’
  
  
  linking RemKonTester
      gflags = -g3
      tstlinkflags = -ldl  -lpthread 
      defines =  -D__linux__   -D_DEBUG   -D_IPC_ARCH_INTEL=1   -D_THREAD_SAFE
  
  ./RemKonTester/Debug/RemKonTester.o: In function `main':
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:81: undefined 
      reference to `RemKon_EdgeIO::EdgeIoObject::hello()'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:111: undefined 
      reference to `RemKon_Discover::DiscoverObject::hello()'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:116: undefined 
      reference to `RemKon_Discover::DiscoverObject::SetLogLevel(unsigned int)'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:117: undefined 
      reference to `RemKon_Discover::DiscoverObject::hello()'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:118: undefined 
      reference to `RemKon_Discover::DiscoverObject::LocalIpAddress(int)'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:122: undefined 
      reference to `RemKon_Discover::DiscoverObject::RegisterCallback(bool(*)
      (void*), void*)'
  /home/wmiller/Projects/Eclipse/./RemKonTester/RemKonTester.cpp:123: undefined 
      reference to `RemKon_Discover::DiscoverObject::Search()'
  
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I get the same set of error messages from Eclipse.</p>
  
  <p>RemKonTester.cpp includes all the .h's where these items are declared.  I have tried them with the declarations extern "C" and not.</p>
  
  <p>Hoping for help,</p>
  
  <p>Wes</p>