fortran module 和主程序一起编译

昨天尝试了一下, 写了一个单独的module,然后再写一个主程序,然后尝试怎么把两个链接起来。 首先我们将正确的做法放在前面,然后,我们将试验的过程写在后面。

正确做法

方法一:分别编译,再链接

gfortran -c tongji.f90 #首先编译第一个代码,生成tongji.o

gfortran -c torch_bridge_fortran.f90 #然后编译第二个代码,生成torch_bridge_fortran.o

gfortran -o bridge tongji.o torch_bridge_fortran.o#最后将.o 文件链接生成可执行程序。

方法二:直接把所有文件加上

gfortran -o bridge tongji.f90 torch_bridge_fortra.f90



代码介绍

第一个文件叫tongji.f90,这是module文件

        module  tongji


        implicit none

        private

        public mean
       
        contains

        subroutine mean(x,n,x_mean)

                integer  :: n 
                integer  :: i 
                real     :: x(n)
                real     :: x_mean

                x_mean = 0.0

                do i =1,n
                x_mean = x_mean + x(i)
                end do

                x_mean = x_mean/n

        end subroutine mean


        end module tongji

第二个文件叫torch_bridge_fortran.f90,这个文件use 了 tognji

      program  bridge 

        use tongji, only:mean

        integer  :: cishu = 10
        real     :: wendu(10)
        real     :: wendu_pingjun

        wendu = (/32,23,25,24,29,30,30,30,30,21/)

        print*,wendu

        call mean(wendu,cishu,wendu_pingjun)
 
        print*,wendu_pingjun

      end program bridge


好,我们下面进行试验

编译试验

首先只编译主文件

gfortran -o bridge  torch_bridge_fortran.f90

结果

[chengxl@login02 torch_bridge_fortran]$ gfortran -o bridge  torch_bridge_fortran.f90
torch_bridge_fortran.f90:2.12:

        use tongji, only:mean
            1
Fatal Error: Can't open module file 'tongji.mod' for reading at (1): No such file or directory

出现了Fatal Error: Can't open module file 'tongji.mod' for reading at (1): No such file or directory的错误。

说明这个module并没有被发现,这是编译没有链接的原因。

编译module 但是不连接

[chengxl@login02 torch_bridge_fortran]$ gfortran -c tongji.f90 && ls tongji.*
tongji.f90  tongji.mod  tongji.o

我们看见在编译tongji.f90 后出现了tongji,o , tongji,mod。

在这种情况下不连接,仅仅编译torch_bridge_fortran.f90,看看会出现什么结果?

[chengxl@login02 torch_bridge_fortran]$ gfortran -c tongji.f90 && gfortran -o bridge torch_bridge_fortran.f90 /tmp/ccXI6lsD.o: In function `MAIN__':
torch_bridge_fortran.f90:(.text+0xfc): undefined reference to `__tongji_MOD_mean'
collect2: error: ld returned 1 exit status

出现了错误undefined reference to `__tongji_MOD_mean'

以上是没有链接的两种常见错误。

编译且连接

正确的方法有两种

第一种方法

[chengxl@login02 torch_bridge_fortran]$ gfortran -c tongji.f90 && gfortran -c torch_bridge_fortran.f90 && gfortran -o bridge tongji.o torch_bridge_fortran.o &&ls
bridge      tongji.o                  yanjiurizhi.txt
tongji.f90  torch_bridge_fortran.f90
tongji.mod  torch_bridge_fortran.o

gfortran -c tongji.f90

gfortran -c torch_bridge_fortran.f90

gfortran -o bridge tongji.o torch_bridge_fortran.o

这样就生成了 bridge 可运行程序

第二种方法

[chengxl@login02 torch_bridge_fortran]$ gfortran -o bridge  tongji.o  torch_bridge_fortran.f90

gfortran -c tongji.f90

gfortran -o bridge tongji.o torch_bridge_fortran.f90

这样也生成了 bridge 可运行程序

第三种方法

[chengxl@login02 torch_bridge_fortran]$ gfortran -o bridge tongji.f90 torch_bridge_fortran.f90

gfortran -o bridge tongji.f90 torch_bridge_fortran.f90

这样也能直接生成bridge可运行程序

其他方法

在小项目中,这样就可以了,但是在很多大的项目中,会使用一些工具比如cmake ,gmake等等,这里不再详细介绍。

 

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值