Results 1 to 8 of 8

Thread: using Qt dll into C++ project

  1. #1
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face using Qt dll into C++ project

    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??
    Thanks in advance
    Andrea

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qt dll into C++ project

    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.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: using Qt dll into C++ project

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using Qt dll into C++ project

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: using Qt dll into C++ project

    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.
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <string>
    5.  
    6. class TestImpl;
    7.  
    8. class Test {
    9. public:
    10. Test();
    11. ~Test();
    12. std::string doStuff(const std::string &s);
    13.  
    14. private:
    15. TestImpl *pimpl; // private implementation
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    The implementation of same. This is Qt dependent to build.
    Qt Code:
    1. #include "test.h"
    2.  
    3. #include <QString>
    4.  
    5.  
    6. class TestImpl {
    7. public:
    8. TestImpl() { }
    9. ~TestImpl() { }
    10.  
    11.  
    12. std::string doStuff(const std::string &s)
    13. {
    14. QString a = QString::fromStdString(s);
    15. QString b = a.toUpper();
    16. return b.toStdString();
    17. }
    18. };
    19.  
    20.  
    21. Test::Test(): pimpl(new TestImpl)
    22. {
    23. }
    24.  
    25. Test::~Test()
    26. {
    27. delete pimpl;
    28. }
    29.  
    30. std::string Test::doStuff(const std::string &s)
    31. {
    32. return pimpl->doStuff(s);
    33. }
    To copy to clipboard, switch view to plain text mode 
    and the PRO file to build the lib:
    Qt Code:
    1. TEMPLATE = lib
    2. QT -= gui
    3. TARGET = test
    4.  
    5. HEADERS = test.h
    6. SOURCES = test.cpp
    To copy to clipboard, switch view to plain text mode 

    Here is a client program that uses the library:
    Qt Code:
    1. #include <string>
    2. #include <iostream>
    3.  
    4. #include "test.h"
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. Test t;
    10. std::string s("test String");
    11.  
    12. std::cout << s << std::endl;
    13. std::cout << t.doStuff(s) << std::endl;
    14.  
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 
    and an example build: (notice no reference to Qt_
    Qt Code:
    1. $ g++ -Ilib -Llib -ltest -o main main.cpp
    To copy to clipboard, switch view to plain text mode 

    Now to run it
    Qt Code:
    1. $ export LD_LIBRARY_PATH=./lib
    2. $ ldd main
    3. linux-gate.so.1 => (0xffffe000)
    4. libtest.so.1 => ./lib/libtest.so.1 (0xb778b000)
    5. libstdc++.so.6 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libstdc++.so.6 (0xb7679000)
    6. libm.so.6 => /lib/libm.so.6 (0xb7653000)
    7. libgcc_s.so.1 => /usr/lib/gcc/i686-pc-linux-gnu/4.4.5/libgcc_s.so.1 (0xb7635000)
    8. libc.so.6 => /lib/libc.so.6 (0xb74da000)
    9. libpthread.so.0 => /lib/libpthread.so.0 (0xb74c1000)
    10. libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0xb7226000)
    11. libgthread-2.0.so.0 => /usr/lib/libgthread-2.0.so.0 (0xb7220000)
    12. librt.so.1 => /lib/librt.so.1 (0xb7217000)
    13. libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0xb7115000)
    14. /lib/ld-linux.so.2 (0xb778f000)
    15. libz.so.1 => /lib/libz.so.1 (0xb7100000)
    16. libdl.so.2 => /lib/libdl.so.2 (0xb70fc000)
    17.  
    18. $ ./main
    19. test String
    20. TEST STRING
    To copy to clipboard, switch view to plain text mode 

  6. The following 2 users say thank you to ChrisW67 for this useful post:

    David812 (31st August 2011), gkarthick5 (25th August 2011)

  7. #6
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qt dll into C++ project

    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

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: using Qt dll into C++ project

    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 to check.

  9. #8
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using Qt dll into C++ project

    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!

Similar Threads

  1. How to convert QT Project to CMake project?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2011, 09:48
  2. How to add a lib to a qt project
    By WilliamSpiderWeb in forum Newbie
    Replies: 37
    Last Post: 18th March 2011, 23:26
  3. Qt project management - bigger project
    By Peppy in forum Qt Programming
    Replies: 11
    Last Post: 24th December 2010, 13:50
  4. Replies: 1
    Last Post: 3rd December 2009, 23:34
  5. Project generation on linux from QT Project
    By darpan in forum Qt Programming
    Replies: 6
    Last Post: 11th December 2006, 09:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.