Reputation: 31
I need to write a c++ program using an DLL. I have the DLL and a header file, but no .a file for the linker. The GNU GCC Compiler cannot find the functions in the DLL file.
I’m getting errors like:
undefined reference to _imp__mbnEthernetOpen@8
How do I fix this problem.
Upvotes: 2
Views: 423
Reputation: 129
It has been quite a long time since I last used WinAPI though if I recall correctly you need to dynamically load the library in runtime using either of LoadLibrary
or LoadLibraryEx
. Afterwards you'd need to get the address of the specific function you are interested in using GetProcAddress
; in your case this would be mbnEthernetOpen
(I guess).
After all this hassle, you can finally call mbnEthernetOpen
through the function pointer you have just obtained from the DLL.
Edit: After reading through the comments, I decided to extend my answer to contain a method for generating the .lib
file.
First of all, VLC's wiki has a very well written article that goes through the process thoroughly. However, for the sake of clarity I'll repeat it here.
From the Start menu locate and execute Visual Studio Command Prompt, according to the wiki this should be under the Microsoft Visual Studio entry.
On the Visual Studio Command Prompt execute this command-line to extract the symbols from the DLL file: dumpbin /exports input.dll > exports.def
Make sure you replace input.dll
and exports.def
with the paths to the correct input and output files, respectively.
At this point you can fire up your text editor and edit your newly generated .def
file to look something like this:
EXPORTS
libvlc_add_intf
libvlc_audio_get_channel
libvlc_audio_get_mute
libvlc_audio_get_track
libvlc_audio_get_track_count
libvlc_audio_get_track_description
libvlc_audio_get_volume
…
Obviously, the names of the exported symbols will be different for your DLL.
However, for whatever reason if you wish not to do all of this manually, the cool guys who wrote the article on the VLC wiki came up with a one-shot command-line that does generate a .def
file automatically:
echo EXPORTS > output.def
for /f "usebackq tokens=4,* delims=_ " %i in (`dumpbin /exports input.dll`) do if %i==tag echo %i_%j >> exports.def
Again make sure you replace the temporary names with their corrected equivalents, additionally pay attention to that tag
symbol. In VLC's example this was libvlc
, it is the initial part of each exported symbol in their example. Now, since you have shown us a single function I am not sure what this should be, but I am sure you can figure it out by taking a look at the exported symbols.
And finally, generate the .lib
file from these exported symbols which are now in your .def
file:
lib /def:exports.def /out:library.lib /machine:x86
Beware that your /machine
might be different depending on your platform.
There you go. This is how you generate a .lib
file from a .dll
file.
Upvotes: 1
Reputation: 10961
It looks like you're trying to compile a program that depends on another shared library. To compile in support for this library on windows you typically need 3 things:
You are saying in your comments that all you have is the header and dll files. This means either you are missing the lib binary and need to find/recompile the library OR you're trying to dynamically load the DLL at runtime and use function pointers to call the different functions (as Goksel mentions in his answer). Your error suggests that you're trying to do the first option (link to a shared library).
You need to have the lib file if you're going to be dynamically linking to the library at compile time.
Upvotes: 3