Undefined reference problems - Fortran

My objective is to link my object with the static lib files like




I have add some codes in my codes target_link_libraries functions like target_link_libraries(objects MKLROOT.a)

The make command is:

cmake -DBLA_STATIC=ON …(no error)

make install(error)

The error message is:

opt/intel/mkl/lib/intel64/libmkl_core.a(dsytrf.o): In function ‘mkl_lapack_dsytrf’:

dsytrf_gen.f:(.test+0x430): undefined reference to “mkl_lapack_dlasyf”

dsytrf_gen.f:(.test+0x6a5): undefined reference to “mkl_lapack_dlasyf”

opt/intel/mkl/lib/intel64libmkl_core.a(dsygst.o): In function ‘mkl_lapack_dsygst’:

dsygst_gen.f:(.test+0x320): undefined reference to “mkl_blas_dsyr2k”

dsygst_gen.f:(.test+0x675): undefined reference to “mkl_blas_dsyr2k”

I have not pasted all error functions. All the errors point to the same static lib file:opt/intel/mkl/intel64/libmkl_core.a.

I have obtained some help from https://www.intel.cn/content/www/cn/zh/developer/tools/oneapi/onemkl-link-line-advisor.html. After many attempts, cmake(cmake -DBLA_STATIC=ON …) and make still exists error.

I am very confused that I have added






in the function target_link_libraries(object lib) in the CMakeLists.txt of the object file.

Is it possible that I should add the MKLROOT file to other sub-objects linked by the current top-object? In fact I have added the three MKLROOT file into target_link_libraries(sub-objects MKLROOT.a) of all sub-objects. And the error still occurs.

I don’t know what happens and which file should I review or recheck to find the error comes from?

Use find_package(MKL CONFIG REQUIRED) and link target MKL::MKL.

This assumes you have oneAPI 2021.3 or newer.

I have tried the find_package(MKL CONFIG REQUIRED)

It tell me
"Could not find a package configuration file provided by “MKL” with any of the following names: MKLconfig.cmake or mkl-config.cmake. Add the installation prefix of “MKL” to CMAKE_PREFIX_PATH or set “MKL_DIR” to directory containing one the above file. If “MKL"provides a separate development package or SDK, be sure it has been installed.”

My codes will also link dynamic .so files. I want my codes to link the specified static dynamic .a files. It seems that it is invalid to set target_link_libraries(objects MKLROOT.a).

Is the Intel oneAPI environment loaded? I.e. did you source setvars.sh? If so, MKLROOT should be set, that is from the Terminal


Should tell where MKL is installed. If it’s blank, then the oneAPI environment isn’t loaded.

On a Linux system, the default oneMKL setvars.sh location is like “/opt/intel/oneapi/setvars.sh” so I would do from Terminal

source /opt/intel/oneapi/setvars.sh

that must be done in every new Terminal when you want to use oneAPI / oneMKL etc. one of the things that script does is set env var MKLROOT.

That CMake example I linked above prints this from CMake


If that is blank, then “find_package(MKL)” may not succeed.

I would choose static or dynamic via the find_package(MKL) and do only

target_link_libraries(myexe PRIVATE MKL::MKL)

Static and dynamic MKL targets required distinct supporting libraries that are encapsulated in imported target MKL::MKL

By the way, that Gist example I linked earlier works on Linux and Windows (and mac)

So following that example, in CMakeLists.txt do like

set(MKL_LINK "static")


to find/link static MKL libraries “.a” instead of the default “.so”

In my linux system, the oneAPI is not installed. In this case, how can I link the mkl static lib files?

I have installed the toolkit oneAPI. It is successful to link the static MKL lib files.

However, static MKL lib cannot ensure the accurancy of the results. It seems the results between dynamic and static MKL lib have large difference. I feel confused about that.

Do you have an example program I could try? Right the numerical results should be identical shared vs. Dynamic linking.

I haven’t tried oneMKL by itself, though it’s certainly possible to do that as a standalone install (e.g. when one wants GCC + MKL on Linux)

I have not an example. I will debug through ouputting the middle variables to find what happened. Thank you for helping me solve the static lib linking problem.