title: CMake and Static Linking
body: <p>I'm using CMake in a project, and I'm trying to statically link some libraries.
  I've set:</p>
  
  <pre><code>set(BUILD_SHARED_LIBS OFF)
  set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
  set_target_properties(icarus PROPERTIES LINK_SEARCH_END_STATIC 1)
  </code></pre>
  
  <p>And I've made sure when looking for the actual libraries that I have the *.a version of them.</p>
  
  <p>Currently the project imports:</p>
  
  <pre><code>libPocoNet.a 
  libPocoUtil.a 
  libPocoXML.a 
  libPocoFoundation.a 
  libmysqlclient.a 
  libmysqlpp.a 
  libcrypto++.a 
  CUDA
  </code></pre>
  
  <p>All libraries are found, and when doing a dynamic/shared linkage, they work fine.
  I have also tried to set compilation flags:</p>
  
  <pre><code>set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} "-static-libgcc -static-libstdc++ -static")
  </code></pre>
  
  <p>But to no avail.
  While I get no issues while compiling, linkage is throwing alot of <strong>undefined reference</strong> errors for calls found in the above libraries, i.e:</p>
  
  <pre><code>undefined reference to `mysql_thread_init'
  undefined reference to `mysql_real_query'
  undefined reference to `pthread_mutex_unlock'
  undefined reference to `Poco::ErrorHandler::handle()'
  </code></pre>
  
  <p>Not in that particular order, and numerous errors for each library.</p>
  
  <p>Looking at the last line of GCC I see:</p>
  
  <pre><code>/usr/bin/c++   -g -g  -static-libgcc -static-libstdc++ -static [list of *.cpp files]
  -o icarus -rdynamic /usr/local/lib/libPocoFoundation.a /usr/local/lib/libPocoNet.a
  /usr/local/lib/libPocoUtil.a /usr/local/lib/libPocoXML.a 
  -Wl,-Bstatic -lmysqlclient -lmysqlpp -lcrypto++
  </code></pre>
  
  <p>Which makes me wonder:</p>
  
  <ol>
  <li>Why are Poco libraries linked as -rdynamic, and there is no -Wl -Bstatic flag? As if they are skipped/excluded from static linkage.</li>
  <li>mysqlclient, mysqlpp and crypto++ seem to be set for static linkage, yet I still get errors</li>
  </ol>
  
  <p>So, could someone please explain to me:</p>
  
  <ol>
  <li>How do I setup for partial static linkage using CMake</li>
  <li>Is CMAKE_EXE_LINKER_FLAGS the only one I need to set?</li>
  <li>Should I be forcing static linking for mentioned libraries but not for entire project?</li>
  </ol>
  
  <p>Please excuse me if those are too many or too localized questions, I haven't tried this before, and I can't seem to find much info on the net.</p>