Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple RPATH directories using CMake on MacOS

Tags:

cmake

rpath

How do we set multiple RPATH directories on a target in CMake on MacOS? On Linux, we can just use a colon-separated list:

set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "\$ORIGIN/../lib:\$ORIGIN/../thirdparty/lib"
)

On MacOS, we can technically add a colon separated list and otool -l should show it, but these directories aren't searched:

set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "@loader_path/../lib:@loader_path/../thirdparty/lib"
)

Normally, if I were going to have multiple RPATH directories on MacOS, I'd send multiple linker flags, and these flags would show up separately with otool -l. Something like:

g++-mp-4.7 mytarget.cpp -o mytarget -Wl,-rpath,@loader_path/../lib,-rpath,@loader_path/../thirdparty/lib

Which gives:

Load command 15
          cmd LC_RPATH
      cmdsize 32
         path @loader_path/../lib (offset 12)
Load command 16
          cmd LC_RPATH
      cmdsize 48
         path @loader_path/../thirdparty/lib (offset 12)

How do I recreate this behavior with CMake?

like image 758
wyer33 Avatar asked Oct 20 '16 05:10

wyer33


1 Answers

According to the documentation, the paths should not be separated with colons, but with semicolons:

set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "@loader_path/../lib;@loader_path/../thirdparty/lib"
)

Or, using the command set to let CMake deals with the separator:

set(MY_INSTALL_RPATH
    "@loader_path/../lib"
    "@loader_path/../thirdparty/lib"
)
set_target_properties(mytarget
    PROPERTIES
    INSTALL_RPATH "${MY_INSTALL_RPATH}"
)

EDIT: (thanks Tsyvarev for the comment)

Or, using the command set_property, which accepts multivalue properties:

set_property(
    TARGET mytarget
    PROPERTY INSTALL_RPATH
    "@loader_path/../lib"
    "@loader_path/../thirdparty/lib"
)
like image 89
rgmt Avatar answered Oct 11 '22 02:10

rgmt