How to link to a third party's DLL in PVF?

Hi Everyone

I have an executable C.exe, a C.DLL and the corresponding C.lib.
All these were created by another party with C/C++ on a 32-bit system
and I do not have their source codes.

I am creating a F.DLL with Fortran for the main calculation, say with source code F.f90.
The executable C.exe is a Window GUI which calls my F.DLL for calculation
and some functions in the C.DLL for messages passing and some other jobs.
My F.DLL also calls some functions in the C.DLL.


I used a compiler from another company and it worked well on 32-bit system.
Now I want to use OpenMP and OpenACC to speed up the calculation,
and use the whole application on both 32-bit and 64-bit systems.
I find that PGI’s PVF supports all these.


I am having problems with it. The following is what I did and the problem I have.

In my source code for the F.DLL, I use PGI Fortran directive
!DEC$ ATTRIBUTES DLLIMPORT:: function_name_from_c.DLL
before calling the function.


From previous experience, I also upgraded the C.lib with the following steps
(1) From the C.DLL, I obtained a C.def file,
which contains the names of the functions that are called in my F.DLL.
From the callings in my F.DLL’s source code, I know the arguments of each
of these functions.

(2) I wrote a Fortran file (say Fstubs.f90) which contains stubs of each of these
functions, like interfaces. These stubs contain
(a) FUNCTION function signature (name and arguments)
(b) declaration of the arguments
© a PGI Fortran directive !DEC$ ATTRIBUTES DLLEXPORT:: function_name
(d) END FUNCTION

(3) I used PGI compiler pgfortran and compiled the Fstubs.f90 with the following command
pgfortran -Bdynamic -c Fstubs.f90
This created an .obj file Fstubs.obj

(4) Then I upgrade the C.lib file with following command
pgfortran -Mmakeimplib -o C.lib Fstubs.obj -def=C.def


To compile my F.f90 for the F.DLL in the Microsoft Visual Statio PVF,
in the project’s property page’s Fortran|Command line and Linker|Command line
(both places), I add
-L\user…\directory_name -lC
where C:\user…\directory_name is the directory containing the .lib file.
It seems that the option -lC looks for the file libC.lib in the directory
C:\user…\directory_name. So I have renamed file C.lib as libC.lib


I also add libC.lib and C.DLL into the propject (I am building) as Resource files


When I buid the project, I got the following errors

unresolved external symbol function1-name_
referenced in function function2-name_ F.obj

There are references on how to call C Functions from Fortran within PGI compiler, but I have not been able to find references on how to link a .DLL from third party into a Fortran .DLL or executable in PVF.

I guess, there will also be a problem when the C.exe calls my F.DLL
after I build it successfully with PVF. Can someone shed some light on this too.

If you have to call code in third party 32-bit DLLs, you are restricted to using the 32-bit PGI compilers. You cannot mix 32-bit and 64 bit objects.

In this case, be aware that, by default, PGI Fortran uses the STDCALL calling convention, which is incompatible with the CDECL convention used by most compilers. There are compiler options to make PGI Fortran compatible with CDECL.

I felt that it was disorienting to read the long and rambling description of the various things that you tried but did not get to work. Please describe your objectives compactly without mixing in a report of what you attempted to do without success.