Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is Q_DECL_EXPORT macro necessary?
Forum Updated to NodeBB v4.3 + New Features

Is Q_DECL_EXPORT macro necessary?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 2.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by
    #1

    Hi,

    I create simple, shared library and simple app, which use that library.

    Libary:

    #ifndef BIBLIOTEKANAZWA_H
    #define BIBLIOTEKANAZWA_H
    
    #include "bibliotekaNazwa_global.h"
    
    extern "C" int suma(int a,int b);
    
    #endif // BIBLIOTEKANAZWA_H
    

    App:

    #include "bibliotekanazwa.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        qInfo()<<suma(2,3);
    }
    

    Of course I added INCLUDEPATH and LIBS to .pro file. And it works - I see correct result of function suma.

    But in docs I see:

    Depending on your target platform, Qt provides special macros that contain the necessary definitions:
    
    Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library.
    Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library.
    

    I don't add Q_DECL_EXPORT \ BIBLIOTEKANAZWA_EXPORT, and it works. So I have to add this macro?

    JKSHJ 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #5

      The defaults for symbols visibility are platform and toolchain specific.

      On Windows with MSVC all symbols are not exported by default and require explicit marking with __declspec(dllexport). If you compile with MSVC your function will not be exported the way you have it now.

      With MinGW it's a bit more complicated. Symbols are by default exported, so require no extra annotation and that's what you're seeing. MinGW also has a linker flag that lets you hide all exports by default, like MSVC: -fvisibility=hidden. When you use it you need to explicitly mark a symbol to be exported with attribute __attribute__ ((dllexport)). There's a weird quirk though that if you don't explicitly mark any symbol for export all symbols will be exported whether the linker flag is passed or not (probably for compatibility with older code).

      In general it's a good idea to keep your library interface as minimal as possible. This is good for internal optimization of the library and allows for better control over future modifications you might want to make. My suggestion is to always make your libraries not export symbols by default.

      Q_DECL_EXPORT is a macro that expands to either the MSVC or MinGW specific attribute to let you write portable code, so you should use it. Similarly, if you're using qmake, you can add CONFIG += hide_symbols in your pro file to add the toolchain specific linker flags to hide symbols by default.

      1 Reply Last reply
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @TomNow99 said in Is Q_DECL_EXPORT macro necessary?:

        and it works

        Is it a shared library?
        Your extern "C" make it visible from the outside. But this works only for C functions.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by TomNow99
          #3

          @Christian-Ehrlicher
          Hmmm,

          After build that "library" I have 3 files:
          bibliotekaNazwa.dll
          bibliotekanazwa.o
          libbibliotekaNazwa.a

          And I add this files to my app like this:
          INCLUDEPATH+="C:/Users/tom/Documents/bibliotekaNazwa"
          LIBS += -LC:\Users\tom\Documents\build-bibliotekaNazwa-Desktop_Qt_5_15_1_MinGW_64_bit-Release\release
          LIBS += -lbibliotekanazwa

          I add folder C:\Users\tom\Documents\build-bibliotekaNazwa-Desktop_Qt_5_15_1_MinGW_64_bit-Release\release to path.

          EDIT:
          So I think this is shared library

          1 Reply Last reply
          0
          • T TomNow99

            Hi,

            I create simple, shared library and simple app, which use that library.

            Libary:

            #ifndef BIBLIOTEKANAZWA_H
            #define BIBLIOTEKANAZWA_H
            
            #include "bibliotekaNazwa_global.h"
            
            extern "C" int suma(int a,int b);
            
            #endif // BIBLIOTEKANAZWA_H
            

            App:

            #include "bibliotekanazwa.h"
            #include <QDebug>
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                qInfo()<<suma(2,3);
            }
            

            Of course I added INCLUDEPATH and LIBS to .pro file. And it works - I see correct result of function suma.

            But in docs I see:

            Depending on your target platform, Qt provides special macros that contain the necessary definitions:
            
            Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library.
            Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library.
            

            I don't add Q_DECL_EXPORT \ BIBLIOTEKANAZWA_EXPORT, and it works. So I have to add this macro?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #4

            @TomNow99 said in Is Q_DECL_EXPORT macro necessary?:

            Of course I added INCLUDEPATH and LIBS to .pro file. And it works - I see correct result of function suma.

            As @Christian-Ehrlicher said, you can see suma because you used extern "C".

            However, notice that you cannot see MainWindow.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            1
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #5

              The defaults for symbols visibility are platform and toolchain specific.

              On Windows with MSVC all symbols are not exported by default and require explicit marking with __declspec(dllexport). If you compile with MSVC your function will not be exported the way you have it now.

              With MinGW it's a bit more complicated. Symbols are by default exported, so require no extra annotation and that's what you're seeing. MinGW also has a linker flag that lets you hide all exports by default, like MSVC: -fvisibility=hidden. When you use it you need to explicitly mark a symbol to be exported with attribute __attribute__ ((dllexport)). There's a weird quirk though that if you don't explicitly mark any symbol for export all symbols will be exported whether the linker flag is passed or not (probably for compatibility with older code).

              In general it's a good idea to keep your library interface as minimal as possible. This is good for internal optimization of the library and allows for better control over future modifications you might want to make. My suggestion is to always make your libraries not export symbols by default.

              Q_DECL_EXPORT is a macro that expands to either the MSVC or MinGW specific attribute to let you write portable code, so you should use it. Similarly, if you're using qmake, you can add CONFIG += hide_symbols in your pro file to add the toolchain specific linker flags to hide symbols by default.

              1 Reply Last reply
              4
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi,

                In addition to @Chris-Kawa great explanation, you can find how to properly use these macro in the create a shared library with Qt chapter in the Qt documentation.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved