Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28455 Discussions

Check for operating system in Fortran?

Wee_Beng_T_
Beginner
1,574 Views
Hi,

In my code, depending on the operating system (OS), I run different stuffs.

Is there a way to do it automatically thru the if...else syntax?

e.g. if (OS=="win7") then
...

else if (OS=="linxu") then
...

end if

Thanks and Merry Christmas!
0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,574 Views
My apologies. Use this form instead:

!DEC$ IF DEFINED(_WIN32)
... Windows
!DEC$ ELSEIF DEFINED(__linux)
... Linux
!DEC$ ELSE
... Oops
!DEC$ ENDIF

View solution in original post

6 Replies
Steven_L_Intel1
Employee
1,574 Views
Since you have to build the program separately, use conditional compilation and the predefined symbols. For example:

!DEC$ IF (_WIN32)
... Windows
!DEC$ ELSEIF (__linux)
... Windows
!DEC$ ELSE
... Mac?
!DEC$ END IF

If you want to detect different Windows versions, see the provided sample Win32\Platform.
0 Kudos
Wee_Beng_T_
Beginner
1,574 Views
Sorry, can you explain more clearly?

I tried to add into my fortran code using:

!DEC$ IF (__linux)

call hypre_deallocate

!DEC$ END IF

but I got the error msg:

remark #5170: Invalid conditional compilation expression component
!DEC$ IF (1)
------^

Thanks again.
0 Kudos
Steven_L_Intel1
Employee
1,575 Views
My apologies. Use this form instead:

!DEC$ IF DEFINED(_WIN32)
... Windows
!DEC$ ELSEIF DEFINED(__linux)
... Linux
!DEC$ ELSE
... Oops
!DEC$ ENDIF
Wee_Beng_T_
Beginner
1,574 Views
Hi,

Thanks for the clarifications. Now there's no error, but even if I use:

!DEC$ IF DEFINED(_WIN32)

in linux, the statement below still gets executed.

I thought that it should be not so if my OS is linux.
0 Kudos
Steven_L_Intel1
Employee
1,574 Views
That is not the way it is supposed to work. Please show a small test program and then a log of your building it on Linux, then running it. You aren't trying to run a Windows program under an emulator such as Wine, are you?
0 Kudos
Wee_Beng_T_
Beginner
1,574 Views
Ops, made a careless mistake. It worked, thanks for the Christmas present ;-)
0 Kudos
Reply