Eastsheng's Wiki

Fortran Tutorial

2021-06-28 10:51:28

[toc]

Fortran教程

call Fortran from C:参考:https://sites.google.com/site/kaizhangstatmech/code/callfortran

  • fortrancode.f

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
         subroutine fortfunc(ii,ff)

    integer ii

    real*4 ff

    write(6,100) ii, ff

    100 format('ii=',i2,' ff=',f6.3)

    return

    end
  • cppcode.c

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include <iostream>

    using namespace std;

    extern"C" {

    void fortfunc_(int *ii, float *ff);

    }

    main()

    {

    int ii=5;

    float ff=5.5;

    fortfunc_(&ii, &ff);

    return 0;

    }
  • compile as

    1
    2
    3
    4
    5
    gfortran -c fortrancode.f

    g++ -c cppcode.c

    g++ -o run fortrancode.o cppcode.o -lgfortran
  • or

    1
    2
    3
    4
    5
    f77 -c fortrancode.f

    g++ -c cppcode.c

    g++ -o run fortrancode.o cppcode.o -lg2c