# Undefined reference problems - Fortran

**URL:** https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837
**Category:** Usage
**Tags:** os:linux
**Created:** [August 29, 2023, 10:29am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837 "2023-08-29T10:29:55Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [August 29, 2023, 10:29am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/1 "2023-08-29T10:29:55Z")

</div>

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

opt/intel/mkl/lib/intel64/libmkl\_intel\_ilp64.a

opt/intel/mkl/lib/intel64/libmkl\_sequential.a

opt/intel/mkl/lib/intel64/libmkl\_core.a

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](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

-Wl,–start-group

opt/intel/mkl/lib/intel64/libmkl\_intel\_ilp64.a

opt/intel/mkl/lib/intel64/libmkl\_sequential.a

opt/intel/mkl/lib/intel64/libmkl\_core.a

-Wl,–end-group

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?

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [August 29, 2023, 6:52pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/2 "2023-08-29T18:52:11Z")

</div>

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

This assumes you have oneAPI 2021.3 or newer.

- [Minimal MKL example](https://gist.github.com/scivision/b81948e7cc3e220103653f6f0016ca8c)
- [reference](https://www.intel.com/content/www/us/en/docs/onemkl/developer-guide-linux/2023-2/cmake-config-for-onemkl.html)

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [August 30, 2023, 8:06am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/3 "2023-08-30T08:06:34Z")

</div>

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.”

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [August 30, 2023, 10:16am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/4 "2023-08-30T10:16:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [August 30, 2023, 8:38pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/5 "2023-08-30T20:38:39Z")

</div>

Is the Intel oneAPI environment loaded? I.e. did you [source setvars.sh](https://www.intel.com/content/www/us/en/docs/oneapi/programming-guide/2023-0/use-the-setvars-script-with-linux-or-macos.html)? If so, MKLROOT should be set, that is from the Terminal

```sh
echo $MKLROOT

```

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

```sh
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](https://gist.github.com/scivision/b81948e7cc3e220103653f6f0016ca8c) prints this from CMake

```cmake
message(STATUS "ENV{MKLROOT}: $ENV{MKLROOT}")

```

If that is blank, then “find\_package(MKL)” may not succeed.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [August 30, 2023, 8:40pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/6 "2023-08-30T20:40:20Z")

</div>

I would choose static or dynamic via the find\_package(MKL) and do only

```cmake
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

```cmake
set(MKL_LINK "static")

find_package(MKL CONFIG REQUIRED)

```

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

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [August 31, 2023, 3:53am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/7 "2023-08-31T03:53:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [September 5, 2023, 9:08am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/8 "2023-09-05T09:08:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [September 5, 2023, 9:10am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/9 "2023-09-05T09:10:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 5, 2023, 10:59pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/10 "2023-09-05T22:59:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 5, 2023, 11:01pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/11 "2023-09-05T23:01:11Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Junru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/f08c70/32.png) [@Junru](https://discourse.cmake.org/u/Junru)
#### Post date: [September 6, 2023, 1:55am UTC](https://discourse.cmake.org/t/undefined-reference-problems-fortran/8837/12 "2023-09-06T01:55:10Z")

</div>

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.
