Linking to the HOSTNM function?


I am compiling a Fortran function for a Windows executable using the -Munix option which causes function names to be converted to lower case with an underscore appended.

Somehow this is causing a problem with the C 3F function “hostnm”.

I read the "C 3F Functions"section in the pgf77.htm document which I quote here:

The implementation of many of the routines uses functions which reside in the C library. If a C library does not contain the necessary functions, undefined symbol errors will occur at link-time. For example, if PGI 's C library is the C library available on the system, the following 3F routines exist in the Fortran run-time library but use of these routines will result in errors at link-time:

The ‘hostnm’ function is in the list that follows this quotation in the manual.

I don’t understand this statment, since in the line that immediately preceeds this quotation in the manual says:

These functions and subroutines are automatically loaded from the PGI 's Fortran run-time library if referenced by a pgf77 program.

What is the trick to use the ‘hostnm’ function??

I am familiar with doing one of the following from a previous problem I had with undefined references: use a special interface to the function; use a ‘C$PRAGMA C()’ in the fortran routine to redefine the name of the function; use a special ‘#define’ statement for ‘hostnm’; or rename the function somehow?

All other undefined references were resolved but the ‘hostnm’ is still problematic.

-Steven Speer

Sorry for my tardiness on this issue, several of us have been doing some training so I haven’t be able to respond. From what I can tell, there are several of the C 3F that aren’t implemented on windows, including “hostnm”. Why our docs say all of them are there, is probably an oversight.

Any case, I’m trying a work around and will post it once I find something useful. From you other post, it looks like this hasn’t stop you from working, so hopefully my delay hasn’t hurt.

  • Mat

I’ve tried several posible solutions with little results. At least I understand better why this wasn’t implemented! The basic problem is that our Windows product is a port of our Linux product. As such, we rely on a Linux like environment (cygwin) to handle system interfaces and are limited by what it provides.

For hostnm, we would need to call the libwsock32.a function ‘gethostname’. While I was able to use ‘gethostname’ with pgcc, pgf90 could not use it either directly or through a C wrapper function.

So this leaves us with an ugly but works solution. You need to use the the “system” function to call the shell command ‘hostname’ and redirect its output to a file. Then open the file, read in the host name, and finally delete the file.

Sorry I couldn’t do better. Maybe someone else has an idea?

I have example program and how I compiled with PGI Win32 5.2-2 release.

Hostname is “port”.
PGI$ pgf90 -Munix main.f -c
PGI$ pgf90 main.o func.c -lwsock32
PGI$ ./a.exe

--------------------------main.c -------------------------
program main
character (len=256) :: nam = “”
integer result
result = hostme(nam)
print *, nam

#include <stdio.h>
#include <unistd.h>
#if defined (WIN32) && ! defined (CYGWIN)
#include <winsock.h>
int hostme
(char* name) {
int i;
#if defined (_WIN32) && ! defined (CYGWIN)
WSAStartup ((2<<8) | 2, &p);
return gethostname(name,256);