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.