[CMake] RPATH/RUNPATH

Alexander Neundorf a.neundorf-work at gmx.net
Tue Aug 23 12:54:02 EDT 2011


On Friday 19 August 2011, Michael Hertling wrote:
> On 08/17/2011 10:03 PM, Knox, Kent wrote:
> > Yes, that's right.  I'm currently using cmake with RPATH, but from what
> > I've been reading the RUNPATH header is now the more preferred approach.
> >  If the user does not set LD_LIBRARY_PATH, the RUNPATH header should
> > take effect, but if the user DOES wish to overload your libraries, they
> > can go ahead and set LD_LIBRARY_PATH themselves assuming that they know
> > what they are doing.
> > 
> > This would help in our testing, as my RPATH settings have screwed up a
> > few of my testers as they thought they were shuffling libraries around,
> > but in fact my RPATH silently made their efforts for naught.
> > 
> > Thank you for the suggestion on modifying the linker flags myself; that
> > sounds feasible.  I had also thought of running the 'chrpath'
> > <http://linux.die.net/man/1/chrpath> utility as a post-processing step
> > on my executables to manually change RPATH's into RUNPATH's, but I'm not
> > quite sure how to write this in CMake yet.  Wanted to check first if
> > CMake had undocumented or planned settings first.
> > 
> > Kent
> 
> In order to enable ELF's DT_RUNPATH tag, you just need to set the
> linker's --enable-new-dtags option, e.g. as in the following
> exemplary project, provided you use GNU binutils' ld:
> 
> CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
> PROJECT(RUNPATH C)
> SET(CMAKE_VERBOSE_MAKEFILE ON)
> SET(CMAKE_INSTALL_RPATH $ENV{HOME}/lib)
> FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
> ADD_EXECUTABLE(main main.c)
> INSTALL(TARGETS main RUNTIME DESTINATION bin)
> SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags")
> 
> In this way, the DT_RPATH tag in the final binary is duplicated for
> DT_RUNPATH, and as the former is ignored when the latter is present,
> it should suit your needs. Furthermore, CMake's rpath machinery can
> operate as usual and the built-in ELF editor addresses the DT_RPATH
> and DT_RUNPATH tags, so there is no necessity to have CMake handle
> the DT_RUNPATH explicitly, in particular as ld does not provide an
> immediate option to set this tag, AFAIK.

Can you please add that to the RPATH cmake wiki page ?

Thanks
Alex


More information about the CMake mailing list