Fortran和C++混合编程学习笔记

Fortran和C++混合编程学习笔记


前言

学习中有Fortran和C++混合编程的需求,但参考《Fortran95程序设计》没有成功实现,最后在这里找到解决方案,特此记录几个demo。测试在windows环境中进行,使用的编译器有g++和gfortran。

一、integer求和

Fortran调用C++函数只需要在C++函数名后加下划线,然后在Fortran代码中像subroutine那样调用。需要注意的是Fortran传到C++函数的不是数值,所以要用指针接收。
Fortran代码fmix.f90:

program main
    implicit none
    integer ::a,b,c
    a=1
    b=2

    call sum(a,b,c)

    write(*,*) c
     
end program main

C++代码cmix.cpp:

extern "C"
{
    void sum_(int *a, int *b, int *c)
    {
        *c = *a + *b;
    }
}

makefile:
链接时存在需要添加-lstdc++参数的情况。

all:mix

cmix.o:cmix.cpp
	g++ -c $^

fmix.o:fmix.f90
	gfortran -c $^

mix:cmix.o fmix.o
	gfortran -o $@ $^

运行结果:
在这里插入图片描述
代码将两个integer相加,并将结果输出。

二、一维数组求和

Fortran代码fmix.f90:

program main
    implicit none
    integer ::a(3) = (/1, 2, 3/)
    integer ::b(3) = (/4, 5, 6/)
    integer ::c(3)
    integer ::i

    call sum(a, b, c)

    write (*, *) c

end program main

C++代码cmix.cpp:

extern "C"
{
    void sum_(int *a, int *b, int *c)
    {
       for(int i=0;i<3;i++)
            c[i]=b[i]+a[i];
    }
}

makefile和之前一样。
运行结果:
在这里插入图片描述
代码将两个整型数组相加,并输出结果。

三、二维数组求和

Fortran代码fmix.f90:

program main
    implicit none
    integer ::a(2,2) = reshape((/1,2,3,4/),(/2,2/))
    integer ::b(2,2) =  reshape((/5,6,7,8/),(/2,2/))
    integer ::c(2,2)
    integer ::i

    call sum(a, b, c)

    write (*, *) c

end program main

C++代码cmix.cpp:

extern "C"
{
    void sum_(int *a, int *b, int *c)
    {
        int stride = 2;
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                c[stride * i + j] = a[stride * i + j] + b[stride * i + j];
            }
        }
    }
}

makefile和之前一样。
运行结果:
在这里插入图片描述
目前没找到C++直接接受Fortran二维数组的方式,所以在C++代码中用一维数组模拟二维数组。注意Fotran数组在内存中为列优先排列,而C++数组为行优先排列。我在demo中没有体现这一点,实际工程代码可能需要注意。

总结

以上内容只是我在学习过程中对Fortran和C++混合编程的一个记录,并没有介绍太多Fortran和C++的内容,不足之处望谅解。后续可能会更新遇到的各种Fortran和其他语言混编问题吧。。。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
FortranC++可以通过一些接口来实现混编,以下是一个简单的示例: 首先是Fortran程序,其中包含了一个Fortran函数和一个接口: ``` ! sample.f90 module sample implicit none contains ! Fortran函数 real function func(x) real, intent(in) :: x func = x**2 + 2*x + 1 end function func ! 接口 interface real function cplusplus_func(x) bind(C, name="cplusplus_func") import :: C_DOUBLE real(C_DOUBLE), intent(in) :: x end function cplusplus_func end interface end module sample ``` 接着是C++程序,其中包含了一个C++函数和一个接口: ``` // sample.cpp #include <iostream> using namespace std; extern "C" { // C++函数 double cplusplus_func(double x) { return x*x + 2*x + 1; } // 接口 extern double func_(double *x); } int main() { double x = 2.0; // 调用C++函数 double y = cplusplus_func(x); cout << "C++ function: " << y << endl; // 调用Fortran函数 double z = func_(&x); cout << "Fortran function: " << z << endl; return 0; } ``` 在C++程序中,我们使用了`extern "C"`来声明C++函数和接口,以便在链接时能够正确地找到它们。在主函数中,我们首先调用了一个C++函数,然后通过接口调用了一个Fortran函数。 编译Fortran程序时,需要使用`-c`选项生成静态库文件: ``` $ gfortran -c sample.f90 ``` 编译C++程序时,需要链接静态库文件,并使用`-lstdc++`选项链接C++标准库: ``` $ g++ sample.cpp sample.o -lstdc++ ``` 运行程序,可以得到如下输出: ``` C++ function: 9 Fortran function: 9 ``` 说明我们成功地实现了FortranC++的混编。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值