找回密码
 注册
查看: 3569|回复: 18

大哥,请问如何在VC中调用Fortran程序? (无内容)

[复制链接]
发表于 2003-6-5 09:49:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2003-6-5 09:53:15 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

望各位大侠不吝赐教!
发表于 2003-6-5 13:45:19 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

作成 dll 不就成了。
 楼主| 发表于 2003-6-5 14:30:33 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

macabre,Could you tell me how to do it?
发表于 2003-6-6 11:10:56 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

看来,vf和studio结合的的确非常紧密,vf支持编译 动态连接库(dll),静态连接库(lib)和com,这三种形式,其实都可以采用,当然各有优缺点。
你可以先试试在建立工程时选,dynamic link library (好象是这么写的吧)。
至于具体细节建议看看帮助中的例子。
有问题再说,(抱歉我现在出门在外,上网很不方便,)
发表于 2003-6-6 23:20:13 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

给一个最懒惰的方案: 用数据文件交换。
 楼主| 发表于 2003-6-7 08:01:41 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

是分别运行vc和fortran吗?通过数据文件的读写来实现两者的联系?
发表于 2003-6-20 18:32:25 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

VC可以直接调用VF的函数或子程序,如果你同时有这两个编译器安装在你的计算机上
 楼主| 发表于 2003-6-22 06:45:42 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

好像不行,编译通不过。
发表于 2003-6-22 19:10:14 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

注意函数调用时变量的对应关系。
发表于 2003-6-22 20:35:10 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

http://www.cae.tntech.edu/help/programming/mixed_languages
 楼主| 发表于 2003-6-23 21:58:10 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

郁闷,打不开该网页!
发表于 2003-6-23 22:23:17 | 显示全部楼层

大哥,请问如何在VC中调用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:
    mwr@ch208m:~$ gcc -c cprogram.c
    mwr@ch208m:~$ g77 -c ffunction.f
    mwr@ch208m:~$ g++ -c cppfunction.C
    mwr@ch208m:~$ gcc -c cfunction1.c
    mwr@ch208m:~$ gcc -o cprogram cprogram.o ffunction.o cppfunction.o cfunction1.o
    mwr@ch208m:~$
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:
  mwr@ch208m:~$ ./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
  mwr@ch208m:~$
Example 2: Main Program in C++, with Subroutines in C, C++, and FORTRAN
The only differences between the C++ code in a normal program is that we have to account for the C and FORTRAN subroutines expecting to be called with a C-style interface, and also that the FORTRAN compiler will append an underscore to the FORTRAN function name. Also, since the C++ function is held in a separate file from the C++ main program, we need to declare a function prototype before the main program code.
File cppprogram.C:
    #include <iostream.h>
    extern "C" {
      void ffunction_(float *a, float *b);
    }
    extern "C" {
      void cfunction(float *a, float *b);
    }
    void cppfunction(float *a, float *b);
    int main() {
      float a=1.0, b=2.0;
      cout << "Before running Fortran function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      ffunction_(&a,&b);
      cout << "After running Fortran function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      cout << "Before running C function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      cfunction(&a,&b);
      cout << "After running C function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      cout << "Before running C++ function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      cppfunction(&a,&b);
      cout << "After running C++ function:" << endl;
      cout << "a=" << a << endl;
      cout << "b=" << b << endl;
      return 0;
    }
File cfunction1.c:
    void cfunction(float *a, float *b) {
      *a=7.0;
      *b=8.0;
    }
File ffunction.f:
      subroutine ffunction(a,b)
      a=3.0
      b=4.0
      end
Compilation Steps:
    mwr@ch208m:~$ g++ -c cppprogram.C
    mwr@ch208m:~$ gcc -c cfunction1.c
    mwr@ch208m:~$ g++ -c cppfunction1.C
    mwr@ch208m:~$ g77 -c ffunction.f
    mwr@ch208m:~$ g++ -o cppprogram cppprogram.o cfunction1.o cppfunction1.o ffunction.o
    mwr@ch208m:~$
Results:
    mwr@ch208m:~$ ./cppprogram
    Before running Fortran function:
    a=1
    b=2
    After running Fortran function:
    a=3
    b=4
    Before running C function:
    a=3
    b=4
    After running C function:
    a=7
    b=8
    Before running C++ function:
    a=7
    b=8
    After running C++ function:
    a=5
    b=6
    mwr@ch208m:~$
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:
    mwr@ch208m:~$ g77 -c fprogram.f
    mwr@ch208m:~$ g77 -c ffunction.f
    mwr@ch208m:~$ g++ -c cppfunction2.C
    mwr@ch208m:~$ gcc -c cfunction2.c
    mwr@ch208m:~$ g77 -lc -o fprogram fprogram.o ffunction.o cppfunction2.o cfunction2.o
    mwr@ch208m:~$
Results:
    mwr@ch208m:~$ ./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.
    mwr@ch208m:~$
 楼主| 发表于 2003-6-23 22:31:52 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

thank u, duck!!!
 楼主| 发表于 2003-6-24 22:14:28 | 显示全部楼层

大哥,请问如何在VC中调用Fortran程序? (无内容)

duck兄,我在VC中(已装有VF)试了,将FORTRAN程序以文件形式加入到VC工程中,编译时出现如下提示:
Compiling Fortran...
Error spawning df.exe
这是怎么回事?如何解决?请教!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表