[英]How to use Fortran shared library in C program?
I have put several subroutines into .f95 file, compiled it with gfortran -shared -fPIC -o bin/Debug/libpr10.so main.f95
, now I want to use some of subroutines from that library in my C program. 我已经将几个子例程放入.f95文件中,并使用
gfortran -shared -fPIC -o bin/Debug/libpr10.so main.f95
,现在我想在我的C程序中使用该库中的一些子例程。 How do I do that (syntactically)? 我该怎么做(语法上)? Do I have to produce any "headers" in Fortran library where I should declare the subroutines I want to use outside of the library (as I would have to if it was C)?
我是否需要在Fortran库中生成任何“标头”,以便在其中声明要在库外使用的子例程(就像是C一样)? If I just tell the linker where the library is and try to call any subroutines in
main()
i get an error 如果我只是告诉链接器库在哪里,并尝试调用
main()
任何子例程,则会收到错误消息
UPDATE I don't want to mix fortran and C! 更新我不想混合使用fortran和C! I just want to use one subroutine from Fortran shared library, even w/o arguments.
我只想使用Fortran共享库中的一个子例程,甚至不带参数。 Despite that I have pointed out a path to the .so library, it's "invisible" for C program!
尽管我已经指出了.so库的路径,但对于C程序而言,它是“不可见的”! The question is how to tell C program that there actually IS a subroutine that I want to invoke.
问题是如何告诉C程序实际上有一个我要调用的子例程。
If I'm violently mistaken with "mixing" idea, correct me. 如果我对“混合”想法犯了严重的错误,请纠正我。
Ok, I don't think that this solution is universal but for Microsoft Visual Studio it should be __stdcall
before subroutne name, and for gcc it should be a _
symbol after subroutine name eg we want to call subroutine called menu, so we put menu_();
好的,我认为该解决方案不是通用的,但是对于Microsoft Visual Studio,它应该在子例程名称之前是
__stdcall
,对于gcc,在子例程名称之后应该是_
符号,例如,我们要调用名为menu的子例程,因此我们将menu_();
into main function. 进入主要功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.