Debugging Gfortran:为了调试目的,将纯函数视为普通函数?
我需要在用gfortran编译的fortran程序中调试一些纯函数。有没有办法忽略Debugging Gfortran:为了调试目的,将纯函数视为普通函数?,debugging,fortran,gfortran,Debugging,Fortran,Gfortran,我需要在用gfortran编译的fortran程序中调试一些纯函数。有没有办法忽略pure语句,这样我就可以在这些pure函数中不费吹灰之力地使用write、print等? 不幸的是,仅仅删除纯语句并不容易。我通常使用预处理器执行此任务: #ifdef DEBUG subroutine test(...) #else pure subroutine(...) #endif ! ... #ifdef DEBUG write(*,*) 'Debug output' #endif ! ..
pure
语句,这样我就可以在这些pure
函数中不费吹灰之力地使用write
、print
等?
不幸的是,仅仅删除
纯语句并不容易。我通常使用预处理器执行此任务:
#ifdef DEBUG
subroutine test(...)
#else
pure subroutine(...)
#endif
! ...
#ifdef DEBUG
write(*,*) 'Debug output'
#endif
! ...
end subroutine
然后,您可以使用gfortran-DDEBUG
编译代码以获得详细的输出。(事实上,我个人并不全局设置此标志,而是通过我想要调试的文件开头的#define DEBUG
)
我还定义了一个宏,以便于调试write语句:
#ifdef DEBUG
#define dwrite write
#else
#define dwrite ! write
#endif
这样,上述代码将简化为:
#ifdef DEBUG
subroutine test(...)
#else
pure subroutine(...)
#endif
! ...
dwrite (*,*) 'Debug output'
! ...
end subroutine
对于gfortran
,可以使用-cpp
启用前置处理器;对于ifort
,可以使用-fpp
启用前置处理器。当然,当使用.F90
或.F
时,默认情况下会启用预处理器 您可以使用宏并使用-cpp
标志
#define pure
pure subroutine s
print *,"hello"
end
我刚刚在编译命令中添加了标志-cpp
和-Dpure=
。真是个好主意!