Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在GNU倍频程内运行Fortran程序?_Fortran_Octave - Fatal编程技术网

如何在GNU倍频程内运行Fortran程序?

如何在GNU倍频程内运行Fortran程序?,fortran,octave,Fortran,Octave,我想在八度内运行Fortran程序。我想这样做的目的是自动化,并使用所有的数据处理倍频程 可以用cygwin从八度音阶运行Fortran程序吗?如果可以的话,你能提供一些关于这个方向的指针吗 此外,我的系统中安装了gfortran编译器,有没有办法利用它来完成上述任务 此外,我尝试使用mex执行相同的操作: mckoctfile --mex HelloWorld.f 在尝试mex方法后,我出现以下错误: c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x

我想在八度内运行Fortran程序。我想这样做的目的是自动化,并使用所有的数据处理倍频程

可以用cygwin从八度音阶运行Fortran程序吗?如果可以的话,你能提供一些关于这个方向的指针吗

此外,我的系统中安装了gfortran编译器,有没有办法利用它来完成上述任务

此外,我尝试使用mex执行相同的操作:

mckoctfile --mex HelloWorld.f
在尝试mex方法后,我出现以下错误:

c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rajan\AppData\Local\Temp/oct-qur1RF.o: in function `hi': C:\Tech Stuff\Fortran Programs/HelloWorld.f:3: undefined reference to `_gfortran_st_write'
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Tech Stuff\Fortran Programs/HelloWorld.f:3: undefined reference to `_gfortran_transfer_character_write'
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Tech Stuff\Fortran Programs/HelloWorld.f:3: undefined reference to `_gfortran_st_write_done'
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rajan\AppData\Local\Temp/oct-qur1RF.o: in function `main':C:\Tech Stuff\Fortran Programs/HelloWorld.f:6: undefined reference to `_gfortran_set_args'
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Tech Stuff\Fortran Programs/HelloWorld.f:6: undefined reference to `_gfortran_set_options'
collect2.exe: error: ld returned 1 exit status
warning: mkoctfile: building exited with failure sta

我该如何解决这个错误才能继续前进?

显然,您的特定用例可能比这复杂得多,但这里有一个简单的示例可以帮助您开始(或者帮助您决定是否值得沿着这条路线走下去……)

让我们从一个简单的八进制文件开始,它执行简单的整数加法,目前不涉及fortran

// in: simple_addition.cpp 
#include <octave/oct.h>

DEFUN_DLD (simple_addition, args, ,"Add two integers via C++")
{
  octave_value retval = args(0).int_value() + args(1).int_value();
  return retval;
}
八度:

octave:1> simple_addition(1,2)
ans =  3

现在让我们把它放在一边,看看我们如何从纯C++调用FORTRAN定义函数。首先,让我们创建一个简单的整数加法函数:

 ! in fortran_addition.f90 
 function fortran_addition(a,b) result(Out)
    integer, intent(in) :: a,b ! input
    integer             :: Out ! output
    Out = a + b
 end function fortran_addition
// in fortran_addition_wrapper.cpp
#include <octave/oct.h>
extern "C" { int fortran_addition_( int *, int *); }
DEFUN_DLD (fortran_addition_wrapper, args, ,"Add two integers via fortran")
{
  int a, b, fortran_result;
  a = args(0).int_value();
  b = args(1).int_value();
  fortran_result = fortran_addition_( &a, &b );
  octave_value retval(fortran_result);
  return retval;
}
并使用gfortran进行编译:

gfortran -c fortran_addition.f90    # creates fortran_addition.o
您可以看到(例如,使用
nm fortran\u addition.o
)生成的对象包含对名为
fortran\u addition\u
下的符号的引用(请注意末尾添加的下划线)

现在让我们创建一个正常的(即非八度相关)C++包装程序,它调用通过这个符号定义的函数:

// in generic_fortran_addition_wrapper.cpp
#include <iostream>
extern "C" { int fortran_addition_( int *, int * ); }
int main() {
    int a = 1, b = 2, fortran_result;
    fortran_result = fortran_addition_( &a, &b );
    std::cout << a << " + " << b << " = " << fortran_result << std::endl;
}
现在,我们具备了创建一个封装fortran函数的octfile的所有要素:

 ! in fortran_addition.f90 
 function fortran_addition(a,b) result(Out)
    integer, intent(in) :: a,b ! input
    integer             :: Out ! output
    Out = a + b
 end function fortran_addition
// in fortran_addition_wrapper.cpp
#include <octave/oct.h>
extern "C" { int fortran_addition_( int *, int *); }
DEFUN_DLD (fortran_addition_wrapper, args, ,"Add two integers via fortran")
{
  int a, b, fortran_result;
  a = args(0).int_value();
  b = args(1).int_value();
  fortran_result = fortran_addition_( &a, &b );
  octave_value retval(fortran_result);
  return retval;
}
然后是八度:

octave:1> fortran_addition_wrapper(1,2)
ans =  3

话虽如此,显然,如果您有一个完全定义的fortran程序,而不仅仅是可链接的函数,并且您的系统上有一个正在运行的编译可执行文件,那么您可以跳过上述所有“手续”,只需通过octave的
system()
命令调用可执行文件即可。显然,在这个场景中,由您以八度不可知的方式传递数据。。。但是,假设您有一个独立的fortran可执行文件,那么它可能已经有了从操作系统读取输入数据的方法


编辑根据下面的评论,我被提醒我被跟踪并回答了原始问题评论中提出的问题,忘记了解决原始问题中的错误消息。正如我在评论中提到的,mkoctave是gnu编译器集合的通用包装器。这些消息听起来并不特定于octave,而是编译器/链接器抱怨您缺少定义这些基本函数的fortran运行库。

显然,您的特定用例可能比这复杂得多,但这里有一个简单的示例让您开始(或者帮助你决定是否值得走这条路……)

让我们从一个简单的八进制文件开始,它执行简单的整数加法,目前不涉及fortran

// in: simple_addition.cpp 
#include <octave/oct.h>

DEFUN_DLD (simple_addition, args, ,"Add two integers via C++")
{
  octave_value retval = args(0).int_value() + args(1).int_value();
  return retval;
}
八度:

octave:1> simple_addition(1,2)
ans =  3

现在让我们把它放在一边,看看我们如何从纯C++调用FORTRAN定义函数。首先,我们创建一个简单的整数加法函数:

 ! in fortran_addition.f90 
 function fortran_addition(a,b) result(Out)
    integer, intent(in) :: a,b ! input
    integer             :: Out ! output
    Out = a + b
 end function fortran_addition
// in fortran_addition_wrapper.cpp
#include <octave/oct.h>
extern "C" { int fortran_addition_( int *, int *); }
DEFUN_DLD (fortran_addition_wrapper, args, ,"Add two integers via fortran")
{
  int a, b, fortran_result;
  a = args(0).int_value();
  b = args(1).int_value();
  fortran_result = fortran_addition_( &a, &b );
  octave_value retval(fortran_result);
  return retval;
}
并使用gfortran进行编译:

gfortran -c fortran_addition.f90    # creates fortran_addition.o
您可以看到(例如,使用
nm fortran\u addition.o
)生成的对象包含对名为
fortran\u addition\u
下的符号的引用(请注意末尾添加的下划线)

现在让我们创建一个正常的(即非八度相关)C++包装程序,它调用通过这个符号定义的函数:

// in generic_fortran_addition_wrapper.cpp
#include <iostream>
extern "C" { int fortran_addition_( int *, int * ); }
int main() {
    int a = 1, b = 2, fortran_result;
    fortran_result = fortran_addition_( &a, &b );
    std::cout << a << " + " << b << " = " << fortran_result << std::endl;
}
现在,我们具备了创建一个封装fortran函数的octfile的所有要素:

 ! in fortran_addition.f90 
 function fortran_addition(a,b) result(Out)
    integer, intent(in) :: a,b ! input
    integer             :: Out ! output
    Out = a + b
 end function fortran_addition
// in fortran_addition_wrapper.cpp
#include <octave/oct.h>
extern "C" { int fortran_addition_( int *, int *); }
DEFUN_DLD (fortran_addition_wrapper, args, ,"Add two integers via fortran")
{
  int a, b, fortran_result;
  a = args(0).int_value();
  b = args(1).int_value();
  fortran_result = fortran_addition_( &a, &b );
  octave_value retval(fortran_result);
  return retval;
}
然后是八度:

octave:1> fortran_addition_wrapper(1,2)
ans =  3

话虽如此,显然,如果您有一个完全定义的fortran程序,而不仅仅是可链接的函数,并且您的系统上有一个正在运行的编译可执行文件,那么您可以跳过上述所有“手续”,通过
system()调用可执行文件
来自octave的命令。显然,在这种情况下,由您以一种与octave无关的方式传递数据……但假设您有一个独立的fortran可执行文件,那么它可能已经有了从操作系统读取输入数据的方法


编辑根据下面的评论,我被提醒,我被跟踪并回答了原始问题评论中提出的问题,而忘记了解决原始问题中的错误消息。正如我在评论中提到的,mkoctave是gnu编译器集合的通用包装器。这些都是我的消息听起来并不特定于倍频程,而是编译器/链接器抱怨您缺少定义这些基本函数的fortran运行时库。

这不是我的很多经验,但出于好奇,与matlab的兼容性听起来不是一个很大的问题;为什么选择MexFile而不是更多本机支持的octfiles接口?您的错误消息听起来更像是没有链接到正确的fortran库,而不像与octave有关的内容。例如,请参阅。请注意,mkoctfile本质上是gnu编译器的包装器,为了方便起见,它会加载一组与octave相关的库,但您仍然可以传递usu您通常会传递给gnu编译器的所有选项和参数。我是第一次这样做。因此,我查阅了wen并继续使用MEX文件。然后我如何使用octfiles来代替?任何指向该方向的指针都将不胜感激。感谢最好的开始位置是在线手册;是关于exter的部分吗nal代码接口(包括fortran)。我查看了它,但什么都看不懂。我对它没有太多经验,但出于好奇,它听起来不像与matlab兼容是一个很大的问题;为什么选择mexfiles而不是更受本机支持的octfiles接口?您的错误消息听起来更像不是lin