且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CMake中更改LD_LIBRARY_PATH?

更新时间:2023-12-03 23:49:10

如果您的共享库不是在可执行文件的同一个CMake项目中构建,您可以使用CMake rpath处理,如下所示:

If your shared lib is not build in the same CMake project of your executable, you can use the CMake rpath handling like this:

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

当您将运行 make install 时,CMake会自动将您的可执行文件的运行时路径设置为您的共享库。

When you will run make install, CMake will automatically set the runtime path of your executable to your shared library.

如果您的共享库是在同一个CMake项目中构建,请使用:

If your shared library is built in the same CMake project, use this:

set(CMAKE_INSTALL_RPATH "/usr/local/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

在这种情况下,您的共享库将安装到运行时路径。

In this case you must add yourself the directory where your shared library will be installed to the runtime path.

有关详细信息,请参阅 CMake rpath处理

For more information, you can read CMake rpath handling