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

getenv and setenv subroutines in Fortran

Miah__Wadud
Beginner
2,487 Views

Does the Intel Fortran compiler have Fortran wrappers for getenv and setenv subroutines? If so, which link flag should I use to obtain them?

0 Kudos
7 Replies
TimP
Honored Contributor III
2,487 Views

If you consult the documentation which comes with ifort, you will see that getenv requires USE IFPORT (which doesn't involve any link options).  Likewise, getenvqq and setenvqq may be looked up via ifport library overview.

0 Kudos
Izaak_Beekman
New Contributor II
2,487 Views

There is also the standard compliant (as of F2003) intrinsic subroutine `get_environment_variable(…)` but there is no standard compliant means of setting an environment variable, as far as I know.

0 Kudos
TimP
Honored Contributor III
2,487 Views

call execute_command_line would at least permit setting environment variables using different Fortran compilers running under similar OS shells.  I think several people filed requests for this feature since f2008 standard came out, and a DPD issue number DPD200137238 was quoted on this forum over year ago.  My premier issue 628820 is 4 years old. Primary alternative Fortrans have supported it for 2 years or more.

0 Kudos
Izaak_Beekman
New Contributor II
2,487 Views

I would very much like to see this added to the standard.

0 Kudos
jimdempseyatthecove
Honored Contributor III
2,487 Views

Tim,

The call execute_command_line would at least permit setting environment variables does work when the command line is executed inside the same shell context as the Fortran program. It would be open to the implementation as to if the execute_command_line spawned a new command shell, inheriting current environment on entry, discarding on exit. IOW, while this works today, it may not work some time later.

Jim Dempsey

0 Kudos
JVanB
Valued Contributor II
2,487 Views

If the Fortran processor implements getenv via a mechanism like C's char **envp, setenv would not necessarily work as seen by the Fortran program because it might only be looking at the state of the environmental variables at the time the program loaded, rather than the state current when getenv is invoked. This is the case in gfortran at least, when changes made by setting environmental variables via API calls at runtime are invisible to getenv.

0 Kudos
Steven_L_Intel1
Employee
2,487 Views

Fortran has the standard intrinsic GET_ENVIRONMENT_VARIABLE, which is getenv. Intel Fortran supports this. There is no corresponding intrinsic for setenv. (But as noted above we support both setenv and getenv with USE IFPORT.)

Note that EXECUTE_COMMAND_LINE won't help you as any changes made in that context are local to the spawned process.

0 Kudos
Reply