title: CMake executable linker error even though the libraries are linked
body: <p>I'm pretty new to CMake, but I already encountered an error which I do not understand.</p>
  
  <p>For given reasons, I have the following C++ setup: I have two own static libraries, multiple external static libraries and one executable. My own libraries <code>a</code> defines common stuff and all external libraries are linked into it. My library <code>b</code> defines more special stuff and has library <code>a</code> linked into it. Finally, <code>b</code> is linked into my executable <code>c</code>. Both <code>b</code> and <code>c</code> use functions from different external libraries, which should be visible through <code>a</code>.</p>
  
  <p>I have the following CMakeLists.txt, where I reduced the problem by using only one external library: ZeroC Ice.</p>
  
  <p><strong>Root CMakeLists.txt</strong></p>
  
  <pre><code>cmake_minimum_required (VERSION 3.3.0)
  project (myproject)
  
  # some settings
  if (CMAKE_COMPILER_IS_GNUCC)
      set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
  endif (CMAKE_COMPILER_IS_GNUCC)
  if (CMAKE_COMPILER_IS_GNUCXX)
      set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
  endif (CMAKE_COMPILER_IS_GNUCXX)
  set (CMAKE_VERBOSE_MAKEFILE ON)
  set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
  set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  include_directories (${CMAKE_CURRENT_SOURCE_DIR})
  
  # ZeroC Ice
  find_package (Ice 3.6.0 REQUIRED COMPONENTS Ice IceUtil)
  include_directories (${Ice_INCLUDE_DIRS})
  
  # libraries and executable
  add_subdirectory (a)
  add_subdirectory (b)
  add_subdirectory (c)
  </code></pre>
  
  <p><strong>CMakeLists.txt for <code>a</code></strong></p>
  
  <pre><code>set (a_FILES
      stuff.cpp
  )
  add_library (a STATIC ${a_FILES})
  
  # link to third-party libraries
  target_link_libraries (a PUBLIC
      ${Ice_LIBRARIES}
  )
  </code></pre>
  
  <p><strong>CMakeLists.txt for <code>b</code></strong></p>
  
  <pre><code>set (b_FILES
      more_stuff.cpp
  )
  add_library (b STATIC ${b_FILES})
  
  # link to a
  target_link_libraries (b PUBLIC
      a
  )
  </code></pre>
  
  <p><strong>CMakeLists.txt for <code>c</code></strong></p>
  
  <pre><code>set (c_FILES
      main.cpp
  )
  add_executable (c ${c_FILES})
  
  # link to b
  target_link_libraries (c PUBLIC
      b
  )
  </code></pre>
  
  <p>In this example, <code>stuff.cpp</code>, <code>more_stuff.cpp</code> and <code>main.cpp</code> use classes from the Ice library. <code>a</code> and strangely even <code>b</code> compile and link without any problem. Only <code>c</code> throws a lot of similar linker errors:</p>
  
  <blockquote>
    <p>C:/PROGRA~2/ZeroC/ICE-36~1.0/include/Ice/FactoryTableInit.h:27: undefined reference to `_imp___ZN11IceInternal16FactoryTableInitD1Ev'</p>
  </blockquote>
  
  <p>For me, the CMakeLists.txt setup looks just fine and googleing for days did not change anything. I would appreciate any help!</p>
  
  <p>I'm working on Windows 10 with CMake 3.3.1 and CMake Generator "Eclipse CDT4 - MinGW Makefiles" (g++ version 4.8.1).</p>
  
  <p>Thanks in advance! :)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The g++ linker output for <code>c</code> shows that it tries to link against <code>liba.a libb.a C:/.../ice.lib C:/.../iceutil.lib</code>, which seems just fine to me. Anyways, the <code>undefined reference</code> linker errors occur in header files whose classes are implemented inside of <code>ice.lib</code>, so I don't know what is going wrong here...</p>