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.

      

  • 相关阅读:
    Kafka日志及Topic数据清理
    python
    kotlin集合操作
    tomcat 下配置 可 调试
    linux 安装nexus3
    启动 idea 编译报错 kotlin
    nginx 增加 lua模块
    logstash配合filebeat监控tomcat日志
    redis 高级特性 不要太好用
    SpringBoot与Docker1
  • 原文地址:https://www.cnblogs.com/greencolor/p/2102007.html
Copyright © 2011-2022 走看看