1

I have installed intel mkl library. contents have path /home/user/intel/..... . I have to run a C++ code using make file on which it is mentioned.

CC = /home/user/intel/bin/icpc -g
INCLUDE = -I/home/user/intel/mkl/include 
LIB = -L/home/user/intel/mkl/lib/intel64 -lmkl_core -lmkl_intel_lp64 -lmkl_intel_thread -liomp5 -lpthread -std=c++11

I have successfully installed parallel_studio_xe_2019_update5_cluster_edition . but still I'm getting an error message that ./main :error while loading shared libraries. How can I fix this error. What changes I need to do?

huda_
  • 57
  • 1
  • 7
  • Did you follow the MKL documentation, namely that [part for setting environment variables by including a given script](https://software.intel.com/en-us/mkl-linux-developer-guide-scripts-to-set-environment-variables) (such as `mklvars.sh`) in your environment? – Daniel Langr Sep 20 '19 at 13:55
  • I only run install_GUI.sh in the terminal. But where can I find mklvars.sh intel64 – huda_ Sep 20 '19 at 14:09
  • Yes, you should include that script in your Bash session, see here how: [How to include file in a bash shell script](https://stackoverflow.com/q/10823635/580083). A common way is to put this inclusion into a Bash startup script (sucha a `~/.bashrc`) to apply it automatically to all Bash sessions. – Daniel Langr Sep 20 '19 at 14:11
  • Before running your application, at the shell, try this: `export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/your/library` – Amadeus Sep 20 '19 at 14:14
  • For me its hard to grasp this explanation. Can you please explain it with some easy language.I compile my code simply by writing make, what will be /path/to/your/library in my case ? – huda_ Sep 20 '19 at 14:20
  • @huda_ Only you can know it. but it seems to be `/home/user/intel/mkl/lib/intel64` – Amadeus Sep 20 '19 at 14:34
  • In my case it is "/home/user/intel/mkl/lib/intel64". I have modified the path makefile. But stil getting same error – huda_ Sep 20 '19 at 14:43
  • Also see [Why audo /sbin/ldconfig -v doesn't fix the ./main: error while loading shared libraries: libmkl_core.so](https://stackoverflow.com/q/58026938/608639) – jww Sep 20 '19 at 15:42
  • What is your code doing? Is there any run-time dynamic loading of shared object (using e.g. `dlopen`)? – Some programmer dude Sep 21 '19 at 10:13

1 Answers1

3

Linking with shared libraries is actually done in two steps: When building (where the linker needs to find the library); And when running (when the operating system dynamic loaded needs to find the library).

When building with libraries installed in non-standard locations, you tell the linker where to find the library using the -L option. Unfortunately it doesn't tell the dynamic loader where the library is located.

To tell the dynamic loader the location of a dynamic library there are a couple of way, the one I recommend is to add a flag when building so the linker will embed the location inside the executable program file for the dynamic loader to see. This is done with the option -Wl,-rpath,/path/to/lib/directory.

In your case you need to add the option -Wl,-rpath,/home/user/intel/mkl/lib/intel64 to the LIB makefile variable.


To clarify, the full line should be

LIB = -L/home/user/intel/mkl/lib/intel64 -Wl,-rpath,/home/user/intel/mkl/lib/intel64 -lmkl_core -lmkl_intel_lp64 -lmkl_intel_thread -liomp5 -lpthread -std=c++11 

That is, you need both the old -L option (as you current have it in the code you show) and add the new option.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But then how and where should I need to define rpath? – huda_ Sep 20 '19 at 13:50
  • @huda_ ***Almost***, `-Wl` with `l` as in lower-case `L`. And you still need the `-L` option for the linker to find the libraries. – Some programmer dude Sep 20 '19 at 13:53
  • I edited like this: LIB = -L//-Wl,rpath//home/tqc04/intel/mkl/lib/intel64 -lmkl_core -lmkl_intel_lp64 -lmkl_intel_thread -liomp5 -lpthread -std=c++11 . But still can't fix this error.I never met with such type of problem. I mistakenly deleted some files in home directory after which i'm facing this problem – huda_ Sep 20 '19 at 13:59
  • @huda_ You're misunderstanding what I mean. See my updated answer – Some programmer dude Sep 20 '19 at 14:01
  • I mentioned that line but still getting same error. – huda_ Sep 20 '19 at 14:27
  • I'm getting the same error even after modifying LIB as you mentioned – huda_ Sep 21 '19 at 02:31
  • @huda_ Then you're doing something else wrong. Please edit your question to show us the *complete* makefile, and the *complete* build log (including the commands that run with their flags). – Some programmer dude Sep 21 '19 at 09:35