• mix c with fortran


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

    The C program is nothing out of the ordinary: it defines two variables, and calls various functions that change those variables' values. C requires that we use a "call by reference" syntax to make these changes persistent, rather than its default "call by value" method. Note that the name of the FORTRAN function called from the C program is ffunction_, a name we extracted via the nm command shown above. Note also that the C++ function has an extern "C" directive above the code of the function, indicating not that cppfunction() is written in C, but that it is called from a C-style interface instead of a C++ interface.

    File cprogram.c:

      #include <stdio.h>

      int main(void) {

        float a=1.0, b=2.0;

        printf("Before running Fortran function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        ffunction_(&a,&b);

        printf("After running Fortran function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        printf("Before running C++ function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        cppfunction(&a,&b);

        printf("After running C++ function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        printf("Before running C function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        cfunction(&a,&b);

        printf("After running C function:\n");

        printf("a=%f\n",a);

        printf("b=%f\n",b);

        return 0;

      }

    File ffunction.f:

          subroutine ffunction(a,b)

          a=3.0

          b=4.0

          end

    File cppfunction.C:

      extern "C" {

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

      }

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

        *a=5.0;

        *b=6.0;

      }

    File cfunction1.c:

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

        *a=7.0;

        *b=8.0;

      }

    Compilation Steps: each program is compiled into an object file using the appropriate compiler with the -c flag. After all the object files are created, the final gcc command links the object files together into a single executable:

        gcc -c cprogram.c

        g77 -c ffunction.f

        g++ -c cppfunction.C

        gcc -c cfunction1.c

        gcc -o cprogram cprogram.o ffunction.o cppfunction.o cfunction1.o

      

    Though this example problem does not require it, many of the math functions (for example, sin, cos, pow, etc.) require that you also link in the libm math library. Add a -lm flag to the final gcc command above to link in the math library.

    Results:

       ./cprogram

      Before running Fortran function:

      a=1.000000

      b=2.000000

      After running Fortran function:

      a=3.000000

      b=4.000000

      Before running C++ function:

      a=3.000000

      b=4.000000

      After running C++ function:

      a=5.000000

      b=6.000000

      Before running C function:

      a=5.000000

      b=6.000000

      After running C function:

      a=7.000000

      b=8.000000

     

  • 相关阅读:
    antd vue 刷新保留当前页面路由,保留选中菜单,保留menu选中
    你不知道的arguments
    js触发复制、粘贴,设置和读取剪切板的数据
    vue父组件data改变触发子组件prop值变化
    关于英语的unless
    PHP闭包调用外部参数使用范例
    全国省市区数据库数据,以及可视化echarts相关geoJson数据
    PHP开发环境搭建&mdash;phpstudy+eclipse+php development tool+xdebug+composer
    LBS——实现附近功能的几种方案浅谈
    好文共赏
  • 原文地址:https://www.cnblogs.com/greencolor/p/2101979.html
Copyright © 2020-2023  润新知