【问题标题】:CLion can't find shared library when running an executable运行可执行文件时,CLion 找不到共享库
【发布时间】:2017-06-18 03:54:23
【问题描述】:

我正在做一个项目。到目前为止,我一直在使用一个简单的编辑器和我自己的 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 找不到呢?如果我从控制台运行二进制文件,一切正常。

【问题讨论】:

  • 你能告诉我们ldd <your_executable>的输出吗?
  • 已编辑以显示 ldd 输出。
  • 我的猜测是 LD_LIBRARY_PATH 没有为 CLion 设置为程序。终端 within CLion 中的echo $LD_LIBRARY_PATH 是什么?
  • 也打印/usr/local/lib
  • @kunterbunt 如果从终端打开 CLion 会怎样?

标签: c++ cmake clion


【解决方案1】:

正如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

【讨论】:

    【解决方案2】:

    看看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:

    自 Ubuntu 9.04 Jaunty Jackalope 起,LD_LIBRARY_PATH 无法设置 $HOME/.profile、/etc/profile 和 /etc/environment 文件。你必须使用 /etc/ld.so.conf.d/*.conf 配置文件。见Launchpad bug #366728 了解更多信息。 (help.ubuntu.com)

    【讨论】:

      【解决方案3】:

      作为已接受答案的替代方案,您可以转到Run -> Edit Configurations -> Templates,选择CMake Application(和/或Google Test)并将Environment variables:设置为:

      LD_LIBRARY_PATH=/usr/local/gcc-latest/lib64
      

      从现在开始创建的任何新应用程序都将继承这些设置。

      【讨论】:

        猜你喜欢
        • 2012-08-13
        • 2011-12-14
        • 2018-10-10
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 2020-06-06
        • 2021-07-17
        • 1970-01-01
        相关资源
        最近更新 更多