[CMake] RPATH for pkg-config

Franck Houssen franck.houssen at inria.fr
Thu Jan 4 04:06:26 EST 2018


----- Mail original -----
> De: "Alexander Neundorf" <a.neundorf-work at gmx.net>
> À: cmake at cmake.org
> Envoyé: Mercredi 3 Janvier 2018 21:35:02
> Objet: Re: [CMake] RPATH for pkg-config
> 
> On 2018 M01 3, Wed 10:08:09 CET Franck Houssen wrote:
> > Hello,
> > 
> > How to ask cmake to add a library path (coming from pc file) to rpath ?
> > 
> > I checked this https://cmake.org/Wiki/CMake_RPATH_handling, but still not
> > working. Can somebody help ?
> > >> more main.cpp
> > 
> > #include <petsc.h>
> > 
> > int main(int argc, char ** argv) {
> > PetscInitialize(&argc, &argv, NULL, "");
> > PetscFinalize();
> > return 0;
> > }
> > 
> > >> more CMakeLists.txt
> > 
> > cmake_minimum_required(VERSION 3.7)
> > enable_language(CXX)
> > 
> > find_package(MPI REQUIRED)
> > find_package(PkgConfig REQUIRED) # Get pkg_check_modules.
> > pkg_check_modules(PETSc REQUIRED PETSc)
> > 
> > project(main)
> > add_executable(main main.cpp)
> > 
> > target_include_directories(main PUBLIC ${MPI_CXX_INCLUDE_PATH})
> > target_link_libraries(main PUBLIC ${MPI_CXX_LIBRARIES})
> > 
> > target_include_directories(main PUBLIC ${PETSc_INCLUDE_DIRS})
> > foreach(lib ${PETSc_LDFLAGS})
> > target_link_libraries(main PUBLIC ${lib})
> > endforeach(lib)
> 
> How does each ${lib} look like ?
> Is it "-lpetsc" or does it have the full path to the libraries ?

I added 2 message: one for target_link_libraries, one for link_directories.

>> more CMakeLists.txt
...
target_include_directories(main PUBLIC ${PETSc_INCLUDE_DIRS})
foreach(lib ${PETSc_LDFLAGS})
  target_link_libraries(main PUBLIC ${lib})
  message("target_link_libraries - lib is ${lib}")
endforeach(lib)

foreach(dir ${PETSc_LIBRARY_DIRS})
  link_directories(main PUBLIC ${dir}) # Not sure: is this needed ?
  message("link_directories - dir is ${dir}")
endforeach(dir)

This gives:

>> cmake ..; make
...
target_link_libraries - lib is -L/path/to/petsc/local/lib
target_link_libraries - lib is -lpetsc
link_directories - dir is /path/to/petsc/local/lib

> You should use the full path to the libraries, otherwise cmake doesn't know
> where they are and the RPATH computation will not work.

I expected the same behavior you describe ! That why I added the link_directories (with the associated comment) BEFORE setting CMAKE_SKIP_BUILD_RPATH and others.

> > 
> > foreach(dir ${PETSc_LIBRARY_DIRS})
> > link_directories(main PUBLIC ${dir}) # Not sure: is this needed ?
> > endforeach(dir)
> 
> no, link_directories() in general should not be used.
> 
> > # use, i.e. don't skip the full RPATH for the build tree
> > SET(CMAKE_SKIP_BUILD_RPATH FALSE)
> > # when building, don't use the install RPATH already
> > # (but later on when installing)
> > SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
> > SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
> 
> If the automatic computation fails, you could add the petsc lib dir here as
> INSTALL_RPATH
> 

Do you mean that ?

>> more CMakeLists.txt
...
foreach(dir ${PETSc_LIBRARY_DIRS})
  link_directories(main PUBLIC ${dir})
  message("link_directories - dir is ${dir}")
  set(CMAKE_INSTALL_RPATH "${dir}:${CMAKE_INSTALL_RPATH}")
endforeach(dir)


I've just tried but still not working:

>> cmake ..; make
...
target_link_libraries - lib is -L/home/fghoussen/Documents/INRIA/petsc/local/lib
target_link_libraries - lib is -lpetsc
link_directories - dir is /home/fghoussen/Documents/INRIA/petsc/local/lib
...
[100%] Built target main
>> ldd main
	linux-vdso.so.1 (0x00007ffebab8a000)
	libmpi_cxx.so.20 => /usr/lib/x86_64-linux-gnu/libmpi_cxx.so.20 (0x00007f36172e3000)
	libmpi.so.20 => /usr/lib/x86_64-linux-gnu/libmpi.so.20 (0x00007f3616fed000)
	libpetsc.so.3.8 => not found


> Alex
> 
> --
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
> 


More information about the CMake mailing list