FAQ: Using DLLS with G95
|
||||||||||||||||||||||||||||||||
Q: Where can I find information about making and using dlls with g95?A: Search for previous postings in the archive of the g95 newsgroup on how to make a dll. Stdcall and DLL tools of MSVC and MinGW: http://www.geocities.com/yongweiwu/stdcall.htm Q: What is a dll?A: DLL is an acronym for "Dynamic Link Library." Dlls are commonly used on MS Windows. A dll typically consists of one or more subroutines that are compiled into a library to be called by other programs. Programs compiled with g95 can statically link to subroutines in dlls. G95 compiled dlls can be used by other programs. Dynamic linking is not currently supported by g95, but can be implemented as explained here. Q: Is there an advantage to using a dll?A: Dlls can be called by programs such as Visual Basic, Word, or Excel. Q: What command line should I use with g95?A: This example produces a dll called ex.dll. Save the code below in file ex.f90.
Compile and generate the dll with the following commands:
By default g95 internal functions are included in the dll. Here is a test program that calls the dll.
Q. What is the stdcall calling convention?A: Explanations are provided at the following links: http://www.programmersheaven.com/2/Calling-conventions#stdcall Q. Can I compile the g95 library to work with dlls?A. Yes, instructions for the build are in the Compilation Notes page, at http://www.g95.org/source.html#Build. Q. Can a dll compiled with g95 be loaded dynamically? Back to topA. The example below, adapted from code provided by David Miles, shows how to include a small C program with dlls compiled with g95 to load a dll dynamically. This requires that the g95 library is compiled as explained on the Compilation Notes page. The object file g95main.o must be present in the /lib directory. The files are included in the g95 Cygwin binary distribution. The idea behind this approach is to link in g95 as a library, which causes the GNU build system to ignore a second "main" function. When executing the program, the files main.exe, dll1.dll and dll2.dll should all be present in the current working directory. Save the code below as dll1.f90
Save the code below as dll2.f90
Save the code below as main.f90
Save the following code as load.c. This code is compiled and the object code linked into dll1, to load dll2 dynamically at runtime.
In MinG use this version:
Compiling:
Output:
Q. What tools are available for working with dlls?A. The binutils package includes the following tools:
Microsoft provides PEDUMP. There is information about using the pedump utility in this series of articles (1), (2). SmidgeonSoft offers a free Windows Debugger, Disassembler, and Code Analyzers More info: DLL Creation in MingW. |


