运行 可执行文件时 CLion 找不到共享库
CLion can't find shared library when running an executable
我正在做一个项目。到目前为止,我一直在使用一个简单的编辑器和我自己的 Makefile 来构建它。不过,我想切换到 CLion。
根据 this question 你可以告诉 CMake 运行 你的 Makefile。所以我的 CMake.txt
看起来像这样:
cmake_minimum_required(VERSION 3.6)
project(rekotrans_testbed_simulator)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(rekotrans_testbed_simulator COMMAND make -C ${rekotrans_testbed_simulator_SOURCE_DIR} CLION_EXE_DIR=${PROJECT_BINARY_DIR})
构建良好。我还设置了工作目录并指向正确的可执行文件。
在我的项目中,我使用 cppunit 1.13
进行测试。但是它找不到共享库:
/home/kunterbunt/dev/comnets/git-repository/rekotrans-testbed-simulator/rekotrans-testbed-simulator-tests: error while loading shared libraries: libcppunit-1.13.so.0: cannot open shared object file: No such file or directory
LD_LIBRARY_PATH
指向
echo $LD_LIBRARY_PATH
/usr/local/lib
和 /usr/local/lib
包含库:
ls /usr/local/lib/
libcppunit-1.13.so.0@ libcppunit-1.13.so.0.0.2* libcppunit.a libcppunit.la* libcppunit.so@ pkgconfig/
ldd
显示:
ldd /home/kunterbunt/dev/comnets/git-repository/rekotrans-testbed-simulator/rekotrans-testbed-simulator-tests
linux-vdso.so.1 (0x00007ffc257e8000)
libboost_thread.so.1.63.0 => /usr/lib/libboost_thread.so.1.63.0 (0x00007f1c73254000)
libboost_system.so.1.63.0 => /usr/lib/libboost_system.so.1.63.0 (0x00007f1c73050000)
libboost_date_time.so.1.63.0 => /usr/lib/libboost_date_time.so.1.63.0 (0x00007f1c72e3f000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f1c72c22000)
libboost_program_options.so.1.63.0 => /usr/lib/libboost_program_options.so.1.63.0 (0x00007f1c729a4000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f1c727a0000)
libcppunit-1.13.so.0 => /usr/local/lib/libcppunit-1.13.so.0 (0x00007f1c72563000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f1c721db000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f1c71ed7000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f1c71cc0000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f1c71922000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f1c7171a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1c7347c000)
那么为什么 CLion 找不到它呢?如果我 运行 来自控制台的二进制文件,一切正常。
正如 oLen 正确指出的那样,当通过 GUI(在我的例子中是 Gnome)启动时,CLion 似乎并不是作为我的用户启动的。我不知道它是什么开始的,但是在 /etc/profile
中设置 LD_LIBRARY_PATH=/usr/local/lib
并重新启动(或重新配置)它使它工作 - 简而言之,变量没有为任何用户设置 运行 CLion.
另一种方式是Run -> Edit Configurations -> (select your application) -> Environment variables
。在这里您可以手动将 LD_LIBRARY_PATH
设置为您需要的任何值,在我的例子中为 /usr/local/lib
.
看看How to set the environmental variable LD_LIBRARY_PATH in linux:
如果您将自定义库路径添加到 LD 配置,则 CLion 会自动找到您的库,您不必将它们添加到 运行 配置。
在 Ubuntu/Debian 上,您可以通过创建新的 .conf
文件来配置 LD
sudo nano /etc/ld.so.conf.d/myLocalLibs.conf
仅包含您的库的路径:/usr/local/lib
。最后调用
sudo ldconfig
更新 LD 配置。
请注意,在某些系统 (Ubuntu/Debian) 上,您无法在 /etc/profile 或 /etc/environment:
中设置 LD_LIBRARY_PATH
Since Ubuntu 9.04 Jaunty Jackalope, LD_LIBRARY_PATH cannot be set in
$HOME/.profile, /etc/profile, nor /etc/environment files. You must use
/etc/ld.so.conf.d/*.conf configuration files. See Launchpad bug #366728
for more information.
(help.ubuntu.com)
作为已接受答案的替代方案,您可以转到 Run -> Edit Configurations -> Templates
,选择 CMake Application
(and/or Google Test
)并将 Environment variables:
设置为:
LD_LIBRARY_PATH=/usr/local/gcc-latest/lib64
从现在开始创建的任何新应用程序都将继承这些设置。
我正在做一个项目。到目前为止,我一直在使用一个简单的编辑器和我自己的 Makefile 来构建它。不过,我想切换到 CLion。
根据 this question 你可以告诉 CMake 运行 你的 Makefile。所以我的 CMake.txt
看起来像这样:
cmake_minimum_required(VERSION 3.6)
project(rekotrans_testbed_simulator)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(rekotrans_testbed_simulator COMMAND make -C ${rekotrans_testbed_simulator_SOURCE_DIR} CLION_EXE_DIR=${PROJECT_BINARY_DIR})
构建良好。我还设置了工作目录并指向正确的可执行文件。
在我的项目中,我使用 cppunit 1.13
进行测试。但是它找不到共享库:
/home/kunterbunt/dev/comnets/git-repository/rekotrans-testbed-simulator/rekotrans-testbed-simulator-tests: error while loading shared libraries: libcppunit-1.13.so.0: cannot open shared object file: No such file or directory
LD_LIBRARY_PATH
指向
echo $LD_LIBRARY_PATH
/usr/local/lib
和 /usr/local/lib
包含库:
ls /usr/local/lib/
libcppunit-1.13.so.0@ libcppunit-1.13.so.0.0.2* libcppunit.a libcppunit.la* libcppunit.so@ pkgconfig/
ldd
显示:
ldd /home/kunterbunt/dev/comnets/git-repository/rekotrans-testbed-simulator/rekotrans-testbed-simulator-tests
linux-vdso.so.1 (0x00007ffc257e8000)
libboost_thread.so.1.63.0 => /usr/lib/libboost_thread.so.1.63.0 (0x00007f1c73254000)
libboost_system.so.1.63.0 => /usr/lib/libboost_system.so.1.63.0 (0x00007f1c73050000)
libboost_date_time.so.1.63.0 => /usr/lib/libboost_date_time.so.1.63.0 (0x00007f1c72e3f000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f1c72c22000)
libboost_program_options.so.1.63.0 => /usr/lib/libboost_program_options.so.1.63.0 (0x00007f1c729a4000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f1c727a0000)
libcppunit-1.13.so.0 => /usr/local/lib/libcppunit-1.13.so.0 (0x00007f1c72563000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f1c721db000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f1c71ed7000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f1c71cc0000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f1c71922000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f1c7171a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1c7347c000)
那么为什么 CLion 找不到它呢?如果我 运行 来自控制台的二进制文件,一切正常。
正如 oLen 正确指出的那样,当通过 GUI(在我的例子中是 Gnome)启动时,CLion 似乎并不是作为我的用户启动的。我不知道它是什么开始的,但是在 /etc/profile
中设置 LD_LIBRARY_PATH=/usr/local/lib
并重新启动(或重新配置)它使它工作 - 简而言之,变量没有为任何用户设置 运行 CLion.
另一种方式是Run -> Edit Configurations -> (select your application) -> Environment variables
。在这里您可以手动将 LD_LIBRARY_PATH
设置为您需要的任何值,在我的例子中为 /usr/local/lib
.
看看How to set the environmental variable LD_LIBRARY_PATH in linux:
如果您将自定义库路径添加到 LD 配置,则 CLion 会自动找到您的库,您不必将它们添加到 运行 配置。
在 Ubuntu/Debian 上,您可以通过创建新的 .conf
文件来配置 LD
sudo nano /etc/ld.so.conf.d/myLocalLibs.conf
仅包含您的库的路径:/usr/local/lib
。最后调用
sudo ldconfig
更新 LD 配置。
请注意,在某些系统 (Ubuntu/Debian) 上,您无法在 /etc/profile 或 /etc/environment:
中设置 LD_LIBRARY_PATHSince Ubuntu 9.04 Jaunty Jackalope, LD_LIBRARY_PATH cannot be set in $HOME/.profile, /etc/profile, nor /etc/environment files. You must use /etc/ld.so.conf.d/*.conf configuration files. See Launchpad bug #366728 for more information. (help.ubuntu.com)
作为已接受答案的替代方案,您可以转到 Run -> Edit Configurations -> Templates
,选择 CMake Application
(and/or Google Test
)并将 Environment variables:
设置为:
LD_LIBRARY_PATH=/usr/local/gcc-latest/lib64
从现在开始创建的任何新应用程序都将继承这些设置。