title: CMake: add packages to rpath
body: <p>I have a dynamic library <code>libhelloworld.so</code> built with CMake that I've installed in <code>/usr/local/lib64</code>. I'm linking against the library in a CMakeLists.txt file that builds an executable:</p>
  
  <pre class="lang-cmake prettyprint-override"><code>find_package(HelloWorld REQUIRED)
  target_link_libraries(helloexecutable HelloWorld::HelloWorld)
  </code></pre>
  
  <p>CMake builds the executable just fine, but when I run it I get the following error:</p>
  
  <pre class="lang-sh prettyprint-override"><code>error while loading shared libraries: libhelloworld.so: cannot open shared object file: No such file or directory
  </code></pre>
  
  <p>I was able to resolve the issue by setting the rpath</p>
  
  <pre class="lang-cmake prettyprint-override"><code>set(CMAKE_INSTALL_RPATH "/usr/local/lib64")
  </code></pre>
  
  <p>but this is not portable.</p>
  
  <p>So I have two questions: 1) shouldn't CMake automatically set the rpath for linked dynamic libraries? and 2) if Cmake can automatically set the rpath how do I tell it to do so, and if it can't, what's a more portable way of setting the rpath?</p>