PDA

View Full Version : QT Creator - Creating and Using LIBS



floyd.pepper
22nd July 2009, 05:05
Hi all,

I know there has been related questions to this on the forums. I've searched through them but none of which answer my particular problem.

Firstly, I'm a nooby, so go easy. I'm trying to learn c++ for myself and have chosen the following configuration to get started;
OS: XP
toolkit: QT 4.5.1
IDE: QT Creator 1.2

Everything is up and running and I've created some *basic* progs.

However, I'm stuck on the following problem. I wanted to learn how to use libraries. So I created a shared library (called test2library) using the QT Creator wizard and added some silly functionality. Unfortunately, I'm not able to use the linked library in my main project (called imaginatively mainProject).

// Please also let me know of any bad practice etc. As I said, I'm trying to learn here so all comments welcome in addition to the solution.//

So my shared library contains four files; test2library.h .cpp .pro and _global.h

Here are the changes I made to the header and implementation files.


test2library.h


#ifndef TEST2LIBRARY_H
#define TEST2LIBRARY_H

#include "test2Library_global.h"
#include <qDebug>

class TEST2LIBRARYSHARED_EXPORT Test2Library {
public:
Test2Library();
void doit();
};

#endif // TEST2LIBRARY_H



test2library.cpp


#include "test2library.h"


Test2Library::Test2Library()
{
}

void Test2Library::doit()
{
qDebug() << "In test2Library";
}


OK, everything compiled OK and seems to be running.

So I go back to my mainProject which is a GUI frame, also with some silly code in it. I open up my mainProject.pro and add the following lines;



<snip>

INCLUDEPATH += "..\test2Library\test2library"
win32:LIBS += -L"..\test2Library\test2library\debug" -ltest2Library



I save everything and rebuild...everything seems fine.

Then in my mainProject I add the header;



#include "../test2Library/test2library/test2library.h"

void MainWindow::on_pushButton_clicked()
{
qDebug() << "Button pressed!";
Test2Library *t = new Test2Library();
t->doit();
}



The program builds fine, but when I run it, I get


..../mainProgram.exe exited with code -1073741515

Can anyone give me any helpful pointers. I've searched for solutions but can't seem to find anything that works. I've simplified the entire process as much as possible in order to isolate the problem. It seems that there is a problem with linking the libraries?

Cheers,
Mick

yogeshgokul
22nd July 2009, 05:41
To create static lib, just add these lines in your .pro file


TEMPLATE = lib
CONFIG += staticlib


And to use this library in main program. Add these lines in .pro file.


LIBS += -Lpath/to/lib/file
LIBS += -lnameoflibrary

wysota
22nd July 2009, 09:14
I think your application is fine. Try adding CONFIG+=console to your project file, rerun qmake, rebuild and see if it changes anything.

floyd.pepper
22nd July 2009, 16:27
Thanks for the replies everyone...

@ yogeshgokul:
I've added the lines you recommended. I'm not quite sure of the syntax for the LIBS though...here is what I have right now


win32:INCLUDEPATH += ../test2Library/test2library
win32:debug:LIBS += -L../test2Library/test2library/debug
LIBS += -ltest2Library

...and it seems to build fine but still not run

@ wysota: I'm not sure if I add that line to the library pro or the mainProject pro?

Update

If I copy the dll file into the same folder as the mainProject.exe then everything runs in the IDE. However, if I remove the dll and leave it in it's build location then I get the error "exited with code -1073741515"

Can you tell me if there is something else I should set? Does the exe know to look for the dll in location specified by LIBS?

wysota
22nd July 2009, 19:43
No, the executable is not responsible for knowing where the library is. That's the job of the system's dynamic linker that gets executed before your application launches and links all needed libraries to the process address space. The dll needs to be placed in a directory where the linker will look for it. The directory where the executable resides is one of those directories when we speak about Windows.

floyd.pepper
22nd July 2009, 21:04
Thanks for your support on this wysota.

So, from my understanding, neither the INCLUDEPATH nor the LIBS variables tell the exe where to look for the dll.

If that is the case, then I can think of two solutions. Please let me know which one you think is most viable;


Option 1
I can change the .pro of the library project. Here I change the TARGET variable from

TARGET = test2Library
to

debug:TARGET = ../../../mainProject/debug/test2Library

In this solution, the dll is placed directly into the mainProject folder with the mainProject.exe. This will become a problem if I want to use the same dll with multiple applications. I would need to create a copy of the dll in each of the application folders.

Option 2
In option two, I can simply add the directory of the dll to my system PATH. However, this will get messy after a while if I keep adding to my PATH for each dll project I create.


Can you tell me if there is a third solution? Surely I can add the linker paths in QT as I did it quite often in MS Visual Studio.

Again, thanks again for taking the time to help out a newbie on this.

All the best,
Mick

wysota
22nd July 2009, 21:27
So, from my understanding, neither the INCLUDEPATH nor the LIBS variables tell the exe where to look for the dll.
Yes, that's correct. They are only meaningful during the compilation.


If that is the case, then I can think of two solutions. Please let me know which one you think is most viable;

[LIST=1]
Option 1
I can change the .pro of the library project. Here I change the TARGET variable from

TARGET = test2Library
to

debug:TARGET = ../../../mainProject/debug/test2Library

In this solution, the dll is placed directly into the mainProject folder with the mainProject.exe.
How about using the DESTDIR variable?


This will become a problem if I want to use the same dll with multiple applications. I would need to create a copy of the dll in each of the application folders.
That's basically the unfortunate choice on Windows. You either copy the dll to the system folder or you have to copy it around to every binary it uses the library.


Can you tell me if there is a third solution? Surely I can add the linker paths in QT as I did it quite often in MS Visual Studio.
Linker paths are related to compilation, not execution. That's exactly the same what -L does for GCC/MinGW.

I think there is a way to do what you want through manifest files but unfortunately I know nothing about them so I can't be of any help.

floyd.pepper
22nd July 2009, 21:46
Cheers wysota,

I'm using DESTDIR now to place the dll with the binary of my mainProject. I will look into manifest files another day I think.

Again thanks for the support.

/Mick