我希望更改Fortran 90代碼中的工作目錄。有沒有可能以非編譯器特定的方式做到這一點?這裏是我的代碼:以非編譯器特定方式更改Fortran中的目錄
program change_directory
integer :: ierr
call system("mkdir -p myfolder/")
!call system("cd myfolder/") !doesn't work
ierr = chdir("myfolder")
if (ierr.NE.0) then
write(*,'(A)') "warning: change of directory unsuccessful"
end if
open(unit=33,file="myfile.txt",iostat=ierr)
if (ierr.EQ.0) then
write(unit=33,fmt='(A)') "Test message"
close(unit=33)
end if
end program change_directory
顯然,在一個系統調用使用cd myfolder/
不起作用。 Intel reference表示我需要添加'use ifport
'。儘管如此,GCC reference中沒有這樣的提及。留下'use ifport
',我可以毫無困難地編譯ifort
以上的代碼。然而,當我把它放入時,它不會用gcc編譯(因爲gcc沒有ifport
模塊) - 不僅如此,它也不會在Intel Fortran下編譯 - 我得到以下錯誤:
$ ifort change_dir.f90 -o change_dir
change_dir.f90(5): error #6552: The CALL statement is invoking a function subprogram as a subroutine. [SYSTEM]
call system("mkdir -p myfolder/")
---------^
compilation aborted for change_dir.f90 (code 1)
所以我的問題是:有沒有更好的方法來做到這一點?我想保持我的代碼儘可能獨立於編譯器。目前,我主要使用gfortran/ifort和mpif90/mpiifort。