title: How to link library correctly in cmake?
body: <p>I'm using yaml-cpp library recently. I follow the config file in &quot;yaml-cpp-config.cmake.in&quot; which content is</p>
  <pre><code>  # - Config file for the yaml-cpp package
    # It defines the following variables
    #  YAML_CPP_INCLUDE_DIR - include directory
    #  YAML_CPP_LIBRARIES    - libraries to link against
     
    # Compute paths
    get_filename_component(YAML_CPP_CMAKE_DIR &quot;${CMAKE_CURRENT_LIST_FILE}&quot; PATH)
    set(YAML_CPP_INCLUDE_DIR &quot;@CONFIG_INCLUDE_DIRS@&quot;)
     
   # Our library dependencies (contains definitions for IMPORTED targets)
   include(&quot;${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake&quot;)
   
   # These are IMPORTED targets created by yaml-cpp-targets.cmake
   set(YAML_CPP_LIBRARIES &quot;@EXPORT_TARGETS@&quot;)
                                                                                                      
                   
  </code></pre>
  <p>According line 8 and line 14 (or the comment section), I edited my CMakeLists.txt and test.cpp like</p>
  <pre><code>  cmake_minimum_required(VERSION 3.5)
    project(yaml_test)
    find_package(yaml-cpp)
    include_directories(${YAML_CPP_INCLUDE_DIRS})
     
    add_executable(yaml_test src/test.cpp)
    target_link_libraries(yaml_test ${YAML_CPP_LIBRARIES})
                                                                                                  
                                                                                                                               
  
     #include &lt;yaml-cpp/yaml.h&gt;
     
     int main()
     { 
       YAML::Node node = YAML::LoadFile(&quot;test.yaml&quot;);
     
       
       return 0;
     }
  </code></pre>
  <p>and I got</p>
  <pre><code>CMakeFiles/yaml_test.dir/src/test.cpp.o: In function `main':
  test.cpp:(.text+0xf9): undefined reference to `YAML::LoadFile(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
  collect2: error: ld returned 1 exit status
  CMakeFiles/yaml_test.dir/build.make:94: recipe for target 'yaml_test' failed
  make[2]: *** [yaml_test] Error 1
  CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/yaml_test.dir/all' failed
  make[1]: *** [CMakeFiles/yaml_test.dir/all] Error 2
  Makefile:83: recipe for target 'all' failed
  make: *** [all] Error 2
  </code></pre>
  <p>I found a solution from <a href="https://stackoverflow.com/questions/22139149/undefined-reference-to-yamlloadfile/22139347#22139347">undefined reference to YAML::LoadFile</a></p>
  <p>changed my CMakeLists.txt to</p>
  <pre><code>cmake_minimum_required(VERSION 3.5)
    project(yaml_test)
    find_package(yaml-cpp)
    include_directories(${YAMLCPP_INCLUDE_DIRS})
     
    add_executable(yaml_test src/test.cpp)
    target_link_libraries(yaml_test ${YAMLCPP_LIBRARIES})
  </code></pre>
  <p>And I can build my code succesfully.</p>
  <p>My question is how can I tell the right &quot;include_directories&quot; and &quot;target_link_libraries&quot; straight from yaml-cpp package's CMakeLists.txt or config file?</p>
  <p>Can someone help me? Thanks!</p>