Results 1 to 6 of 6

Thread: Creating a DLL using Qt

  1. #1
    Join Date
    Oct 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating a DLL using Qt

    Hi!

    I am learning to create a DLL which will be used from no-qt apps and I have a doubt.

    My dll has only a cpp file:

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. void hello3() {
    5. cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
    6. }
    7.  
    8. void hello2() {
    9. cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
    10. }
    To copy to clipboard, switch view to plain text mode 

    If I build the dll with that cpp file then my dll exports the following symbols:
    Qt Code:
    1. _CRT_MT
    2. _Z6hello2v
    3. _Z6hello3v
    4. _get_output_format
    To copy to clipboard, switch view to plain text mode 

    It exports all the functions inside the dll.


    But if I add export declaration to one of my functions
    Qt Code:
    1. void hello3() {
    2. cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
    3. }
    4.  
    5. extern "C" __declspec(dllexport) void hello2() {
    6. cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
    7. }
    To copy to clipboard, switch view to plain text mode 

    then my dll exports only the symbol:
    Qt Code:
    1. hello2
    To copy to clipboard, switch view to plain text mode 

    It only exports the functions I have delcared explicitily as exported. Why??



    Finally, if I add the 'extern "C"' to all my functions
    Qt Code:
    1. extern "C" void hello3() {
    2. cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
    3. }
    4.  
    5. extern "C" void hello2() {
    6. cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
    7. }
    To copy to clipboard, switch view to plain text mode 

    then the symbols exported are:

    Qt Code:
    1. _CRT_MT
    2. _get_output_format
    3. hello2
    4. hello3
    To copy to clipboard, switch view to plain text mode 


    I don´t understand why qt has this behaviour.
    Can you help me?

    Regards.

  2. #2
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a DLL using Qt

    Hi, This is not Qt Behavior. Please learn what is name mangling and when to use extern "C"

  3. #3
    Join Date
    Oct 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a DLL using Qt

    Hi,

    I know what name mangling is and the use of extern c

    My two questions are

    why does qt export all the functions inside the dll by default?

    and why does qt export only the functions with '__declspec(dllexport)' declarations if I use it?

    Regards

  4. #4
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a DLL using Qt

    Althor,
    why does qt export all the functions inside the dll by default?
    1. Qt is not used/responsible to create your dll. It is just a toolkit to create gui's and do other common task in platform independent way.
    2. Your compiler creates the dll. In windows you have to use __declspec(dllexport) or use a .def file to export a function from a dll.

    The following link may be useful... ?

    Creating a DLL using Qt is different from Creating a DLL to use with Qt App.

  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: Creating a DLL using Qt

    Quote Originally Posted by Althor View Post
    I don´t understand why qt has this behaviour.
    Can you help me?
    Regards.
    The behaviour you describe has nothing to do with Qt. This is a behaviour of your compiler and linker. I assume from your conflation of Qt and compiler that you are using the Qt SDK for Windows, which bundles the MingW includes, GNU compiler and tools. From the GNU ld manual:
    _exporting DLL symbols_
    The cygwin/mingw `ld' has several ways to export symbols for dll's.

    _using auto-export functionality_
    By default `ld' exports symbols with the auto-export
    functionality, which is controlled by the following command
    line options:

    * -export-all-symbols [This is the default]
    * -exclude-symbols
    * -exclude-libs
    * -exclude-modules-for-implib
    * -version-script

    When auto-export is in operation, `ld' will export all the
    non-local (global and common) symbols it finds in a DLL, with
    the exception of a few symbols known to belong to the
    system's runtime and libraries. As it will often not be
    desirable to export all of a DLL's symbols, which may include
    private functions that are not part of any public interface,
    the command-line options listed above may be used to filter
    symbols out from the list for exporting. The `--output-def'
    option can be used in order to see the final list of exported
    symbols with all exclusions taken into effect.

    If `--export-all-symbols' is not given explicitly on the
    command line, then the default auto-export behavior will be
    _disabled_ if either of the following are true:

    * A DEF file is used.
    * Any symbol in any object file was marked with the
    __declspec(dllexport) attribute.

  6. #6
    Join Date
    Oct 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a DLL using Qt

    Hi

    Now I undestand this behaviour. I thought that qMake was the responsible for saying to g++ what methods must be exported.

    All is clear now.

    Thanks a lot guys.
    My best regards.

Similar Threads

  1. Creating xls File
    By bismitapadhy in forum Qt Programming
    Replies: 0
    Last Post: 26th May 2009, 11:35
  2. Help creating .vcproj
    By Muzz in forum Newbie
    Replies: 5
    Last Post: 7th February 2008, 19:09
  3. Creating a customizable gui
    By TMan in forum Qt Programming
    Replies: 10
    Last Post: 23rd April 2007, 12:56
  4. creating .cfg file in qt
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2007, 10:26
  5. Help creating a PDF doc
    By Dark_Tower in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2006, 12:43

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.