I'm looking for a way to use Qt developped dll(and .so or .a in Linux) into a pure C++ project without to include every Qt .h headerfile.
Any suggestion??
Obviously you cannot build a shared object that uses Qt without including Qt headers and linking to Qt libraries. You can, however, build a program that uses the shared object with some careful construction:
- Don't use Qt types in your exposed API
- Only use Qt internally to your library implementation
- Use a private implementation pattern to hide any Qt-specific portions from the consumers of your API header file.
You must have the Qt libraries to run the result. You also have complications if you need a Qt event loop.
Here is an example library:
The library header used by consumers. Notice that there are no references to Qt.
#ifndef TEST_H
#define TEST_H
#include <string>
class TestImpl;
class Test {
public:
Test();
~Test();
std::string doStuff(const std::string &s);
private:
TestImpl *pimpl; // private implementation
};
#endif
#ifndef TEST_H
#define TEST_H
#include <string>
class TestImpl;
class Test {
public:
Test();
~Test();
std::string doStuff(const std::string &s);
private:
TestImpl *pimpl; // private implementation
};
#endif
To copy to clipboard, switch view to plain text mode
The implementation of same. This is Qt dependent to build.
#include "test.h"
#include <QString>
class TestImpl {
public:
TestImpl() { }
~TestImpl() { }
std::string doStuff(const std::string &s)
{
return b.toStdString();
}
};
Test::Test(): pimpl(new TestImpl)
{
}
Test::~Test()
{
delete pimpl;
}
std::string Test::doStuff(const std::string &s)
{
return pimpl->doStuff(s);
}
#include "test.h"
#include <QString>
class TestImpl {
public:
TestImpl() { }
~TestImpl() { }
std::string doStuff(const std::string &s)
{
QString a = QString::fromStdString(s);
QString b = a.toUpper();
return b.toStdString();
}
};
Test::Test(): pimpl(new TestImpl)
{
}
Test::~Test()
{
delete pimpl;
}
std::string Test::doStuff(const std::string &s)
{
return pimpl->doStuff(s);
}
To copy to clipboard, switch view to plain text mode
and the PRO file to build the lib:
TEMPLATE = lib
QT -= gui
TARGET = test
HEADERS = test.h
SOURCES = test.cpp
TEMPLATE = lib
QT -= gui
TARGET = test
HEADERS = test.h
SOURCES = test.cpp
To copy to clipboard, switch view to plain text mode
Here is a client program that uses the library:
#include <string>
#include <iostream>
#include "test.h"
int main(int argc, char *argv[])
{
Test t;
std::string s("test String");
std::cout << s << std::endl;
std::cout << t.doStuff(s) << std::endl;
return 0;
}
#include <string>
#include <iostream>
#include "test.h"
int main(int argc, char *argv[])
{
Test t;
std::string s("test String");
std::cout << s << std::endl;
std::cout << t.doStuff(s) << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
and an example build: (notice no reference to Qt_
$ g++ -Ilib -Llib -ltest -o main main.cpp
$ g++ -Ilib -Llib -ltest -o main main.cpp
To copy to clipboard, switch view to plain text mode
Now to run it
$ export LD_LIBRARY_PATH=./lib
$ ldd main
linux-gate.so.1 => (0xffffe000)
libtest.so.1 => ./lib/libtest.so.1 (0xb778b000)
libstdc++.so.6 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libstdc++.so.6 (0xb7679000)
libm.so.6 => /lib/libm.so.6 (0xb7653000)
libgcc_s.so.1 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libgcc_s.so.1 (0xb7635000)
libc.so.6 => /lib/libc.so.6 (0xb74da000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb74c1000)
libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0xb7226000)
libgthread-2.0.so.0 => /usr/lib/libgthread-2.0.so.0 (0xb7220000)
librt.so.1 => /lib/librt.so.1 (0xb7217000)
libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0xb7115000)
/lib/ld-linux.so.2 (0xb778f000)
libz.so.1 => /lib/libz.so.1 (0xb7100000)
libdl.so.2 => /lib/libdl.so.2 (0xb70fc000)
$ ./main
test String
TEST STRING
$ export LD_LIBRARY_PATH=./lib
$ ldd main
linux-gate.so.1 => (0xffffe000)
libtest.so.1 => ./lib/libtest.so.1 (0xb778b000)
libstdc++.so.6 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libstdc++.so.6 (0xb7679000)
libm.so.6 => /lib/libm.so.6 (0xb7653000)
libgcc_s.so.1 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libgcc_s.so.1 (0xb7635000)
libc.so.6 => /lib/libc.so.6 (0xb74da000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb74c1000)
libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0xb7226000)
libgthread-2.0.so.0 => /usr/lib/libgthread-2.0.so.0 (0xb7220000)
librt.so.1 => /lib/librt.so.1 (0xb7217000)
libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0xb7115000)
/lib/ld-linux.so.2 (0xb778f000)
libz.so.1 => /lib/libz.so.1 (0xb7100000)
libdl.so.2 => /lib/libdl.so.2 (0xb70fc000)
$ ./main
test String
TEST STRING
To copy to clipboard, switch view to plain text mode
Bookmarks