PDA

View Full Version : Some advice on how to structure this program?



mrwooster
12th August 2009, 09:29
I am designing a program which reads some data from a text/data file, performs some calculations and then writes the results to another file...

I wrote the program using Dev C++... it was 100% independent, runs from the command prompt and does not require the use of any external libraries (except the STL).

I have now ported the app to Qt and created a GUI to make it easier to change the parameters and specify file locations etc... also to display a progress bar. The Qt GUI is a single class which obviously requires the QtCore and QtGUI libraries as well as the classes that are used to perform the calculations....

The problem is... I want to be able to develop the classes related to the calculations on PCs which do not have Qt installed.... now that the Qt GUI has been created, it does not need to be changed or re-compiled, but because I am constantly making alterations to the classes involved in the calculations, I am constantly having to recompile the whole application on a PC which has Qt installed.....


So... I want to be able to compile the code from my calculations without having to re-compile my GUI... I am a bit of a rookie at this so I am not 100% sure of what is the best way to do this??

I am guessing that I have to create a set of shared libraries? Is this the case and if so how do I set about it? Is there any way that I can compile it so that I just put an #include <MyCalcProgLibrary> at the top of my QtGUI and use the classes and functions from it like you would for any other Qt or STL library?

Thanks for any help... sorry about the waffling

yogeshgokul
12th August 2009, 09:48
I am guessing that I have to create a set of shared libraries?
Yes this is true, this is best solution for your problem, go for static libraries.


Is this the case and if so how do I set about it?
Just need to add following line to your .pro file, to create static library.

TEMPLATE = lib

Add following line in .pro file of user program, who is going to use this library.

LIBS += -lyourLibraryName

mrwooster
12th August 2009, 10:31
Thanks for the help... I have put all my source classes into a static library and when I compile it it produces a load of .o files and a .a file named libXXXX.a

But I am having trouble linking it to my GUI program....? I have tried editing the .pro file but I can't seem to get it to work? Where does the libXXXXX.a file need to reside? Do I need the XXXXX.o files as well?

BTW... I am using QtCreator... is there an easier way to link it using the IDE?

yogeshgokul
12th August 2009, 10:39
Thanks for the help... I have put all my source classes into a static library and when I compile it it produces a load of .o files and a .a file named libXXXX.a
Relax, this is usual. After all you are making a big change in your project so errors are normal.


But I am having trouble linking it to my GUI program....? I have tried editing the .pro file but I can't seem to get it to work? Where does the libXXXXX.a file need to reside? Do I need the XXXXX.o files as well?
Yes you need to have lib files. Otherwise GUI will provide you errors.


BTW... I am using QtCreator... is there an easier way to link it using the IDE?
I dont think so, .pro files are very powerful. You should configure them instead of changing the IDE settings. Then it will be easy for you to port your project to other operating systems.

I would suggest, to create a test static lib and a test GUI who uses that test lib. Then it will be easy change your actual project.

All the best.:)

mrwooster
12th August 2009, 10:45
Thanks again.... but I am still having a trouble understanding a couple of things (sorry :()

Are you saying that the fact that I am getting XXX.a and XXX.o files means that there are errors? When I compile the files it does not come up with any errors???

If I simply put TEMPLATE = lib then it produces a .dll surly this is a dynamic library and not what I want.... if I put CONFIG += staticlib as well in the .pro then it creates the .o and .a files.... but no .lib files...?

yogeshgokul
12th August 2009, 10:59
Using these 2 lines will create static lib.

TEMPLATE = lib
CONFIG += staticlib
Resulting 1 output file XXX.a


Using only this line will create dynamic lib.

TEMPLATE = lib
Resulting 2 output files.
XXX.so
XXX.a

mrwooster
12th August 2009, 12:09
Thanks for all the help but I am still having issues and its driving me nuts....

as I understand it the libXXX.a file is the library containing all the classes and functions... I found http://doc.trolltech.com/4.5/qmake-variable-reference.html#libs and its helped me out a bit but I still cant get it to work

In my .pro file of the GUI app I have LIBS += -L"C:/Documents and Settings/......../c++/disspertionLib/release" -ldisspertionLib

the folder release contains all the .o and libxxx.a files

but I am still getting errors:

mainwindow.cpp:7:24: coordinate.h: No such file or directory
mainwindow.cpp:8:25: boundaryMap.h: No such file or directory
mainwindow.cpp:9:22: particle.h: No such file or directory
mainwindow.cpp: In member function `void MainWindow::on_btnGo_clicked(bool)':
mainwindow.cpp:53: error: `boundaryMap' was not declared in this scope
mainwindow.cpp:53: error: expected `;' before "bound"
mainwindow.cpp:55: error: `Particle' was not declared in this scope

etc......


btw... how do I include the library in my code? At the moment I still have
#include "coordinate.h"
#include "boundaryMap.h"
#include "particle.h"

which were the origional names of the files before I put them into the library... do these still stay the same or do I need to change them?

Thanks again

yogeshgokul
12th August 2009, 13:18
btw... how do I include the library in my code? At the moment I still have
You have to update INCLUDEPATH variable in .pro file.
For example, your header files are in XXX folder.
Write this line in your .pro file:

INCLUDEPATH += XXX
Then your GUI program will be able to locate your header files.

Using Static Libs Example:
Suppose:
I have 2 libs testLib1 and testLib2.
These files are in user/local/libs.
And header files of these libs are in user/local/headers.

This is how my GUI application .pro file will look like.

INCLUDEPATH += user/local/headers
LIBS += -Luser/local/libs
LIBS += -ltestLib1 -ltestLib2

mrwooster
12th August 2009, 13:35
Ah... thank you... I have finally managed to get it to compile and run.

I created a folder libs and libs/headers to put the files in and it compiles and runs correctly....

However, if I make any changes to disspertionLib then I have to re-compile my GUI program to get it to accept the changes.... is there any way around this? Or would that mean having to use dynamic linking?

BTW.. thanks for you help.... its been really great!

yogeshgokul
12th August 2009, 13:49
However, if I make any changes to disspertionLib then I have to re-compile my GUI program to get it to accept the changes.... is there any way around this? Or would that mean having to use dynamic linking?
You are right. if you are working with static libs, then you have to re-compile user program also. Otherwise go for dynamic lib. But in dynamic libs, there are many more issues, like exporting, importing, symbol resolution, library loading.

mrwooster
12th August 2009, 13:54
AH!!!!! Finally!! It is working as I wanted it....

This is how I did it:

I removed the CONFIG += staticlib from the .pro file of my library.... this then compiled it into a dll..... not a .a file

I placed the disspertion.dll in a folder named libs and the header files in libs/headers

the .pro file for my GUI looks like this:

INCLUDEPATH = "C:/........./c++/libs/headers"
LIBS += -L"C:/........./c++/libs"
LIBS += -ldisspertionLib

And it compiles fine.

I then copied the disspertionLib.dll into the folder containing the .exe of the GUI application and it runs fine....

If I change anything in disspertionLib and re-compile the dll.... I just copy it back into the directory of the GUI .exe and the changes are shown when I run the GUI without having to re-compile it....

Seems to work fine.... is this an acceptable way of doing it? I had the impression that implementing .dll s was supposed to be much more complicated?

yogeshgokul
12th August 2009, 14:02
You can do one more thing.
Set the TARGETPATH of your dll's .pro file. Then it will automatically generate dll in your exe path. So you doesn't need to copy and paste again and again.

mrwooster
12th August 2009, 14:19
Thanks... thats a good idea....

Just out of interest... how come I can use a dll like this? I thought that a dll required a much more complicated setup procedure and calling functions from dlls required setting up pointers etc... and using the windows API?

Ty

yogeshgokul
12th August 2009, 14:24
how come I can use a dll like this? I thought that a dll required a much more complicated setup procedure and calling functions from dlls required setting up pointers etc... and using the windows API?
Ty
This is a big topic, please google some docs for dynamic libraries. There is adequate information available.
QLibrary is used to load DLLs and resolve any symbol from DLL.