About preprocessing of FORTRAN code

About preprocessing of FORTRAN code

You can use preprocessing to activate different code sections as needed. E.g.:

Main.F:

#define debug

#ifdef debug

    write(*,*) "hello 1"

#else

    write(*,*) "hello 2"

#endif

By default, the directives begin with # will be treated as comments. If you compile this file directly by ifort Main.F, you will get the following code in Main.for:

    write(*,*) "hello 2"

To active the preprocess directives, you need to add the option of -fpp at compilation step. Here fpp stands for “FORTRAN preprocessor”.

ifort main.F -fpp

or if you use MS visual studio, turn on fpp:

click Project->Console properties:


After compilation, you will get the following code in main.for:

    write(*,*) "hello 1"

if you want to “#define debug” at compilation step instead of hardcoding. You can:

#ifdef debug

    write(*,*) "hello 1"

#else

    write(*,*) "hello 2"

#endif

WIN: ifort Main.F -fpp /Ddebug or Linux: ifort Main.F -fpp -Ddebug

or manually add option “debug” in MS Visual studio as below.


After compilation, you will get the following code in main.for:

    write(*,*) "hello 1"

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容