Shared library in Fortran, minimal example not working

I am trying to understand how to dynamically create and link a shared library in Fortran under Linux.

I have two files: the first liblol.f90,, looks like this:

subroutine func()
    print*, 'lol!'
end subroutine func

I will compile it with gfortran -shared -fPIC -o liblol.so liblol.f90

The second file is main.f90as follows:

program main
    call func()
end program main

When I try to compile this with the command gfortran -L. -llol main.f90 -o main, I get the following error:

/tmp/ccIUIhcE.o: In function `MAIN__':
main.f90:(.text+0xa): undefined reference to `func_'
collect2: ld returned 1 exit status

I don’t understand why it says “undefined link” as the output nm -D liblol.sogives me this:

                 w _Jv_RegisterClasses
0000000000201028 A __bss_start
                 w __cxa_finalize
                 w __gmon_start__
0000000000201028 A _edata
0000000000201038 A _end
0000000000000778 T _fini
                 U _gfortran_st_write
                 U _gfortran_st_write_done
                 U _gfortran_transfer_character_write
0000000000000598 T _init
00000000000006cc T func_

Is any other parameter required?

+5
source share
1 answer

The only thing that needs to be changed is the order of the arguments, as in

gfortran -L. main.f90 -llol -o main

, main.f90 -llol . , - , . , , LAPACK BLAS ( , ), . :

gfortran mylapack.f90 -llapack -lblas -o mylapack

, . http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html -l:

, ; . , foo.o -lz bar.o z foo.o, bar.o. bar.o `z ', .

+8

All Articles