Using F77/3F/Vax libraries alongside extern C by reference

I am running PGI Fortran under MSVS 2008 to compile for x64. I have code that requires I link to some extern C library functions as well as use some 3F/Vax functions (GETARG and FLUSH).

Here’s my problem:

I need to set the PGI Fortran compiler options to use C by reference for the calling convention in order to access my C libraries, but when I do that, it breaks my ability to call the F77 3F/Vax functions. (It seems that the default libraries for these do not support being called by reference.)

Are there any precompiled PGI libraries for x64 that I can use to enable GETARG and FLUSH that I can call by reference? If not, can somebody tell me how to wrap these functions into C so I can use them alongside my extern C libraries?

Thanks in advance, and any help would be appreciated…

Hi grantmccardell,

Fortran as well as our 3f libraries uses pass by reference so I’m not clear as to the issue you’re encountering. Can you please post more details and if possible, an example?


Hi Mat,

I’ve isolated the problem, and it is not one of pass by reference, but probably an UPPERCASE / lowercase or underbar_ mangling issue.

The library I was using (netCDF) was one that I needed to build in C for x64, and the C source uses cfortran.h. This header contains a number of preprocessor directives designed to allow interplatform C/Fortran interoperability. I had naively set VISUAL_CPLUSPLUS as the preprocessor switch for this header without realizing that this would produce a library that used the old Compaq VF passing conventions. This, however, should not have prevented the PGI Visual Fortran linker from linking. (But would have caused an exception when trying to run the code because of the string length argument passing.) I believe what caused the PGI Visual Fortran linker not to link was probably an UPPERCASE or no underbar_ issue.

For some reason, however, I was able to link to this (bad CVF) library when I set the PGI Visual Studio -> Fortran-> External Procedures-> calling convention switch to ‘C by reference.’ After I did this, however, I got a few ‘unresolved external’ linker errors for some 3F functions. I believe these 3F functions could just be used directly under PGI Fortran, but for portability, the code is set up to access them using CALL statements. The linker didn’t seem to mind this when the calling convention was set to ‘default’ but it didn’t find them as external functions in the default x64 libraries when ‘C by reference’ was set.

The following sample code will illustrate the problem I had. When built for x64 using PGI Visual Fortran in Visual Studio 2008, this snippet will build OK with the Fortran-> External Procedures-> calling convention set to ‘default,’ but will throw an ‘unresolved external’ linker error when the calling convention is set to ‘C by reference.’ It will build OK for WIN32 either way, so this is only an issue for the x64 libraries. I believe x64 only passes using C by reference anyway (__fastcall), so the option in PGI Visual Fortran to set this is also a little bit confusing.

program prog
implicit none
end program prog

In the meantime, I’ve rebuilt my x64 netCDF libraries for pgiFortran without the obfuscating CVF stuff and everything is (momentarily) copasetic. Now I need to get MPI working under x64 for code written for UNIX. Anyone know if there’s an easy way to allow Windows to permit multiple processes to access the same open file? Not that anything is ever easy. If it were, they wouldn’t need me to do it.