PDA

View Full Version : Creating and Using a Static Library (QT4.7.4, QT Creator 2.2.1, Red Hat Linux 6.1)



bruceariggs
27th August 2011, 21:40
I've been browsing the forums, and I've found a few posts that seem to address the same problem I'm having, but after implementing the solutions for those people, I still have problems.

I am trying to create a static library with one .pro file, and then use that library in another .pro file.

I created an example project to see if I could get it working. Here is the folder layout:

Example
--bin (Where the app and library build)
--LibraryProject
----LibraryProject // (contains LibraryProject.pro and all the header/source files)
----LibraryProject-build-desktop
--SampleProject
----SampleProject // (contains main.cpp hello world, would like to use the lib here)
----SampleProject-build-desktop

This is my LibraryProject.pro file

QT += core gui

TEMPLATE = lib
CONFIG += staticlib

HEADERS += \
SimpleClass.h

SOURCES += \
SimpleClass.cpp

TARGET = MyLibrary
DESTDIR = ../../bin/

This is the SimpleClass .h file (in the Library)

#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H

class SimpleClass
{
public:
SimpleClass();
int ReturnNumber();
};

#endif // SIMPLECLASS_H

This is the SimpleClass.cpp file (in the library)

#include "simpleclass.h"

SimpleClass::SimpleClass()
{
}

int SimpleClass::ReturnNumber()
{
return 0;
}

This is the SampleProject.pro file

LIBS += -L../../bin/ -lMyLibrary

INCLUDEPATH = ../../LibraryProject/LibraryProject

SOURCES += \
main.cpp

DESTDIR += ../../bin

This is the main.cpp (In the Sample project)

#include <iostream>

//How do I #include and use this library??
//My #includes yield no .h's or libraries
//that I made available to me... using
//them anyways causes "not found" errors.

using namespace std;

int main()
{
//Would love to use the library right here
//SampleClass...
return 0;
}

It seems no matter how I try to include the library...

LIBS += -L../../bin/ -lMyLibrary
INCLUDEPATH += ../../LibraryProject/LibraryProject/

or...

LIBS += ../../bin/libMyLibrary.a

or

The horrible Right-Click "Add Library" huge block of text...

No matter what method I try, the library is not found in the main.cpp...

What am I doing wrong?

(Included on this post should be the very example I just described)

(Crap, it's actually the UPDATEDExample.zip, not the Example.zip... but where the heck is the DELETE Attachment????)

norobro
28th August 2011, 01:18
Your updated example worked fine using Qt 4.7.4 & Qt Creator 2.2.1 on my Debian Sid system.
#include <QDebug>
#include "SimpleClass.h"

int main()
{
SimpleClass sc;
qDebug() << sc.ReturnNumber();
return 0;
}
Don't know anything about Red Hat but maybe this will help: link (http://www.qtcentre.org/threads/28359-Setting-LD_LIBRARY_PATH-from-Qt-Creator)