PDA

View Full Version : using Qt dll into C++ project



David812
7th July 2011, 11:13
Dear All,
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?? :confused:
Thanks in advance
Andrea

mcosta
7th July 2011, 11:32
First a question: "Why don't you want include Qt Headers?"

After that, the answer to your question is: "depends to the library API".
If it hide all Qt Types you don't need to include directly Qt Headers.

David812
24th August 2011, 13:35
Sorry,
I'd like include only costum Qt header files but not Qt libreries.
For example if i produce a new class neamed libMyclassname which includes Qt standard libreries, i'd like include, in the C++ project, only libMyclassname.h and liblibMyclassname.dll .
The problem is, How to call the libMyclassname constructor in a c++ project?
Do i need to call the QApplication exec() method in the c++ project??
How to start a Qt application in a C++ project??

Could someone give me an exemple???
Thanks in advance
Andrea

high_flyer
24th August 2011, 14:07
into a pure C++ project
You either use Qt or you don't.
There is no middle way.
If in your lib you are depending on Qt, you will have to include the headers and libs you depend on.

ChrisW67
25th August 2011, 02:37
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


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)
{
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);
}

and the PRO file to build the lib:


TEMPLATE = lib
QT -= gui
TARGET = test

HEADERS = test.h
SOURCES = test.cpp


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;
}

and an example build: (notice no reference to Qt_


$ g++ -Ilib -Llib -ltest -o main main.cpp


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

David812
31st August 2011, 14:42
Dear ChrisW67,
thank you very much for help!
I've tried your code and i must admit that it works. The only problem is that when i run the main.exe (in windows) it arises this line.

1 [sig] main 628 open_stackdumpfile: Dumping stack trace to main.exe.stackdump

Do you have any advice to solve the problem??

cheers
Andrea

ChrisW67
31st August 2011, 23:30
Could be a gajillion things. Is it failing this way in the IDE or when run standalone? Are all the library dependencies met? Use Dependency Walker (http://www.dependencywalker.com/) to check.

David812
1st September 2011, 10:21
Thanks again Chris,
I opened the .exe file with DependencyWalker then it arise the following line :

Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

DependencyWalker also highlighted a missing function called WNetRestoreConnectionA , by trying to google it, i found the problem could be solved by changing the compiler(which is one of the many ways to solve the problem).
Then by changing the compiler (on windows)from cygwin to mingw i solved the problem!!
Now i hope to not have problem on linux!