Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

preprocessor - Gfortran pre-processor directives for Different Operating systems

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...