Page 1 of 1

Linking .lib and .dll

Posted: Sun Nov 10, 2013 3:05 am
by musictreeAstro
Hi,

I'm running Windows 7 using MinGW-4.7.1 and version 5.3 of codelite.

I have an SDK in the form of a .dll, .lib, and .def (created by MSVC) that I need to use in a program I'm writing. I also have the .h file (which I #include in my program) that goes with these, but besides that the only documentation is a pdf list of the included functions.

I have tried several approaches to linking the library, but all have been successful - resulting in

Code: Select all

undefined reference to `_imp__*@#'
errors or MinGW being unable to find the library depending on how I've set the project settings.

In project settings I have set the library path explicitly for the linker, the .dll is in my folder where the executable should appear, and I have tried giving the library name in several different ways. One approach was to link the library as libfli.lib. This results in

Code: Select all

C:\Windows\system32\cmd.exe /c "mingw32-make.exe -j 8 -e -f  Makefile"
"----------Building project:[ v_0 - Debug ]----------"
mingw32-make.exe[1]: Entering directory `C:/Users/Documents/Software/CameraController/v_0'
g++ -o ./Debug/v_0 @"v_0.txt" -L. -LC:/Users/Documents/Software/CameraController/libfli-1.104-winbin  -lfli.lib   -mthreads -LC:/wxWidgets-2.9.5/lib/gcc_dll -lwxmsw29ud_xrc -lwxmsw29ud_aui -lwxmsw29ud_html -lwxmsw29ud_adv -lwxmsw29ud_core -lwxbase29ud_xml -lwxbase29ud_net -lwxmsw29ud_richtext -lwxbase29ud -lwxtiffd -lwxjpegd -lwxpngd -lwxzlibd -lwxregexud -lwxexpatd -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lwxregexud -lwinspool -lwinmm -lshell32 -lcomctl32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lwsock32  -mwindows
c:/mingw-4.7.1/bin/../lib/gcc/mingw32/4.7.1/../../../../mingw32/bin/ld.exe: cannot find -lfli.lib
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/v_0] Error 1
v_0.mk:79: recipe for target `Debug/v_0' failed
mingw32-make.exe[1]: Leaving directory `C:/Users/Documents/Software/CameraController/v_0'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target `All' failed
0 errors, 0 warnings
It is complaining that it cannot find -lfli.lib, yet I have read in the MinGW documentation that MinGW should now be able to handle .lib files by compiling with the -llib flag (see http://www.mingw.org/wiki/Specify_the_l ... ker_to_use) rather than the usual -l. Is there some way to set codelite to use -llib when I give it a library ending in .lib?

I have also read that to use libraries created in MSVC with MinGW, you can use MinGW's pexports and dlltool to create a library ending in the .a extension. This was my second approach. I followed the instructions here: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
and created libfli.a, entered libfli as the library to link and I then get this error (actually every call to a function in the library is undefined, but I've commented the others out so the build log is easier to read).

Code: Select all

C:\Windows\system32\cmd.exe /c "mingw32-make.exe -j 8 -e -f  Makefile"
"----------Building project:[ v_0 - Debug ]----------"
mingw32-make.exe[1]: Entering directory `C:/Users/Documents/Software/CameraController/v_0'
mingw32-make.exe[1]: Leaving directory `C:/Users/Documents/Software/CameraController/v_0'
mingw32-make.exe[1]: Entering directory `C:/Users/Documents/Software/CameraController/v_0'
g++  -c  "C:/Users/Documents/Software/CameraController/v_0/MainFrame.cpp" -g -O0 -Wall -mthreads -DHAVE_W32API_H -D__WXMSW__ -D__WXDEBUG__ -D_UNICODE -IC:/wxWidgets-2.9.5/lib/gcc_dll/mswud -IC:/wxWidgets-2.9.5/include -DWXUSINGDLL -Wno-ctor-dtor-privacy -pipe -fmessage-length=0  -fno-keep-inline-dllexport   -DSVN_REVISION=\"\"   -o ./Debug/MainFrame.o -I. -IC:/CDSDK/inc
g++ -o ./Debug/v_0 @"v_0.txt" -L. -LC:/Users/Documents/Software/CameraController/libfli-1.104-winbin  -lfli   -mthreads -LC:/wxWidgets-2.9.5/lib/gcc_dll -lwxmsw29ud_xrc -lwxmsw29ud_aui -lwxmsw29ud_html -lwxmsw29ud_adv -lwxmsw29ud_core -lwxbase29ud_xml -lwxbase29ud_net -lwxmsw29ud_richtext -lwxbase29ud -lwxtiffd -lwxjpegd -lwxpngd -lwxzlibd -lwxregexud -lwxexpatd -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lwxregexud -lwinspool -lwinmm -lshell32 -lcomctl32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lwsock32  -mwindows
./Debug/MainFrame.o: In function `ZN9MainFrame11onTakeImageER14wxCommandEvent':
C:/Users/Documents/Software/CameraController/v_0/MainFrame.cpp:77: undefined reference to `_imp__FLIGetLibVersion@8'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Debug/v_0] Error 1
v_0.mk:79: recipe for target `Debug/v_0' failed
mingw32-make.exe[1]: Leaving directory `C:/Users/Documents/Software/CameraController/v_0'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target `All' failed
1 errors, 0 warnings
What am I missing or how can I use the functions provided by this library which was created in MSVC?

Thanks,

Will

Re: Linking .lib and .dll

Posted: Sun Nov 10, 2013 10:50 am
by eranif
I suggest that you try asking this question on the MinGW mailing list - they will surely be able to help you or maybe at the library mailing list (if there is such a one)
Eran

Re: Linking .lib and .dll

Posted: Mon Nov 11, 2013 2:32 pm
by ColinTaylor
I had a similar sounding problem trying to use a microsoft dll file, eventually "solved it" as follows (it seems rather hacky looking back at my notes!).
Since you've already got the .def file, there'll be a step or two you can omit, and you'll have to adapt all these steps for your own case. I can't recall where I got the 'nn' values from in step 3.
Hope this helps and good luck,
Colin

-------------------------------

Start point : pdh.dll (microsoft Perfromance Data Helper library)
Requirement : create libpdh.a and link it into an wxwidgets app

libpdh.a created as follows

1. download pexports ("pexports-0.44-1-mingw32-bin.tar.lzma") from sourceforge
("http://sourceforge.net/projects/mingw/f ... ts-0.44-1/")

2. 7zip (e.g.)
Open above file
copy pexports.exe from archive to E:\x86\MinGW-4.6.1\utils

2. command line
cd E:\x86\MinGW-4.6.1\utils
pexports c:\Windows\system32\pdh.dll > pdh.def

3. text editor
modify the following in pdh.def, adding the @nn bit ...

PdhAddCounterW@16
PdhCloseQuery@4
PdhCollectQueryData@4
PdhGetFormattedCounterValue@16
PdhOpenQueryW@12

4. command line (dlltool is in mingw/bin)
dlltool -k -D pdh.dll -d pdh.def -l libpdh.a

5. explorer
copy libpdh.a to a suitable folder (e.g. <project>/lib) and link against it

Re: Linking .lib and .dll

Posted: Mon Nov 11, 2013 3:08 pm
by Jarod42
Note the @nn value are the total byte size of the arguments.