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.

      

  • 相关阅读:
    用遗传算法解决子集和问题
    XML映射配置文件
    generator插件配置方式使用
    声明
    spring IOC简单分析
    模板模式
    原型模式
    委派模式,策略模式
    单例模式2
    单例模式
  • 原文地址:https://www.cnblogs.com/greencolor/p/2102007.html
Copyright © 2011-2022 走看看