4

Could you tell me please how can I do the following:

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

using gfortran compiler.

Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47
  • 2
    gfortran's manual contains instructions on how to use the preprocessor. Is there anything about them that's not clear to you? – eriktous Dec 26 '11 at 17:27

1 Answers1

9

This can be done in combination with conditional compilation and using the "D" option on the command line. Here is some example 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 filetype F90 to cause gfortran to run the preprocessor or use -cpp on the compile line. Then pass options to the prepreprocessor by including them after D on the compile line, e.g., gfortran -DWOS. (This is more general then gfortran -- most Fortran compilers will process C-style pre-processor directives.) Then you can identify the OS outside of Fortran and pass the information to the Fortran program.

You can compile your code via using the filetype F90 or -cpp.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • `__unix__` and `_WIN32` are the usual predefined preprocessor flags used to indicate which system you are on in most compilers. – Chris Jan 08 '12 at 17:15