致电 C/C++ Visual Studio 中的 Fortran 程序代码? (如何在Visual Studio中编译混合C和Fortran代码)
我正在寻找一种方法,如何将 C++ 代码与 Fortran 代码集成(我想简单地在 Fortran 代码中调用一些 C/C++ 函数)。
我找到了一些关于 gcc 或控制台编译器的建议,但我不知道如何转换这种方法来解决 Visual Studio 中的集成问题。
当时我正在考虑创建一个 dll 形式的 C++ 代码并从 Fortran 代码调用它。
有人已经看到解决方案了吗?或者从 dll 调用函数的开销是多少?我的fortran代码将大量内存转移到C函数中,是否有任何问题,如果我用dll解决这个问题?
谢谢。
聚苯乙烯 我正在使用 Visual Studio 2008 Prof 和 Intel 编译器 10
PPS 我想,我必须更具体地指定我想要什么:我想在 Visual Studio 中编译一个 Fortran 项目,它使用一些 C 函数。
i am looking for a way, how i can integrate a c++ code with fortran code (i want simply call some C/C++ functions in the fortran code).
I have found some proposals for gcc or console compilers, but i have not any idea how to translate this approach to solve integrationproblem within the visual studio.
At the time I am thinking about creating a dll form c++ code and calling it from Fortran code.
Has someone already seen a solution? Or what is about overhead for calling function from dll? My fortran code transfers a lot of memory into C function, is there any problems, if i would solve this problem with dll?
thx.
PS
I am using Visual Studio 2008 Prof and Intel compilers 10
PPS
I think, i have to specify more concrete, what i want: i want to compile a fortran project in visual studio, which uses some C functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
找到解决方案:
解决方案链接
我在链接方面遇到了几个问题,可以通过添加项目属性来解决。
测试代码:
Fortran 代码
按需 我可以上传测试项目。谢谢大家,希望这是我使用 c 和 fortran 的最后一个问题
Solution found:
solution link
i have had several problem with linking, which could be solved with adding in project properties.
code for testing:
fortran code
on demand i can upload the testproject. thanks all, hopefully it was my last problem with c and fortran
多亏了 自定义构建工具,我能够从 Fortran 源构建 obj Visual Express 2010 的。我想在 Visual Studio 中也是可能的。
如果您想将 C 和 Fortran 混合在一起,这里有一个很好的教程。它是为 gcc 编译器编写的,但您应该能够学习如何轻松处理名称修改。
根据编译器的不同,编译的子例程/函数是大写/小写,带有尾随下划线,带有前导下划线,...为了成功链接,您可以使用 dumpbin 工具来查看名称在目标文件中的显示方式。
另一种方法是使用
iso_c_binding
模块,但它仅适用于 Fortran 2003。I was able to build obj from fortran sources thanks to the Custom Build Tools of Visual Express 2010. I guess it is also possible in Visual Studio.
If you want to mix C and Fortran together, there is a good tutorial here. It was written for gcc compilers but you should be able to learn how to deal with name mangling easily.
Depending on the compiler, compiled subroutines/functions are Uppercase/lowercase, with a trailing underscore, with a leading underscore,... For a succesfull linkage, you could use dumpbin tools to see how the name appears in the objectfile.
An other way is to use
iso_c_binding
modules, but it is available with Fortran 2003 only.这就是它与 gcc 和控制台 c.c 一起工作的方式
:
fortran.f90
Makefile
另一个问题:我是否总是在 c 函数名称后添加“_”,我在 fortran 程序中使用它?
This is the how it works with gcc and console
c.c:
fortran.f90
Makefile
One other question: have i always add '_' after c-function name, which i use in the fortran program?
有一种新方法可以做到这一点,它具有许多优点 - 使用 Fortran 2003 ISO C 绑定。这是一个标准,因此在很大程度上独立于操作系统和语言的 Fortran 和 C(以及任何将使用 C 调用约定的语言)接口方式。英特尔 Fortran 11 支持许多其他编译器 - 不确定版本 10。使用 ISO C 绑定,您可以匹配任何 C 名称(任何情况),不必担心下划线(以及编译器之间的差异),并且可以指定参数的类型和调用方法(通过引用、通过值)。英特尔在其编译器的文件夹中提供了一些示例; gfortran 手册中还有一些示例以及针对 Windows 的其他注意事项的讨论。有以前的问题&此处和英特尔 Fortran 论坛上的答案。
There is a new way to do this that has many advantages -- use the Fortran 2003 ISO C Binding. This is a standard and therefore largely OS and language independent way of interfacing Fortran and C (and any language that will use C calling conventions). Intel Fortran 11 supports along with numerous other compilers -- not sure about version 10. Using the ISO C Binding, you can match any C name (any case), don't have to worry about underscores (and variations between compilers) and can specify the types and calling methods (by reference, by value) for the arguments. Intel provides some examples in a folder with their compiler; there are also examples in the gfortran manual and a discussion of additional considerations for Windows. There are previous questions & answers here and on the Intel Fortran forum.
我大约 20 年前集成了 C 和 Fortran,并一直保持这种集成直到 5 年前。我使用的技巧是:
老实说,当我转向 VS2005 时,我放弃了 C 和 Fortran 的集成,所以从那时起事情可能发生了变化。尽管如此,使用 DUMPBIN 来查看 Fortran 编译器生成的符号类型并调整 C/C++ 源代码的编译以适应该符号仍然是一个好主意。
I integrated C and Fortran about 20 years ago and maintained this integration up to 5 years ago. The tricks I used were:
To be honest, I gave up integrating C and Fortran when I switched to VS2005, so things might have changed since then. Nevertheless, it's still a good idea to use DUMPBIN to see what kind of symbols the Fortran compiler produces, and adjust the compilation of C/C++ sources to fit with that.
我们在我工作的地方这样做。
假设您使用的是英特尔 Fortran 编译器,请查找其文档。默认情况下,Intel Fortran 通过引用传递所有内容,并且(我相信)使用 C 调用约定,并带有全部大写标识符。字符串是一个特殊的问题,因为 Fortran 喜欢将长度作为隐藏参数传递,并通过编译器设置它在参数列表中的位置。
明智的程序员不会依赖默认值(错误可能会导致未定义的行为),并且会使用 intel INTERFACE 语句来指定例程的调用约定、参数传递和链接名称。 此页面上的信息 ( ATTRIBUTES(属性和调用约定) 是必读的。特别是,您需要它来了解何时何地传递字符串长度参数的神秘规则。我随身携带一份打印件。 :-)
另一件需要注意的事情是 VisualStudio 6 以上的版本不喜欢混合 Fortran 和 C 项目。我们通过创建调用 makefile 的自定义项目文件解决了这个问题,但这就是 PITA。我建议顺其自然并使用单独的项目,除非您像我们一样很多这样做。
We do it where I work.
Assuming you are using the Intel Fortran compiler, look up its docs. By default Intel Fortran passes everything by reference and (I believe) uses the C calling convention, with an all caps identifier. Strings are a particular issue, as Fortran likes to pass the length as a hidden parameter, with a compiler setting for where it goes in the parameter list.
A wise programer doesn't rely on defaults (where a mistake can lead to undefined behavior), and will use the intel
INTERFACE
statements to specify calling convention, parameter passing, and the link name for the routine. The information on this page (ATTRIBUTES Properties and Calling Conventions) is a must-read. In particular you need it to understand the arcane rules for when and where string length parameters will be passed. I have a printout of it that I keep on my person. :-)One other thing to note is that versions of VisualStudio past 6 don't like mixed Fortran and C projects. We solved the problem by creating custom project files calling out to makefile, but that's a PITA. I'd suggest going with the flow and using separate projects unless you are doing this a lot like we are.