Visual Studio下fortran与c++/c混合编程

发布于 2024-10-01 16:01:05 字数 402 浏览 5 评论 0原文

可能很多人都问过这个问题,但是,我还没有看到有人给出满意的解决方案。 好吧,让我明确我的问题:我正在尝试从现有的 Fortran 代码中调用一些 C++ 子例程/函数。重点是我正在 Visual Studio 2008 中做所有事情(fortran 编译器是 intel fortran 11.0)。有人可以给我在同一项目中编译两种语言的分步说明吗?如果不可能在一个项目中做到这一点,我该怎么办?请显示示例代码,可能只有几行只是为了演示如何继续。

提前致谢。

欢迎任何想谈论这个问题的人。但要非常清楚的是,我正在寻找 Visual Studio 和 Intel Fortran 的解决方案(使用 GUI 进行编译和调试)。关于在 Linux 中使用 gcc/gfortran 编译,没有废话。我读过很多这样的回复。

谢谢

Many people may have asked this question, however, I have never seen any one gave a satisfactory solution.
okay, let me make my question clear: I am trying to call some c++ subroutines/functions from my exsiting fortran code. The point is i am doing everyting within visual studio 2008 (fortran compiler being intel fortran 11.0). Dose any one can give me a step by step instruction of compiling the two languages in the same project? if not possible to do that in ONE project, what do i do? plz show an example code, probably only a few lines just to demenstrate how to proceed.

Thanks in advance.

any one who want talk about this is welcome. BUT be very clear that i am looking for solutions with visual studio and intel fortran(compiling and debugging with GUI). NO BULLSHIT about compiling with gcc/gfortran in linux. I have read tons of such respond.

Thanks

Peng

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执妄 2024-10-08 16:01:05

您将无法从 Fortran 调用 C++。 Fortran 不遵循 C++ 类的概念,没有模板的概念,没有异常的概念,并且不会正确维护 C++ 对象的生存期。

您可以从 C++ 代码中公开 C 接口,Fortran 可以调用该接口。您可以将此类代码编译为 .dll,然后像在任何其他 dll 中一样加载和调用 dll 内的函数。但是,您必须查阅 Fortran 文档以了解如何调用 DLL 内的函数。

You would not be able to call C++ from Fortran. Fortran does not obey the concept of a C++ class, does not have the concept of templates, does not have the concept of exceptions, and would not correctly maintain C++ object lifetimes.

You could expose a C interface from your C++ code, which Fortran could call. You would compile such code as a .dll, and then you would load and call functions inside the dll like you would in any other dll. However, you would have to consult Fortran documentation on how to call functions inside DLLs.

來不及說愛妳 2024-10-08 16:01:05

如果您只需要 C++ 的 C 功能,一般方法是在 C++ 代码中使用 extern C。然后您可以在 Fortran 端使用 ISO C 绑定,并且 Fortran 语言标准要求 Fortran 和 C 能够互操作。在 Fortran 中,您声明一个接口来描述 C 例程。 gfortran 手册中有 Fortran / C 代码示例...适用于所有编译器,因为它是该语言的一部分。英特尔手册中的英特尔(R) Fortran/C 混合语言编程下有一些 Fortran/C 示例。印刷版请参阅 Metcalf、Reid 和 Cohen 编写的 Fortran 95 书。对于英特尔编译器特有的方面,英特尔论坛上有很多问题和答案。

If you only want C features of C++, the general approach is to use extern C in your C++ code. Then you can use the ISO C Binding on the Fortran side and the Fortran language standard requires that the Fortran and C will be interoperable. In the Fortran, you declare an interface to describe the C routine. There are Fortran / C code examples in the gfortran manual ... applicable to all compilers since it is part of the language. There are some Fortran/C examples in the Intel manual under Intel(R) Fortran/C Mixed-Language Programming. In print, see the Fortran 95 book by Metcalf, Reid and Cohen. For the aspects specific to the Intel compilers, there are many questions and answers on the Intel forums.

陌路黄昏 2024-10-08 16:01:05

Fortran 编译器应该不重要,因为它们都是 COFF 对象格式。如果我正确理解你的问题,你正在尝试从 fortran 调用 c/c++,但你的主 gui 是 VS 2008。由于你的主 gui 是 vs 2008,我进一步假设你的主体源代码是 c/c++ 并且你想调用调用 ac/c++ 例程的 Fortran 例程。鉴于此,您需要首先使用 VS 2008 将您的 c/c++ 函数编译为目标文件。然后使用 Intel Fortran 编译器将您的 fortran 例程编译为引用这些 c/c++ 函数(使用 c/c++ 目标文件)的目标文件。现在应该为您提供一个结合了 fortran 和 c/c++ 函数的新目标文件。现在您可以将此对象文件与 Visual Studio 主体源代码一起使用。最重要的是要记住,每当您编译为目标代码时,请确保静态链接运行时库。编译器开关和调用外部语言例程的语法你必须自己找出来,因为互联网上有大量材料。这是一个很好的参考http://docs .cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html

The Fortran compiler shouldn't matter since they are all COFF object format. If I understand your question correctly you are trying to call c/c++ from fortran but your main gui is VS 2008. Since your main gui is vs 2008 I am further assuming your main body source code is c/c++ and you want to call a fortran routine that calls a c/c++ routine. Given this you need to first compile ONLY your c/c++ functions into an object file using VS 2008. Then compile your fortran routine into an object file referencing those c/c++ functions (with the c/c++ object file) using the Intel Fortran compiler. This should now give you a new object file that combines the fortran and c/c++ functions. Now you can use this object file back with your Visual Studio main body source code. The biggest thing to remember is whenever you compile to object code make sure you statically link the run-time libraries. The compiler switches and syntax for calling external language routines you'll have to find out yourself since there is TON of material on the internet. Here's a good reference http://docs.cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文