How to add rpath to the executable in cmake when building (not installing) on ​​OSX?

So, if you already have the binary, you can add the path to OSX to it using "install_name_tool", since:

install_name_tool -add_rpath @executable_path/blah 

xcode does this automatically when you create an application package.

I know that in cmake you can use this to set the page_name of the shared library:

 set_target_properties(nshared PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "@rpath") 

My question is: what is the equivalent of this for adding rpath to binary?

(for “why do you do this?” among you, look at otool -l on any of the applications in your Applications / folder, and you will see many applications with entries such as:

 Load command 15 cmd LC_RPATH cmdsize 36 path @executable_path/../../Frameworks/ 

This is standard practice. I'm just trying to do it in cmake)

+8
cmake macos
source share
1 answer

You can use the POST_BUILD command to add the LC_RPATH boot LC_RPATH to the embedded executable:

 add_custom_command(TARGET executable POST_BUILD COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath "@executable_path/../../Frameworks/" $<TARGET_FILE:executable>) 

The variable CMAKE_INSTALL_NAME_TOOL contains the path to the install_name_tool executable file under Darwin.

+10
source share

All Articles