如何将Fortran项目与C+&#x2B相结合项目?

发布于 2025-02-10 01:43:28 字数 449 浏览 0 评论 0原文

我正在使用C ++进行数字项目,但它将在另一个Fortran项目中使用多个Fortran子例程。 Fortran项目具有头文件和多个子例程文件。文件依赖项如下图所示:

我想知道如何在Visual Studio中实现此界面?我可以设置两个项目,一个用于fortran,它包括所有fortran代码,另一个用于CPP,在标题文件和类文件中我使用extern“ c”关键词来声明Fortran Subroutine?

Update1:​​Fotran的编译器是Intel Fortran,C ++的编译器是使用MFC和标准Windows库的MSVC。

I am working on a numerical project with C++, but it will use several fortran subroutines in another fortran project. The fortran project has head files and multiple subroutine files. The file dependency is shown in the figure below:
enter image description here

I am wondering that how can I implement this interface in visual studio? Can I set up two projects, one is for fortran, it includes all fortran code, another is for Cpp, in the header file and class file I use extern "C" key word to declare the fortran subroutine?

Update1: The compiler for fotran is Intel Fortran, the compiler for C++ is msvc using MFC and the standard windows libraries.

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

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

发布评论

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

评论(1

往日 2025-02-17 01:43:28

为C ++零件创建一个可执行的项目(和VS解决方案)。现在,添加到该解决方案一个fortran静态库项目,然后将您的fortran文件添加到其中。

右键单击C ++项目,然后选择“构建依赖关系”>项目依赖性。选中Fortran库项目的框,以使其成为C ++项目的依赖。在VS的较旧版本中,这是您需要将库链接到其中的全部,但是MS删除了该功能的非Microsoft编译器,因此您还有一些其他步骤:

  • 右键单击C ++项目,选择Linker>一般的。在其他图书馆目录中,将要构建Fortran .lib的位置放置在路径中。请注意,这是一个人均配置选项,因此请重复该版本配置(请确保为库指定正确的调试或发布文件夹)。
  • 在Linker>输入>其他依赖项,插入fortran .lib文件的名称。将其与其他条目分离出半分号。要节省时间,请选择“配置”>在执行此步骤之前,在左上下拉列表中的所有配置。
  • 现在右键单击Fortran项目,选择Fortran>库。确保将运行时库设置为调试配置的多线程dll,并且发行配置的多线程dll

这应该是您需要构建的全部。您可能需要在FORTRAN代码中使用C互操作性功能,包括将绑定(C)添加到C ++代码所调用的任何例程中,并使用name = specifier,具有case敏感的名称C ++将使用。请注意,仅当例程具有绑定(C)时,Fortran值属性意味着逐个价值。

Intel Fortran样品捆绑包中有一个有效的C_CALLS_FORTRAN示例, https://software.intel.com/content/www/us/en/develop/download/776976.html (在Compiler_f \ mixchlanguage下)。如果您需要更多帮助,请在Intel fortran用户论坛上询问 https://community.intel.com/t5/intel-fortran-compiler/bd-p/fortran-compiler

Create an executable project (and VS Solution) for the C++ part. Now add to that solution a Fortran Static Library project and add your Fortran files to it.

Right click on the C++ project and select Build Dependencies > Project Dependencies. Check the box of the Fortran library project to make it a dependent of the C++ project. In older versions of VS, this is all you would need to have the library linked in, but MS removed that function for non-Microsoft compilers, so you have some additional steps:

  • Right click on the C++ project, select Linker > General. In Additional Library Directories, put in the path to where the Fortran .LIB will be built. Note that this is a per-configuration option, so repeat it for the Release configuration (make sure you specify the correct Debug or Release folder for the library).
  • In Linker > Input > Additional Dependencies, insert the name of your Fortran .LIB file. Separate it from other entries there with a semicolon. To save time, select Configuration > All Configurations in the upper-left dropdown before doing this step.
  • Now right-click on the Fortran project, select Fortran > Libraries. Make sure that Runtime Library is set to Debug Multithread DLL for the Debug configuration, and Multithread DLL for the Release configuration

This should be all you need to build. You will likely want to use the C interoperability features in your Fortran code, including adding BIND(C) to any routines called by the C++ code, with a NAME= specifier with the case-sensitive name C++ will use. Be aware that the Fortran VALUE attribute means pass-by-value only when the routine has BIND(C).

There is a worked C_Calls_Fortran example in the Intel Fortran samples bundle at https://software.intel.com/content/www/us/en/develop/download/776976.html (under compiler_f\MixedLanguage). If you need more help, ask in the Intel Fortran user forum at https://community.intel.com/t5/Intel-Fortran-Compiler/bd-p/fortran-compiler

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