title: How to install shared library in CMAKE to custom path
body: <p>I am working on a machine with no access to <code>/usr</code>, but rather a completely different path, so I cannot access <code>/usr/lib</code>, <code>/usr/bin</code> or <code>/usr/include</code>.</p>
  <p>So I have simple directory structure under some <code>parent_dir</code>:</p>
  <pre><code>--mylib
      --file1.cpp
      --file1.h
  --src
      --main.cpp
  --install
  </code></pre>
  <p>Install dir is where I wish to keep binaries, so it should have bin, lib and include dir after installation.
  Parent CMakeFiles.txt is:</p>
  <pre><code>cmake_minimum_required(VERSION 3.13)
  project(MyProject VERSION 2.0)
  set(CMAKE_CXX_STANDARD 17)
  
  set(CMAKE_PREFIX_PATH &quot;${CMAKE_BINARY_DIR}&quot; &quot;${CMAKE_PREFIX_PATH}&quot;)
  
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY &quot;${CMAKE_SOURCE_DIR}/install/lib&quot;)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY &quot;${CMAKE_SOURCE_DIR}/install/lib&quot;)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY &quot;${CMAKE_SOURCE_DIR}/install/bin&quot;)
  set(CMAKE_INCLUDE_OUTPUT_DIRECTORY &quot;${CMAKE_SOURCE_DIR}/install/include&quot;)
  
  add_subdirectory(mylib)
  add_subdirectory(src)
  </code></pre>
  <p>mylib/CMakeText.txt looks like this</p>
  <pre><code>set(SOURCE_FILE     file1.cpp) 
  set(SOURCE_HEADERS  file1.h)
  
  add_library(mylib SHARED ${SOURCE_FILES} ${SOURCE_HEADERS})
  
  target_include_directories(mylib 
                            PUBLIC $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}&gt;
                            PUBLIC $&lt;INSTALL_INTERFACE:${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
  
  set_target_properties(mylib PROPERTIES PUBLIC_HEADER &quot;${SOURCE_HEADERS}&quot;)
  
  install(TARGETS mylib EXPORT qlcsim_lib
          LIBRARY DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
          PUBLIC_HEADER DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
  </code></pre>
  <p>In src/CMakeFiles.txt I have:</p>
  <pre><code>add_executable(main main.cpp)
  target_link_libraries(main mylib)
  </code></pre>
  <p>which works fine but is incorrect.
  What it should be:</p>
  <pre><code>set(CMAKE_PREFIX_PATH &quot;parent_dir/install/&quot;)
  find_library(MY_LIB mylib)
  
  add_executable(main main.cpp)
  target_link_libraries(main ${MY_LIB})
  </code></pre>
  <p>The reason why CMakeFile.txt builds my executable properly the first time is because I am not linking to library in /install/lib (even though it's created there with no issues), but rather building the entire library into main.cpp, I think, I might be horribly wrong, this is my first cmake use.</p>
  <p>Find library on the other hand, seems correct. However it fails to locate the library if I hint it as <code>find_library(my_lib HINT &quot;parent_dir/install/lib&quot;)</code> and I had to set <code>CMAKE_PREFIX_PATH</code> first for <code>find_library</code> to start working. However even though a library is found, main.cpp completely fails saying: <code>&quot;catastrophic error: cannot open source file &quot;file1.h&quot;</code>. Why?</p>
  <p>I tried <code>include_directories(parent_dir/mylib)</code> (absolute path), tried <code>target_link_directories</code>. Nothing works. Headers of library mylib are installed in <code>install/include</code>, and I tried linking <code>main</code> target to there as well.</p>
  <p>Note that mylib has about 40 .h and .cpp files and I need it as shared library by several other projects. So solution to copy paste the entire library all over again is not good.</p>
  <p>I found out somewhere that I should be including class headers as <code>include &lt;file1.h&gt;</code> rather than <code>include &quot;file1.h&quot;</code> if library is properly installed in the system. Is that the issue? I really don't wish to dive into 80+ files fixing includes. I tried changing includes in executables which suppose to use the library (main.cpp) and issue is still there.</p>