Download PGI Compiler User`s Guide
Transcript
Creating and Using Libraries
1. Create the DLL obj1.dll and its import library obj1.lib using the following series of
commands:
% pgfortran -Bdynamic -c object1.f
% pgfortran -Mmakedll object1.obj -o obj1.dll
2. Compile the main program:
% pgfortran -Bdynamic -o prog1 prog1.f -defaultlib:obj1
The –Bdynamic and –Mmakedll switches cause the compiler to link against the PGI
runtime DLLs instead of the PGI runtime static libraries. The –Bdynamic switch is required
when linking against any PGI-compiled DLL, such as obj1.dll. The -defaultlib: switch
specifies that obj1.lib, the DLL’s import library, should be used to resolve imports.
3. Ensure that obj1.dll is in your path, then run the executable prog1 to determine if the
DLL was successfully created and linked:
% prog1
sub1 adata 11
sub1 i 12
main adata 12
Should you wish to change obj1.dll without changing the subroutine or function
interfaces, no rebuilding of prog1 is necessary. Just recreate obj1.dll and the new
obj1.dll is loaded at runtime.
10.7.2. Build a DLL: C
In this example, we build a DLL out of a single source file, object2.c, which exports data
and a subroutine using __declspec(dllexport). The main source file, prog2.c, uses
__declspec(dllimport) to import the data and subroutine from the DLL.
object2.c
int __declspec(dllexport) data;
void __declspec(dllexport)
func2(int i)
{
printf("func2: data == %d\n", data);
printf("func2: i == %d\n", i);
data = i;
}
prog2.c
int __declspec(dllimport) data;
void __declspec(dllimport) func2(int);
int
main()
{
data = 11;
func2(12);
printf("main: data == %d\n",data);
return 0;
}
PGI Compiler User's Guide
128