Search code examples
operating-systemfortranfortran90intel-fortran

Identify operating system


My Fortran 90 code on Intel compiler depends on the operating system it is running on, e.g.

if (OS=="win7") then
   do X
else if (OS=="linux") then
   do y
end if

How do I do this programmatically?


Solution

  • You can use pre-processor directives for this task, see here and here for details:

    • _WIN32 for Windows
    • __linux for Linux
    • __APPLE__ for Mac OSX

    Here is an example:

    program test
    
    #ifdef _WIN32
      print *,'Windows'
    #endif
    #ifdef __linux
      print *,'Linux'
    #endif
    
    end program
    

    Make sure you enable the pre-processor by either specifying -fpp//fpp or given the file a capital F/F90 in the extension. You could do this in a central location, do define e.g. a constant describing the OS. This would avoid these Macros all over the place.

    Please note that no macro for Linux is specified by gfortran. As it still defines _WIN32 on Windows, you can alternatively use #else if you just consider Linux and Windows:

    program test
    
    #ifdef _WIN32
      print *,'Windows'
    #else
      print *,'Linux'
    #endif
    
    end program