1) I'm not a programmer, most of the time I'm a physicist, but I do numerical simulation.
2) I have a DLL that I want to use functions from. I have the .h file associated with the DLL, but no .def or library that I can use for static linking.
I am trying to implement the GMP (GNU multiprecision library) in a large simulation code that I’ve been running on a PC, written in standard C. I’ve been using MetroWerks CodeWarrior IDE for the compiling and debugging.
The libraries are actually from the GNU project, but unfortunately, only the unix versions of the static library are available, and I’ve not managed to find a way to convert them for use in CodeWarrior. I did manage to find a version of the DLL (used by both Maple and Matlab), and a DLL viewer that lets me look at what routines are in the DLL. Using the GMP manual, I was able to get the function definitions. With this data, I’m trying to import the functions in GMP.dll into the simulation code.
I tested the routines by writing a small piece of code that imported the functions as shown below. In this simple routine I could load and execute the functions. The problem is that I want to use the functions in MANY routines distributed in six other files in the simulation code. I would like to import the functions from the DLL so that they are global in scope.
I can link the functions successfully using the following code, inside main.c,
(only one function is shown for brevity..)
Expand|Select|Wrap|Line Numbers
- #include <gmp.h>
- typedef void (*importFunction ) (unsigned long int); // mpf_set_default_prec
- ..
- ..
- int main(void)
- {
- ..
- ..
- importFunction mpf_set_default_prec; //---line(1)
- //
- // Load the DLL
- //
- HINSTANCE hinstLib = LoadLibrary("gmp.dll");
- if(hinstLib == NULL)
- {
- printf("ERROR: unable to load gmp.dll\n");
- return 1;
- }
- mpf_set_default_prec = ((importFunction)GetProcAddress(hinstLib, "__gmpf_set_default_prec"));
- ..
- }
When I try to make the definition of mpf_set_default_prec global, by moving line 1 outside the definition of main, I get the following error,
Error : identifier '__gmpf_set_default_prec(unsigned long)' redeclared
was declared as: 'void (unsigned long)'
now declared as: 'void (*)(unsigned long)'
main.c line 17 importFunction mpf_set_default_prec;
Error : not an lvalue
main.c line 44 mpf_set_default_prec = ((importFunction)GetProcAddress(hinstLib, "__gmpf_set_default_prec"));
So, do I have to link to the DLL and load the functions within it inside each function that I want to use the imported functions in? Or is there some way to link to the library once and make the function available globally?
Any help would be very greatly appreciated.
Andre