[CMake] RPATH for pkg-config

Alexander Neundorf a.neundorf-work at gmx.net
Thu Jan 4 16:04:02 EST 2018


On 2018 M01 4, Thu 10:06:26 CET Franck Houssen wrote:
...
> ...
> 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

yes, so cmake doesn't know that it will link to /path/to/petsc/local/lib/
libpetsc.so .
I usually recommend to use the results from pkgconfig as input to find_library() 
etc. calls, ie. put the directory reported by pkgconfig in the HINTS section of 
find_library(). This should then return the full path to the library, which you 
can then use in target_link_libraries(), and cmake will take care of the 
rpath.

...
> >> 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


you can also use readelf or objdump to see the RPATH entry in the ELF file 
directly.

Alex



More information about the CMake mailing list