# Compile a portable executable with lapack

**URL:** https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018
**Category:** Help
**Created:** [June 22, 2023, 8:23am UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018 "2023-06-22T08:23:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![XTof38](https://avatars.discourse-cdn.com/v4/letter/x/76d3ee/32.png) [@XTof38](https://fortran-lang.discourse.group/u/XTof38)
#### Post date: [June 22, 2023, 8:23am UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/1 "2023-06-22T08:23:35Z")

</div>

The following sample program calls DGEEV from lapack:

```auto
       program main
       Implicit None
       external :: DGEEV       
       Real :: VL(4,4),VR(4,4),WORK(4),eigenvalues_R(4),eigenvalues_I(4),companion_matrix(4,4)
       Integer (kind=4) :: LWORK,INFO,Var               
       companion_matrix(4,4)=1.D0
       LWORK=-1
       call DGEEV( 'N', 'N', 4, companion_matrix, 4, eigenvalues_R, eigenvalues_I, VL, 4, &
     & VR, 4, WORK, LWORK, INFO )
       print*,"DGEEV called",LWORK
       end

```

`ifort -o test.exe main.f90 /lib64/liblapack.so.3`  
compiles it fine, but test.exe is not portable to a computer that does not have lapack installed. I tried:  
ifort -static -o test.exe main.f90 /lib64/liblapack.so.3  
ld: attempted static link of dynamic object `/lib64/liblapack.so.3’  
and with (/usr/lib64/liblapack\_pic.a has the correct rights)  
ifort -static -o test.exe main.f90 /usr/lib64/liblapack\_pic.a  
ld: cannot find -lm  
ld: cannot find -lpthread  
ld: cannot find -lc  
ld: cannot find -ldl  
ld: cannot find -lc  
What am I doing wrong?

---

<div class="post-metadata">

### Author: ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)
#### Post date: [June 22, 2023, 12:28pm UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/2 "2023-06-22T12:28:37Z")

</div>

Either ship `liblapack.so` with your program or link against the static library `liblapack.a` instead.

Also, make sure the linker can find the other shared libraries by passing the library search path through command-line argument `-L`.

---

<div class="post-metadata">

### Author: ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)
#### Post date: [June 22, 2023, 12:59pm UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/3 "2023-06-22T12:59:33Z")

</div>

To compile try this:

> ifort -o test.exe main.f90 -L/usr/lib64 -llapack

where /usr/lib64 contains the liblapack.so.

For shipping, you can compile the `lapack library` in the current directory containing the main.f90 or any other location in the shipped directory.

---

<div class="post-metadata">

### Author: ![VladimirF](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VladimirF](https://fortran-lang.discourse.group/u/VladimirF)
#### Post date: [June 22, 2023, 3:24pm UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/4 "2023-06-22T15:24:22Z")

</div>

With `ifort -static -o test.exe main.f90 /usr/lib64/liblapack_pic.a` you are trying to statically link all libraries into your executable. That includes the system’s C library, most likely GLIBC on a GNU-derived system. Linking GLIBC statically has many issues ([c++ - Why is statically linking glibc discouraged? - Stack Overflow](https://stackoverflow.com/questions/57476533/why-is-statically-linking-glibc-discouraged)), but foremost, you need to install the static version of the libraries in question. That means libm, libpthread, libc. For my distribution, they are in the `glibc-devel-static` repository package.

I am not actually sure to what extent an ifort-compiled code needs those libraries and to what extent the need only comes from the liblapack library. You may also try linking with MKL instead.

E.g.,

```auto
ifort -static main.f90 -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_sequential.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: /data-disk2/opt/intel/oneapi/mkl/2023.0.0/lib/intel64/libmkl_core.a(mkl_memory_patched.o): in function `mkl_serv_set_memory_limit':
mkl_memory.c:(.text+0x5d1): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

```

and run

```auto
> ./a.out 
 DGEEV called -1

```

You have to observe the limitations given by that warning about the necessary GLIBC version.

After I installed the static versions of `liblapack` and `libblas` for my distribution, I can also try to link with the distribution LAPACK. But be careful, they were compiled for gfortran and yours may be as well. I can manage to link the code using

```auto
ifort -static main.f90 -llapack -lblas -lgfortran -lquadmath

```

but the executable will just crash

```auto
./a.out 
 DGEEV called -1
forrtl: severe (174): SIGSEGV, segmentation fault occurred

Stack trace terminated abnormally.

```

---

<div class="post-metadata">

### Author: ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)
#### Post date: [June 22, 2023, 3:34pm UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/5 "2023-06-22T15:34:55Z")

</div>

> [@VladimirF](#):
>
> You may also try linking with MKL instead.

This can be done with: `ifort -qmkl -static main.f90`

There are also options `-static-intel` and `-static-libgcc` for the base runtime libraries.

---

<div class="post-metadata">

### Author: ![VladimirF](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VladimirF](https://fortran-lang.discourse.group/u/VladimirF)
#### Post date: [June 22, 2023, 3:36pm UTC](https://fortran-lang.discourse.group/t/compile-a-portable-executable-with-lapack/6018/6 "2023-06-22T15:36:54Z")

</div>

Yes, that is a shorthand for the basic options, thanks. For more involved cases one should follow the Link Line Advisor [Link Line Advisor for Intel® oneAPI Math Kernel Library](https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-link-line-advisor.html)
