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.

      

  • 相关阅读:
    实现两个窗口通信方法-postMessage
    Java中的参数传值方式
    数据库连接池(connection pool)
    批量处理JDBC语句提高处理速度
    数据库事务,隔离级别
    BeanUtils介绍及使用
    JDBC获得数据库连接及使用
    jquery radio 行选中 操作
    EXTJS4.0 grid 可编辑模式 配置
    sql server 中使用 LIKE 语句 SqlParameter 使用
  • 原文地址:https://www.cnblogs.com/greencolor/p/2102007.html
Copyright © 2011-2022 走看看