13

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?

rocambille
  • 15,398
  • 12
  • 50
  • 68
wyer33
  • 6,060
  • 4
  • 23
  • 53

1 Answers1

18

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"
)
rocambille
  • 15,398
  • 12
  • 50
  • 68