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
QT -= gui
TARGET = myLIB
TEMPLATE = lib
DEFINES += MYLIB_LIBRARY
SOURCES += mylib.cpp
HEADERS += mylib.h\
myLIB_global.h
To copy to clipboard, switch view to plain text mode
.h file
class MYLIBSHARED_EXPORT MyLIB {
public:
MyLIB();
int add(int x,int y);
};
extern "C"
{
int sub(int x,int y);
}
class MYLIBSHARED_EXPORT MyLIB {
public:
MyLIB();
int add(int x,int y);
};
extern "C"
{
int sub(int x,int y);
}
To copy to clipboard, switch view to plain text mode
.cpp file :
MyLIB::MyLIB()
{
}
int MyLIB::add(int x, int y)
{
return x+y;
}
int sub(int x, int y)
{
return x-y;
}
MyLIB::MyLIB()
{
}
int MyLIB::add(int x, int y)
{
return x+y;
}
int sub(int x, int y)
{
return x-y;
}
To copy to clipboard, switch view to plain text mode
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);
typedef int (*pf)(int,int);
pf fun=(pf)QLibrary::resolve("./libmyLIB.so","sub");
int x=fun(20,10);
To copy to clipboard, switch view to plain text mode
It is working.
But when i am trying to use isLoaded function it is returning false.
ok=myLib.isLoaded();
QLibrary myLib("./libmyLIB.so");
ok=myLib.isLoaded();
To copy to clipboard, switch view to plain text mode
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 ?
Bookmarks