Fortran中的共享库,最小的示例不起作用 [英] Shared library in Fortran, minimal example does not work

查看:88
本文介绍了Fortran中的共享库,最小的示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在Linux下的Fortran中动态创建和链接共享库.

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

我有两个文件:第一个文件liblol.f90看起来像这样:

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

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

我用gfortran -shared -fPIC -o liblol.so liblol.f90

第二个文件main.f90看起来像这样:

The second file, main.f90, looks like this:

program main
    call func()
end program main

当我现在尝试使用命令gfortran -L. -llol main.f90 -o main进行编译时,出现以下错误:

When I now try to compile that 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

我不明白为什么它说未定义引用",因为nm -D liblol.so的输出给了我这个:

I do not understand why it says "undefined reference", since the output of nm -D liblol.so gives 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_

还需要其他参数吗?

推荐答案

唯一需要更改的是参数的顺序,如

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

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

是的,只有main.f90和-llol被颠倒了.我希望这样可以节省别人他的生活一年,我只是失去了对这个.与此相关的是,如果您尝试编译使用LAPACK或BLAS的程序(对我不起作用,这就是为什么我首先尝试自己创建共享库的原因),则同样适用. 首先写出源文件的名称:

Yes, only main.f90 and -llol are reversed. I hope this saves someone the year of his life I just lost on this. On a related note, if you are trying to compile a program which uses LAPACK or BLAS (which did not work for me and is why in the first place I tried to create a shared library myself), the same also applies. Write the name of the source file first:

gfortran mylapack.f90 -llapack -lblas -o mylapack

其原因可以在手册页中找到,请参见 http://gcc的顶部.gnu.org/onlinedocs/gcc/Link-Options.html 用于选项-l:

The reason for this can be found in the manual pages, see the top of http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html for the option -l:

在命令中写入此选项的位置有所不同;这 链接器按顺序搜索和处理库和目标文件 他们被指定.因此,foo.o -lz bar.o在之后搜索库z 文件foo.o但在bar.o之前.如果bar.o引用了"z"中的函数, 这些功能可能无法加载.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

这篇关于Fortran中的共享库,最小的示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆