zoukankan      html  css  js  c++  java
  • mix fortran with c

    Example 3: Main Program in FORTRAN, with Subroutines in C, C++, and Fortran

    Though the non-FORTRAN subroutines don't have any underscores after their names in the main FORTRAN program, running the nm command on fprogram.o shows that the FORTRAN program expects that they'll have underscores appended to the function name internally. Therefore, in both the C and C++ files, we need to write the function prototype statement accordingly, with an underscore after the function name. We also must use the extern "C" directive in the C++ file, indicating that the C++ function is called with a C-style interface, similar to the first example.

    File fprogram.f:

          program fprogram

          real a,b

          a=1.0

          b=2.0

          print*,'Before Fortran function is called:'

          print*,'a=',a

          print*,'b=',b

          call ffunction(a,b)

          print*,'After Fortran function is called:'

          print*,'a=',a

          print*,'b=',b

          print*,'Before C function is called:'

          print*,'a=',a

          print*,'b=',b

          call cfunction(a,b)

          print*,'After C function is called:'

          print*,'a=',a

          print*,'b=',b

          print*,'Before C++ function is called:'

          print*,'a=',a

          print*,'b=',b

          call cppfunction(a,b)

          print*,'After C++ function is called:'

          print*,'a=',a

          print*,'b=',b

          stop

          end

    File ffunction.f:

          subroutine ffunction(a,b)

          a=3.0

          b=4.0

          end

    File cppfunction2.C:

        extern "C" {

          void cppfunction_(float *a, float *b);

        }

        void cppfunction_(float *a, float *b) {

          *a=5.0;

          *b=6.0;

        }

    File cfunction2.c:

        void cfunction_(float *a, float *b) {

          *a=7.0;

          *b=8.0;

        }

    Compilation Steps:

        g77 -c fprogram.f

        g77 -c ffunction.f

        g++ -c cppfunction2.C

        gcc -c cfunction2.c

        g77 -lc -o fprogram fprogram.o ffunction.o cppfunction2.o cfunction2.o

        

    Results:

         ./fprogram

         Before Fortran function is called:

         a=  1.

         b=  2.

         After Fortran function is called:

         a=  3.

         b=  4.

         Before C function is called:

         a=  3.

         b=  4.

         After C function is called:

         a=  7.

         b=  8.

         Before C++ function is called:

         a=  7.

         b=  8.

         After C++ function is called:

         a=  5.

         b=  6.

      

  • 相关阅读:
    谷歌浏览器调试
    建立标准编码规则(五)-工程结构
    Ruby页面,循环赋值方法(类似java EL表达式赋值)
    ruby--Hash方法汇总
    ant design环境搭建过程中遇到的问题--Windows-dva-cli
    Linux下Nodejs安装(完整详细)
    linux chmod命令使用
    EL表达式获取日期时间类型后格式化的问题
    百度网盘视频资源
    Eclipse插件:mybatis generator的使用步骤
  • 原文地址:https://www.cnblogs.com/greencolor/p/2102007.html
Copyright © 2011-2022 走看看