我有一个fortran程序调用C函数并使用open()打开一个文件
main.f90时:
PROGRAM TEST
integer :: oflag, mode
!Set oflag to O_CREAT|O_RDWR
oflag = 66
mode = 600
call test2("test.txt", oflag, mode)
END PROGRAM
Run Code Online (Sandbox Code Playgroud)
test.c的:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#pragma weak test2_ = test2
#pragma weak test2__ = test2
#pragma weak TEST2 = test2
void test2(char* filename, int* flag, int* mode)
{
int fd;
if(-1 == (fd = open(filename, *flag, *mode)))
puts("Returned -1");
}
Run Code Online (Sandbox Code Playgroud)
我编译为:
gcc -c test.c
gfortran main.f90 test.o
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,它会创建文件test.txt,但权限不正确:
---x--x--T 1 xyz users …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何从 Windows 上的 fortran 可执行文件调用 fortran dll 中的函数。我正在 Eclipse 中使用 gfortran 4.7 和 photran。
我的测试 dll 在 hello.f90 中有一个函数:
你好.f90
subroutine hello
implicit none
print *, "Hello World!"
end subroutine hello
Run Code Online (Sandbox Code Playgroud)
使用以下生成文件:
all:
gfortran -Wall -c hello.f90
gfortran -shared -o hello.dll hello.o
Run Code Online (Sandbox Code Playgroud)
Dependency Walker 确认已导出函数“hello_”。
现在我正在尝试构建一个动态调用它的程序。我根据在网上找到的示例构建了以下内容,但无法编译:
main.f90
program main
implicit none
integer :: p
pointer (q, hello)
p = loadlibrary("hello.dll")
q = getprocaddress(p, "hello_")
call hello
end program main
Run Code Online (Sandbox Code Playgroud)
生成文件
all:
gfortran -Wall -pedantic -fcray-pointer main.f90
Run Code Online (Sandbox Code Playgroud)
错误消息是函数 LoadLibrary(和 getprocaddress)没有 IMPLICIT …
看一下gfortran随机数发生器播种的示例代码,我对这里的时间转换感到困惑:
call date_and_time(values=dt)
tms = (dt(1) - 1970) * 365_8 * 24 * 60 * 60 * 1000 &
+ dt(2) * 31_8 * 24 * 60 * 60 * 1000 &
+ dt(3) * 24 * 60 * 60 * 60 * 1000 &
+ dt(5) * 60 * 60 * 1000 &
+ dt(6) * 60 * 1000 + dt(7) * 1000 &
+ dt(8)
t = transfer(tms, t)
Run Code Online (Sandbox Code Playgroud)
我很好奇,为什么365和31有_8尾. …
过去两天我在一个大型 Fortran 项目中调试了一个看似无意义的段错误。当我将代码移到自己的计算机上时,问题就开始了,段错误出现在代码的一部分中,而该部分代码多年来在其他几个系统上都运行良好。我最终找到了段错误的根源,但它是如此令人惊讶的意外(并且依赖于编译器),所以我决定将其发布在这里。
考虑以下 MWE:
program dafuq
implicit none
integer :: a=1
integer, parameter :: b=2
call foo(a,b)
end program dafuq
subroutine foo(a,b)
implicit none
integer, intent(inout) :: a, b
a=b !OK
b=a !causes segfault
end subroutine foo
Run Code Online (Sandbox Code Playgroud)
我可以访问两个 HPC 集群,它们与我的笔记本电脑一起允许我检查这些(有时有点旧)编译器:
事实证明,所有四个编译器都会对上述代码产生段错误,因为变量b被声明为parameter. 因此,在子例程中更改其值是违规的。intent我的问题是,只有最新的 gfortran 在编译期间显示警告(即使使用 -Wall),如果我省略子例程中的规范,该警告也会消失。我怀疑在 C++ 中使用const变量的相同设置会引发一个巨大的危险信号。
现在,为了使其更加晦涩,请考虑以下代码,其中使用数组而不是标量:
program dafuq_array
implicit none
integer :: a(2)=(/1,1/)
integer, parameter :: b(2)=(/2,2/)
call foo(a,b)
end …Run Code Online (Sandbox Code Playgroud) 我有几十个f77函数的文件,我想将它们包含在我的f90程序中.我如何与gfortran一起带来它?如果我只是尝试
gfortran myprogram.f90
Run Code Online (Sandbox Code Playgroud)
它抱怨其他文件中的f77代码.据我所知,它希望看到所有文件都符合明确的标准(f90的f77).
我正在编写我的代码并在Fortran中使用输入和输出功能.代码看起来像这样(仅用于简化):
PROGRAM TEST
REAL, DIMENSION(1000):: A
REAL:: B
INTEGER::T
!Defining input and output
OPEN(UNIT=1, FILE='input.dat', STATUS='OLD')
OPEN(UNIT=2, FILE='output.dat', STATUS='NEW')
!Reading from file "input.dat"
READ(1,*) (A(I),I=1,1000)
!Just for initial condition
B=0.0
DO T=1, 10
PRINT *, 'Step =', T
DO I=1, 1000
B=B+1.0
A(I)=A(I)/B
END DO
END DO
!Writing results into file "output.dat"
DO I=1, 1000
WRITE (2,100) I, A(I)
END DO
100 FORMAT (' ',T3, I12, T17, F14.4)
END PROGRAM TEST
Run Code Online (Sandbox Code Playgroud)
我正在使用Gfortran 5.3,结果不像我期待的那样.我有望获得变量的结果T的画面(或终端在Ubuntu OS)上时该程序正在运行,并且变量I和 …
我想在 fortran90 中填充一个未知大小的数组。这是 MATLAB 中的等效代码:
for i=1:10
A[i] = i
end
Run Code Online (Sandbox Code Playgroud)
我知道我可以传递大小,但是如何在 fortran90 中执行此操作而不传递数组的大小。我读到我们可以使用指针,但我真的不知道如何处理指针
我刚接触 OpenMP,想知道这段代码有什么问题。它以串行方式工作。我正在使用 Ubuntu Linux 和 gfortran。
!
program test_rand
use omp_lib
implicit none
integer, parameter :: num_threads =36
integer*8,parameter :: nc = 1000000000
integer*8 ncirc,ncircs(0:35)
integer i,thread_num,istart,iend,ppt
real*8 x,y,dist,pi
integer,parameter :: seed = 864
call srand(seed)
do i=1,4
ncircs(i)=0
end do
ncirc=0
ppt=(nc+num_threads-1)/num_threads
istart=1
iend=nc
thread_num=1
!$ call omp_set_num_threads(num_threads)
!$omp parallel default(none) private(istart,iend,thread_num,i, &
!$omp dist,x,y,ncircs) shared(ppt,ncirc)
!$ thread_num = omp_get_thread_num()
!$ istart=thread_num*ppt+1
!$ iend = min(nc,thread_num*ppt+ppt)
print*,thread_num
do i=istart,iend
x=rand()
y=rand()
dist=sqrt((x-0.5)**2+(y-0.5)**2)
if (dist.le.0.5) ncircs(thread_num)=ncircs(thread_num)+1
end do
!$omp critical …Run Code Online (Sandbox Code Playgroud) 这个问题以前没有回答过.我正试图在Fortran中正确表示真实或任何数字.gfortran为我做的事情远没有.例如,当我声明变量REAL pi = 3.14159 fortran打印pi = 3.14159012而不是说3.14159000.见下文:
PROGRAM Test
IMPLICIT NONE
REAL:: pi = 3.14159
PRINT *, "PI = ",pi
END PROGRAM Test
Run Code Online (Sandbox Code Playgroud)
这打印:
PI = 3.14159012
Run Code Online (Sandbox Code Playgroud)
我可能已经预料到类似PI = 3.14159000的东西,因为REAL应该精确到至少8位小数.