I have previously written an article <discussing the use of rpath in cmake>, as long as the specific aspect is how to remove rpath after compilation (excluding the installation of make install. Today, we will mainly introduce how to keep rpath after cmake uses the install command.
I will briefly introduce cmake's rpath mechanism. I have introduced it in my previous article. If you do not specify cmake_skip_rpath, scheme, cmake_skip_build_rapth, and cmake_skip_install_rpath, cmake will compile it for you by default, if you use the relevant dynamic library, it will add the path for generating the dynamic library in the corresponding executable, in this way, you do not need to run LD_LIBRARY_PATH every time you execute it. You can use it at this time.
readelf -d myexe
You can see that the rpath field in myexe currently has a library rpath, which specifies the target path for generating the corresponding dynamic library target. You can also
ldd -r myexe
To view which dynamic libraries have been found for the current executable. Of course, you can do a small experiment, that is, move the path of the shared_lib library. When you run myexe again, you will find that it cannot find the relevant dynamic library, because the current rpath is an absolute write path, if you just move the EXE instead of Lib, there is no problem.
Let's talk about how cmake handles rpath under make install. To facilitate installation, cmake will automatically remove and delete the related rpath after make install by default. At this time, you can check the rpath of the EXE and find that this field is no longer available. Therefore, after each make install operation, when we enter the installation path to execute the relevant EXE, we will find that the relevant library path is no longer found in the exe, because its rpath has been removed by cmake.
So, how can we enable cmake to write the relevant rpath during the install process, and this rpath cannot make the rpath at the time of initial build? The answer is the global variable cmake_install_rpath and the Target attribute install_rpath. The following is a simple example.
As we all know, cmake has the same installation path as configure in the installation process. cmake_install_prefix (-- prefix in configure, and the global variable destdir in shell can also be used ), at this time, it will install your installation file to the relative path under your prefix, so when we want to make install, for example, the current cmd_lib is in the lib directory, we hope that the rpath after installation can be found automatically, so we can write it like this.
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
Note that this variable is a global variable, which means that all the rpaths of your target will be written as this variable during the install process (including myexe and cmd_lib without rpath ), is there a simple target? You should have thought of it.
set_target_properties(myexe PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
This ensures that rpath is written only when make install is performed for the current target.
The above is a supplement to the previous article cmake and rpath. I hope you will like it :)