# Compiling simple Fortran program using fpm and local lapack and blas

**URL:** <https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754>\
**Category:** fpm\
**Created:** [November 17, 2022, 7:19am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754 "2022-11-17T07:19:10Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [November 17, 2022, 7:19am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/1 "2022-11-17T07:19:10Z")

</div>

I am using fpm and locally installed Lapack and Blas libraries to test the Ax=b for a simple case (without fpm the example works perfectly): here is the example

```auto
program main
  use example1, only: say_hello ! calling module example1.f90 from src
  implicit none
    external :: sgesv
    real :: amat(2, 2)  
    real :: bvec(2)     
    real :: pivot(2) 
    integer :: rcval ,i, j

    amat = reshape([2., 3., 2., 2.], [2, 2])
    bvec = [3., 6.]

    print*, 'Amat: '
    do j = 1,2
        print*, (amat(i,j),i=1,2)
    enddo

    call sgesv(2, 1, amat, 2, pivot, bvec, 2, rcval)

    if (rcval /= 0) then
        print '(a, i0)', 'Error: ', rcval
        stop
    end if

    print*, "bvec: ", bvec
    print '("solution: x vect ", f0.6, ", ", f0.6)', bvec
    
    call say_hello()

!
! using gFortran for this test_example.f90 work fine
! gfortran -L/usr/loca/lib -o example.x example.f90 -llapack -lblas && ./example.x
!! Amat: 
! 2.00000000 3.00000000    
! 2.00000000 2.00000000    
! bvec: 3.00000000 -1.50000012    
! solution: x vect 3.000000, -1.500000
!
end program main

```

With fpm, I am getting the following error:

```auto
fpm build --flag "-L/usr/loca/lib -lblas -llapack"
example1 failed.
[100%] Compiling...
/usr/bin/ld: build/gfortran_76E40220992853F5/example1/app_main.f90.o: in function `MAIN__':
main.f90:(.text+0x199): undefined reference to `sgesv_'
collect2: error: ld returned 1 exit status
<ERROR> Compilation failed for object " example1 "
<ERROR>stopping due to failed compilation
STOP 1

```

Any suggestion will be very helpful.

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [November 17, 2022, 7:34am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/2 "2022-11-17T07:34:17Z")

</div>

```nohighlight
[build]
link = ["blas", "lapack"]

```

add link in fpm.toml maybe useful

---

<div class="post-metadata">

**Author:** ![rgba](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rgba/32/2115_2.png) [@rgba](https://fortran-lang.discourse.group/u/rgba)\
**Post date:** [November 18, 2022, 9:08pm UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/3 "2022-11-18T21:08:31Z")

</div>

Yes, but I never understood why passing the link flags manually is not sufficient (I’m not aware how fpm passes them to gfortran, or other compilers). Then of course a compiler independent way through the toml is the right and polished way to go.

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [November 19, 2022, 2:20am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/4 "2022-11-19T02:20:06Z")

</div>

> [@snano](#):
>
> `fpm build --flag "-L/usr/loca/lib -lblas -llapack"`

Apart from the other issues that have been talked about, please check the spelling of the path to the libraries: “/usr/loca/lib” should probably be “/usr/local/lib”, unless you actually have a directory called /usr/loca/lib.

---

<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:** [November 19, 2022, 3:20am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/5 "2022-11-19T03:20:23Z")

</div>

part of fpm.toml:

```auto
[build]
link=["blas", "lapack"]

```

does the job.

> Passing  
> `--flag "-L/usr/local/lib -lblas -llapack"` does not work. Simply use “fpm build”.

---

<div class="post-metadata">

**Author:** ![zoziha](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zoziha/32/582_2.png) [@zoziha](https://fortran-lang.discourse.group/u/zoziha)\
**Post date:** [November 19, 2022, 4:31am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/6 "2022-11-19T04:31:13Z")

</div>

~~`--flag` is for compiling and `--link-flag` is for linking, you can try this:~~

```nohighlight
fpm build --link-flag '-lblas -llapack'

```

Sorry, I misunderstood :\

---

<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:** [November 19, 2022, 11:28am UTC](https://fortran-lang.discourse.group/t/compiling-simple-fortran-program-using-fpm-and-local-lapack-and-blas/4754/7 "2022-11-19T11:28:51Z")

</div>

In general you should be careful about the order of the linked libraries. The typical linker will attempt to satisfy the dependencies moving from left to right. In that case the correct order would be `-llapack -lblas`, since LAPACK builds on top of BLAS.

Depending on your LAPACK installation (i.e. whether you have a shared or static library) it could happen the dependencies are embedded/chained automatically. For example ion my Mac, I was able to compile your example with `gfortran main.f90 -llapack`. By adding `-v` to to produce verbose output, I was able to infer that I was calling the LAPACK library included in the system-installed [Apple Accelerate framework](https://developer.apple.com/documentation/accelerate).

Using the the programs `otool` and `dyld_info` I could pinpoint the exact dynamic libraries being used:

```auto
$ gfortran -o main call_sgesv.f90 -llapack
$ otool -L main
main:
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/opt/gcc/lib/gcc/current/libgfortran.5.dylib (compatibility version 6.0.0, current version 6.0.0)
	/usr/local/opt/gcc/lib/gcc/current/libgcc_s.1.1.dylib (compatibility version 1.0.0, current version 1.1.0)
	/usr/local/opt/gcc/lib/gcc/current/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)

```

```auto
$ dyld_info -dependents /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib [x86_64h]:
    -dependents:
        attributes load path
                       /usr/lib/libc++.1.dylib
                       /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
                       /usr/lib/libSystem.B.dylib

```
