首页 > 解决方案 > 互操作性:Fortran 到 C++

问题描述

我想开发一个Fortran 静态库,其中包含一个需要很长时间才能完成的自定义子例程。 这个静态库也将静态链接到我的 C++ 应用程序中。

我的目标是在我的 C++ 应用程序中实时监控这个子程序的当前状态。

因此,对于“fortran 循环”的每一步,我想将循环索引发送到我的 C++ 应用程序。

我是 Fortran 世界的新手,所以我想这个任务可能是这样的:

我的 C++ 头文件:

extern "C" void fortran_status(int* value);

我的 Fortran-90 文件:

module my_interfaces
    use iso_c_binding
        interface
            subroutine fortran_status(progress_value) bind(C, name = 'fortran_status')
                use, intrinsic :: iso_c_binding
                integer(c_int), intent(out) :: progress_value
            end subroutine fortran_status
        end interface
end module my_interfaces


! My long calc subroutine
subroutine my_long_calc(progress_value) BIND(C, name = 'my_long_calc')
    use, intrinsic :: ISO_C_BINDING
    use my_interfaces
    implicit none

    EXTERNAL fortran_status

    integer (C_INT), INTENT(INOUT) :: progress_value
    integer (C_INT) :: count


    do count = 0, 5    
        progress_value = progress_value + 1

        ! Send 'progress_value' to a C++ function:
        call fortran_status(progress_value)

        ! Wait 1 second:
        call sleep(1)
    end do

end subroutine my_long_calc

这个 Fortran 代码给了我一个编译时错误:

error #6406: Conflicting attributes or multiple declaration of name.   [FORTRAN_STATUS]       C:\Users\lamar\Desktop\Lib1_interface\Lib1.f90    18 

我将 Microsoft Visual Studio 2019 与 Intel Visual Fortran (Windows 10 x64) 结合使用。

我如何监控该子程序状态?或者在我的 C++ 应用程序中获取 fortran 循环索引?

更新 1:

我删除了EXTERNAL fortran_status,现在我的 Fortran 代码的编译时没有错误。

现在,我想将这个静态库(x86)与我的 C++ 代码链接起来:

#include <iostream>

extern "C" {
  void my_long_calc(void);
  void fortran_status(int* value);
}

void fortran_status(int* value)
{
  std::cout << "Fortran current status = " << *value << std::endl;
}

int main (void)
{
  std::cout << "Monitoring Fortran subroutine..." << std::endl;

  my_long_calc();

  return 0;
}

我正在尝试使用 MingW 编译和链接它:

g++ -Wall -L. .\Lib2.lib -lgfortran .\main.cpp -o app.exe

我得到了这个链接器错误: undefined reference to my_long_calc

我怎样才能链接它?

我想在我的终端输出中看到:

Monitoring Fortran subroutine...

Fortran current status = 0
Fortran current status = 1
Fortran current status = 2
​​​​​​​Fortran current status = 3
​​​​​​​Fortran current status = 4
​​​​​​​Fortran current status = 5

更新 2

现在这个改变的 Fortran 代码编译并且只有当我使用 MingW g++ (x86) 时它才能正常工作。

Fortran 代码:

module my_interfaces
  use iso_c_binding
  interface
    subroutine fortran_status(progress_value) bind(C, name = 'fortran_status')
      use, intrinsic :: iso_c_binding
      integer(c_int), intent(out) :: progress_value
    end subroutine fortran_status
  end interface
end module my_interfaces


! My long calc subroutine
subroutine my_long_calc() BIND(C, name = 'my_long_calc')
  use, intrinsic :: ISO_C_BINDING
  use my_interfaces
  implicit none

  integer (C_INT) :: progress_value
  integer (C_INT) :: count

  progress_value = 0

  do count = 0, 5
    progress_value = count

    ! Send 'progress_value' to a C++ function:
    call fortran_status(progress_value)

    ! Wait 1 second:
    call sleep(1)
  end do
end subroutine my_long_calc

C++ 代码:

#include <iostream>

extern "C" {
    void my_long_calc(void);
    void fortran_status(int* value);
}

void fortran_status(int* value)
{
    std::cout << "Fortran current status = " << *value << std::endl;
}

int main (void)
{
    std::cout << "Monitoring Fortran subroutine..." << std::endl;

    my_long_calc();

    return 0;
}

编译c++:g++ -Wall -c .\main.cpp

编译fortran:gfortran -c .\gfortran.f90

链接在一起:g++ .\main.o .\gfortran.o -o app.exe -lgfortran

现在的问题是我需要使用 Visual Studio 2019 和 Visual Fortran 来开发我的 Fortran 代码。 我想使用 Visual Studio将所有 Fortran 代码编译为单个静态库 (*.lib) 文件。

我需要更改什么才能使用 MingW g++ 命令链接 *.lib 文件(来自 Visual Fortran)?

我正在考虑使用这样的东西: g++ main.cpp my_lib.lib -o app.exe

但我得到了这个链接器错误: undefined reference to my_long_calc

我需要做什么?

标签: fortranintel-fortran

解决方案


“Update 2”中的代码非常好,可以与 Intel Visual Fortran 和 Microsoft Visual C++ 一起使用。另请参阅您在https://community.intel.com/t5/Intel-Fortran-Compiler/Interoperability-Fortran-to-C/mp/1144147中开始的讨论

我不建议在 Windows 上将 Intel Visual Fortran 编译对象/库与 g++ 混合使用。如果您满足相当宽松的许可条款,您可以免费获得 Microsoft Visual Studio 社区版。如果你坚持使用g++,可以和gfortran混合使用。


推荐阅读