-1

I am trying to compile a minimal C++ code

#include <iostream>
#include <mkl.h>
#include <omp.h>

int main(int argc, char *argv[])
{
omp_set_num_threads(4);

return 0;
}

using the MKL library (icc version 17.0.4) in a MacOSX Sierra 10.12.5, using the command

icc main.cpp -o main.o -DMKL_ILP64 -I/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/include \
  -L/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/lib -Wl,-rpath,/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/lib -lmkl_intel_ilp64 \
  -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm -ldl

However I receive the following error when I run the program

dyld: Library not loaded: @rpath/libiomp5.dylib
  Referenced from: /Users/user/C++/MKL1/./main.o
Reason: image not found
/bin/sh: line 1:  8898 Abort trap: 6           ./main.o
make: *** [run] Error 134

How to set up properly the openmpi with the MKL library? I tried to follow the instruction in https://software.intel.com/en-us/articles/dyld-library-not-loadedlibiomp5dylib?page=1#comment-1905809

by adding

 source /opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/bin/mklvars.sh intel64

However I still get the same error during runtime.

Galuoises
  • 2,630
  • 24
  • 30
  • I seem to remember icc compiling OpenMP code with -openmp and not linking to an (external) library (-liomp5). Not sure if that is still true. – Gerriet May 24 '17 at 15:08

2 Answers2

1

I have tried a few ways to solve this issue. It literally took an entire day. But here are the results,

Project Files

I created a very simple Project with just one file, named,

test.c

1. The Easiest Solution

Fixing this can be as simple as,

icc -qopenmp -qopenmp-link=static test.c

This method just tells the compiler to link to static OpenMP run-time libraries.

Interestingly, the docs at Intel C++ Compiler Docs v.15 say that this is deprecated while Intel C++ Compiler Docs v.17 do not even mention about this argument, BUT it works. Probably they forgot to take it out.

2. Another method (which I do not recommend, nor do folks at Intel)

To compile the file you can use the normal "-qopenmp" flag

icc -qopenmp test.c

This creates the "a.out" file.

This method of fixing the error makes use of command-line utility called,

install_name_tool

Using this method we can change the path of @rpath/libiomp5.dylib

install_name_tool -change @rpath/libiomp5.dylib /opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib/libiomp5.dylib a.out

Note: In place of compilers_and_libraries_2018.1.126 it should be your version of the compiler.

3. One of the BEST (correct) ways

You can just add

export DYLD_LIBRARY_PATH="/opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib"

To your ~/.bash_profile

And then use the normal way of compiling,

icc -qopenmp test.c

Everything works perfectly.


Quick tip: You can use the otool command line utility to check the libraries your file links to.

otool -L a.out
incorelabs
  • 566
  • 5
  • 13
0

I found the answer on https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/645194

Basically one has to add -Wl,-rpath,/opt/intel/compilers_and_libraries_2017.4.181/mac/compiler/lib

during compilation. Now openmp with icc works perfectly

Galuoises
  • 2,630
  • 24
  • 30
  • In Xcode one has to add /opt/intel/compilers_and_libraries_2017.4.181/mac/compiler/lib in the Runpath search path – Galuoises May 31 '17 at 12:52