<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div>Hello,<br></div><div><br></div><div>How to ask cmake to add a library path (coming from pc file) to rpath ?<br></div><div><br></div><div>I checked this <a href="https://cmake.org/Wiki/CMake_RPATH_handling,">https://cmake.org/Wiki/CMake_RPATH_handling,</a> but still not working. Can somebody help ?<br></div><div><br></div><div>>> more main.cpp <br>#include <petsc.h><br><br>int main(int argc, char ** argv) {<br>  PetscInitialize(&argc, &argv, NULL, "");<br>  PetscFinalize();<br>  return 0;<br>}</div><div><br>>> more CMakeLists.txt <br>cmake_minimum_required(VERSION 3.7)<br>enable_language(CXX)<br><br>find_package(MPI REQUIRED)<br>find_package(PkgConfig REQUIRED) # Get pkg_check_modules.<br>pkg_check_modules(PETSc REQUIRED PETSc)<br><br>project(main)<br>add_executable(main main.cpp)<br><br>target_include_directories(main PUBLIC ${MPI_CXX_INCLUDE_PATH})<br>target_link_libraries(main PUBLIC ${MPI_CXX_LIBRARIES})<br><br>target_include_directories(main PUBLIC ${PETSc_INCLUDE_DIRS})<br>foreach(lib ${PETSc_LDFLAGS})<br>  target_link_libraries(main PUBLIC ${lib})<br>endforeach(lib)<br><br>foreach(dir ${PETSc_LIBRARY_DIRS})<br>  link_directories(main PUBLIC ${dir}) # Not sure: is this needed ?<br>endforeach(dir)<br># use, i.e. don't skip the full RPATH for the build tree<br>SET(CMAKE_SKIP_BUILD_RPATH  FALSE)<br># when building, don't use the install RPATH already<br># (but later on when installing)<br>SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)<br>SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")<br># add the automatically determined parts of the RPATH<br># which point to directories outside the build tree to the install RPATH<br>SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)<br># the RPATH to be used when installing, but only if it's not a system directory<br>LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)<br>IF("${isSystemDir}" STREQUAL "-1")<br>   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")<br>ENDIF("${isSystemDir}" STREQUAL "-1")<br><br>include(CTest)<br>enable_testing()<br>add_test(NAME main COMMAND "mpirun -n 2 ./main" WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")<br><br>Now,<br></div><div><br></div><div>>> export PKG_CONFIG_PATH="/path/to/petsc/local/lib/pkgconfig"</div><div>>> cmake ..; make => OK<br></div><div><br></div><div>But<br></div><div><br></div><div>>> ldd main <br>    linux-vdso.so.1 (0x00007ffcf23ca000)<br>    libmpi_cxx.so.20 => /usr/lib/x86_64-linux-gnu/libmpi_cxx.so.20 (0x00007f68593ea000)<br>    libmpi.so.20 => /usr/lib/x86_64-linux-gnu/libmpi.so.20 (0x00007f68590f4000)<br>    libpetsc.so.3.8 => not found<br><br></div><div>and so, make test fails ?!...<br></div><div><br></div></div></body></html>