Gfortran pre-processors for various operating systems

Could you tell me how I can do the following:

#if __unix__ #define path_sep='/' #elif __windows__ #define path_sep='\' #else #error "path_sep not defined." #endif 

using the gfortran compiler.

+4
fortran preprocessor
source share
1 answer

This can be done in conjunction with conditional compilation and using the "D" option on the command line. Here is a sample code:

 program test_Dopt character (len=1) :: pathsep pathsep = "?" #ifdef WOS pathsep = "\" #endif #ifdef UOS pathsep = "/" #endif write (*, '( "pathsep is >", A1, "<")' ) pathsep end program test_Dopt 

Name the program with the F90 file to force gfortran to run the preprocessor or use -cpp on the compilation line. Then pass the parameters to the preprocessor, including them after D in the compilation line, for example gfortran -DWOS. (This is more general than gfortran — most Fortran compilers handle C-style preprocessor directives.) Then you can define the OS outside Fortran and pass the information to Fortran.

You can compile your code using a file like F90 or -cpp.

+9
source share

All Articles