title: Why "undefined reference" when the variable is defined and the library is linked?
body: <p>We have a few applications which link to several of our own libraries. I recently added a new application which links to these libraries, and this compiles successfully on Windows.</p>
  
  <p>However, on Linux, I see an "undefined reference" error to a static variable, which of course means that the variable wasn't defined. The error occurs in a library, but only when linked to the new application (the existing applications still link fine).</p>
  
  <p>As you can see, the compiler says that <code>kAppVersion</code> is undefined. When actually it is defined in <a href="http://code.google.com/p/synergy-plus/source/browse/branches/1.4/src/lib/common/Version.cpp" rel="nofollow noreferrer">Version.cpp</a>, and common is linked (specified in <a href="http://code.google.com/p/synergy-plus/source/browse/branches/1.4/src/cmd/synergyd/CMakeLists.txt" rel="nofollow noreferrer">CMakeLists.txt</a>). We thought that it may be something to do with <code>target_link_libraries</code> ordering, but moving <code>common</code> to before <code>base</code> (where the error occurs) has no effect it seems. Besides, in other <a href="http://code.google.com/p/synergy-plus/source/browse/branches/1.4/src/cmd/synergys/CMakeLists.txt" rel="nofollow noreferrer">CMakeLists.txt</a> (e.g. for synergys) the ordering appears to be irrelevant, as this application compiles successfully. This similar question appears to hint that ordering matters, but I haven't had any success with this:</p>
  
  <p>Qustion: <a href="https://stackoverflow.com/questions/7783345/library-is-linked-but-reference-is-undefined">library is linked but reference is undefined</a></p>
  
  <p>My only idea is that it could be something wrong in <a href="http://code.google.com/p/synergy-plus/source/browse/branches/1.4/src/cmd/synergyd/synergyd.cpp" rel="nofollow noreferrer">synergyd.cpp</a> or <a href="http://code.google.com/p/synergy-plus/source/browse/branches/1.4/src/lib/synergy/CDaemonApp.cpp" rel="nofollow noreferrer">CDaemonApp.cpp</a> -- but I can't see anything obvious that compiles on Linux but not Windows.</p>
  
  <pre><code>Scanning dependencies of target synergyd
  [ 90%] Building CXX object src/cmd/synergyd/CMakeFiles/synergyd.dir/synergyd.o
  Linking CXX executable ../../../../../bin/debug/synergyd
  ../../../../../lib/debug/libarch.a(CArch.o): In function `~XExitApp':
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/arch/../synergy/XSynergy.h:114: undefined reference to `vtable for XExitApp'
  ../../../../../lib/debug/libarch.a(CArch.o): In function `CArchAppUtil::exitApp(int)':
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/arch/CArchAppUtil.h:31: undefined reference to `XExitApp::XExitApp(int)'
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/arch/CArchAppUtil.h:31: undefined reference to `typeinfo for XExitApp'
  ../../../../../lib/debug/libarch.a(CArchAppUtilUnix.o): In function `CArchAppUtilUnix::parseArg(int const&amp;, char const* const*, int&amp;)':
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/arch/CArchAppUtilUnix.cpp:32: undefined reference to `CApp::isArg(int, int, char const* const*, char const*, char const*, int)'
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/arch/CArchAppUtilUnix.cpp:37: undefined reference to `CApp::isArg(int, int, char const* const*, char const*, char const*, int)'
  ../../../../../lib/debug/libbase.a(CLog.o): In function `CLog::insert(ILogOutputter*, bool)':
  /srv/buildbot/synergy/1.4-linux32/build/src/lib/base/CLog.cpp:213: undefined reference to `kAppVersion'
  collect2: ld returned 1 exit status
  make[2]: *** [../../bin/debug/synergyd] Error 1
  make[1]: *** [src/cmd/synergyd/CMakeFiles/synergyd.dir/all] Error 2
  make: *** [all] Error 2
  </code></pre>
  
  <p>The full code can be browsed from <a href="http://code.google.com/p/synergy-plus/source/browse/#svn/branches/1.4/src" rel="nofollow noreferrer">our repository</a>.</p>