PDA

View Full Version : Help linking and using libs in simple project



Mentok
13th September 2014, 00:22
Hello,

I am very new to QT and having trouble with a particular issue understanding how importing libs works in Qt. I have a very simple default Qt project for the MinGW compiler and I need to use a custom string class compiled in Visual Studio 2008. (I don't have access to the source, and there are other libs I need to work besides this one as well).


#-------------------------------------------------
#
# Project created by QtCreator 2014-09-12T09:46:39
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QtNVCDemo
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

win32: LIBS += -L$$PWD/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug/ -lSString-4

INCLUDEPATH += $$PWD/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug
DEPENDPATH += $$PWD/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug

win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug/SString-4.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug/libSString-4.a


I get this error

:-1: error: No rule to make target 'C:/Users/bereall/QtDemo/../build-QtDemo-Desktop_Qt_5_3_MinGW_32bit-Debug/debug/libSString-4.a', needed by 'debug\QtDemo.exe'. Stop.

The import lines were done trying to link a static lib using the wizard. What are .a libs and how do you create them? If I try changing the Sstring-4.a to the lib which I have it just says the same error? Trying to compile without the target dependencies lines works but then when I try to create a SString class or add the header to my project it doesn't recognize SString. Do I have to use libs created with the same compiler or can I import other libs?

I just want to be able to import an external lib and be able to use its classes in my code. Haven't had any luck with reading old forum posts on this particular issue. :(

Also if you're just trying to develop for Windows Desktop is MinGW the right compiler or should I be using msvc2010 opengl?

Nomad_Tech
13th September 2014, 13:52
A '.a' file is an archive file. As I understand it, it tells the linker where to find your functions inside the dll.
Your "SString-4.dll" should have come with a "SString-4.a" if it was for development use. If you don't have the .a file it's not the end of the world.

-Download Dependency Walker.
-Open your "SString-4.dll" with Dependency Walker.
-On the middle-right side should be a list of all the functions contained in the dll.
-Right click one of these functions and choose "Select All".
-Right click again and choose "Copy function names".
-Paste into Notepad and save as "SString-4.def" in the same directory as your dll.
-The top line in the .def file should be "LIBRARY SString-4.dll". The next line should be "EXPORTS". Your list of functions will be below this.
-After each function leave a space, then a "@" followed by the ordinal number of the function. You can get the ordinals by reading them off the list on Dependency Walker.
Your .def file should now look something like this example here:


LIBRARY K8055D.dll
EXPORTS
SetCounterDebounceTime @1
ResetCounter @2
ReadCounter @3
ReadAllDigital @4
ReadDigitalChannel @5
SetAllDigital @6
SetDigitalChannel @7
ClearAllDigital @8
ClearDigitalChannel @9
WriteAllDigital @10
SetAllAnalog @11
SetAnalogChannel @12
ClearAllAnalog @13
ClearAnalogChannel @14
OutputAllAnalog @15
OutputAnalogChannel @16
ReadAllAnalog @17
ReadAnalogChannel @18
CloseDevice @19
OpenDevice @20

-Now open windows command line and type: PATH=%PATH%;C:\Qt\2010.05\mingw\bin (or whatever the path to 'dlltool' is on your build)
-CD to the directory where your dll and .def files are located.
-Then type: dlltool -v --dllname SString-4.dll --def SString-4.def --output-lib SString-4.a

If all was successful you should see a list of stuff from dlltool and your .a file will be created.

I've had to this before with dlls that didn't come with the required files so I understand your frustration -_-.