title: Cmake install nested static library target_link_library undefined reference
body: <p>Install nested static library, and target_link_library not working</p>
  <p>File structure:</p>
  <pre><code>HelloLib
      WorldLib
          CMakeLists.txt
          WorldLib.cpp
          WorldLib.h
  
      CMakeLists.txt
      HelloLib.cpp
      HelloLib.h
  
  CMakeLists.txt
  main.cpp
  </code></pre>
  <p>main.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &quot;HelloLib/HelloLib.h&quot;
  int main() {
    std::cout &lt;&lt; hello() &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  <p>CMakeLists.txt (root)</p>
  <pre class="lang-cpp prettyprint-override"><code>cmake_minimum_required(VERSION 3.16.3)
  project(HelloWorld)
  
  add_executable(${PROJECT_NAME} main.cpp)
  
  add_subdirectory(HelloLib)
  target_link_libraries(
      ${PROJECT_NAME}
      HelloLib
  )
  </code></pre>
  <p>HelloLib.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;string&gt;
  #include &quot;WorldLib/WorldLib.h&quot;
  std::string hello() { return &quot;Hello &quot; + world(); }
  </code></pre>
  <p>HelloLib.h</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  #include &lt;string&gt;
  std::string hello();
  </code></pre>
  <p>CMakeLists.txt (HelloLib)</p>
  <pre class="lang-cpp prettyprint-override"><code>add_library(HelloLib HelloLib.cpp)
  
  add_subdirectory(WorldLib)
  target_link_libraries(
      HelloLib
      WorldLib
  )
  </code></pre>
  <p>WorldLib.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;string&gt;
  std::string world() { return &quot;World!&quot;; }
  </code></pre>
  <p>WorldLib.h</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  #include &lt;string&gt;
  std::string world();
  </code></pre>
  <p>CMakeLists.txt (WorldLib)</p>
  <pre class="lang-cpp prettyprint-override"><code>add_library(WorldLib WorldLib.cpp)
  </code></pre>
  <p>Build and run, successfully print out <code>Hello World!</code></p>
  <p>Now I would like to make HelloLib as static library</p>
  <p>Change CMakeLists.txt (HelloLib), install to <code>/usr/lib</code> and <code>/usr/include</code></p>
  <pre class="lang-cpp prettyprint-override"><code>add_library(HelloLib HelloLib.cpp)
  
  add_subdirectory(WorldLib)
  target_link_libraries(
      HelloLib
      WorldLib
  )
  
  install(
      TARGETS HelloLib
      ARCHIVE DESTINATION lib
  )
  install(
      DIRECTORY &quot;${CMAKE_SOURCE_DIR}/&quot; # source directory
      DESTINATION &quot;include&quot; # target directory
      FILES_MATCHING # install only matched files
      PATTERN &quot;*.h&quot; # select header files
  )
  </code></pre>
  <p>Run (<code>make install</code>) (CMAKE_INSTALL_PREFIX=<code>/usr</code>)</p>
  <pre class="lang-bash prettyprint-override"><code>mkdir -p out/build
  cmake -S src -B out/build -DCMAKE_INSTALL_PREFIX=/usr
  cd out/build
  make
  make install
  </code></pre>
  <pre class="lang-cpp prettyprint-override"><code>[ 33%] Built target WorldLib
  [ 66%] Built target HelloLib
  [100%] Built target HelloWorld
  Install the project...
  -- Install configuration: &quot;&quot;
  -- Installing: /usr/lib/libHelloLib.a
  -- Up-to-date: /usr/include
  -- Installing: /usr/include/HelloLib
  -- Installing: /usr/include/HelloLib/HelloLib.h
  -- Installing: /usr/include/HelloLib/WorldLib
  -- Installing: /usr/include/HelloLib/WorldLib/WorldLib.h
  </code></pre>
  <p>Static library (.a) should generate to <code>/usr/lib/libHelloLib.a</code></p>
  <p>Let's test it, change CMakeLists.txt (root)</p>
  <pre class="lang-cpp prettyprint-override"><code>cmake_minimum_required(VERSION 3.16.3)
  project(HelloWorld)
  
  add_executable(${PROJECT_NAME} main.cpp)
  
  add_subdirectory(HelloLib)
  target_link_libraries(
      ${PROJECT_NAME}
  
      /usr/lib/libHelloLib.a
  )
  </code></pre>
  <p>Then build, it give undefined reference error</p>
  <pre><code>/bin/ld: /usr/lib/libHelloLib.a(HelloLib.cpp.o): in function `hello[abi:cxx11]()':
  HelloLib.cpp:(.text+0x20): undefined reference to `world[abi:cxx11]()'
  </code></pre>
  <p>What's wrong with me? This can be successful with no nested library</p>
  <p>What is the correct way to install nested static library?</p>