CLion can't find shared library when running a

2020-07-22 09:09发布

I am working on a project. I've been using a simple editor so far and my own Makefile to build it. I would like to switch to CLion, though.

According to this question you can tell CMake to run your Makefile. So my CMake.txt looks like this:

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})

It builds fine. I also set the working directory and pointed at the correct executable.

In my project I test using cppunit 1.13. However it can't find the shared library:

/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 points to

echo $LD_LIBRARY_PATH 

/usr/local/lib

and /usr/local/lib contains the library:

ls /usr/local/lib/

libcppunit-1.13.so.0@  libcppunit-1.13.so.0.0.2*  libcppunit.a  libcppunit.la*  libcppunit.so@  pkgconfig/

ldd shows this:

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)

So why can't CLion find it? Everything works if I run the binary from console.

标签: c++ cmake clion
3条回答
走好不送
2楼-- · 2020-07-22 09:39

As oLen correctly pointed out, CLion doesn't seem to start as my user when launched via the GUI (Gnome in my case). I don't know as what it starts, but setting LD_LIBRARY_PATH=/usr/local/lib in /etc/profile and rebooting (or resourcing) it makes it work - in a nutshell the variable wasn't set for whatever user is running CLion.

Another way is Run -> Edit Configurations -> (select your application) -> Environment variables. Here you can manually set LD_LIBRARY_PATH to whatever you need, in my case to /usr/local/lib.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-07-22 09:55

As an alternative to the accepted answer you can go to Run -> Edit Configurations -> Templates, choose CMake Application (and/or Google Test) and set Environment variables: to:

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

Any new application created from now on will inherit these settings.

查看更多
forever°为你锁心
4楼-- · 2020-07-22 09:58

Have a look at How to set the environmental variable LD_LIBRARY_PATH in linux:

If you add your custom library path to the LD configuration, then CLion will find your libraries automatically and you don't have to add them to the run configurations.

On Ubuntu/Debian, you can configure LD by creating a new .conf file

sudo nano /etc/ld.so.conf.d/myLocalLibs.conf

that simply contains the path to your libraries: /usr/local/lib. Finally, call

sudo ldconfig

to update the LD configuration.

Note that on some systems (Ubuntu/Debian) you cannot set the LD_LIBRARY_PATH in /etc/profile or /etc/environment:

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)

查看更多
登录 后发表回答