PDA

View Full Version : create and use shared library in qt



bibhukalyana
7th May 2011, 08:59
Hi everyone,
I tried to create and use a shared library in qt.I used the c++ library project to create this.

code used to create a shared library:

.pro file:



QT -= gui

TARGET = myLIB
TEMPLATE = lib

DEFINES += MYLIB_LIBRARY

SOURCES += mylib.cpp

HEADERS += mylib.h\
myLIB_global.h


.h file



class MYLIBSHARED_EXPORT MyLIB {
public:
MyLIB();
int add(int x,int y);
};

extern "C"
{
int sub(int x,int y);
}


.cpp file :



MyLIB::MyLIB()
{
}
int MyLIB::add(int x, int y)
{
return x+y;
}

int sub(int x, int y)
{
return x-y;
}



In my test application i am able to use sub function.code is


typedef int (*pf)(int,int);
pf fun=(pf)QLibrary::resolve("./libmyLIB.so","sub");
int x=fun(20,10);

It is working.
But when i am trying to use isLoaded function it is returning false.


QLibrary myLib("./libmyLIB.so");
ok=myLib.isLoaded();



My problem is i want to use the function directly without using QLibrary.(only adding in .pro file)
How i will use add function in my application ?

Lykurg
7th May 2011, 09:35
See in the qmake documentation for LIBS, include the header file of your library and use your library classes as usual.

bibhukalyana
7th May 2011, 11:02
thanks for reply.Can you tell me what should be written in .h file.One prototype or any thing extra.

Lykurg
7th May 2011, 11:20
In your case you probably only have to use
#include "mylib.h"

bibhukalyana
8th May 2011, 08:48
thanks it is working.