Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Creating Shared Libraries

The following sections list certain things that should be taken into account when creating shared libraries.

Using Symbols from Shared Libraries

Symbols - functions, variables or classes - contained in shared libraries intended to be used by clients, such as applications or other libraries, must be marked in a special way. These symbols are called public symbols that are exported or made publicly visible.

The remaining symbols should not be visible from the outside. On most platforms, compilers will hide them by default. On some platforms, a special compiler option is required to hide these symbols.

When compiling a shared library, it must be marked for export. To use the shared library from a client, some platforms may require a special import declaration as well.

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.

Now, we need to ensure that the right macro is invoked -- whether we compile a shared library itself, or just the client using the shared library. Typically, this can be solved by adding a special header.

Let us assume we want to create a shared library called mysharedlib. A special header for this library, mysharedlib_global.h, looks like this:

 #include <QtCore/QtGlobal>

 #if defined(MYSHAREDLIB_LIBRARY)
 #  define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
 #else
 #  define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
 #endif

In the .pro file of the shared library, we add:

 DEFINES += MYSHAREDLIB_LIBRARY

In each header of the library, we specify the following:

 #include "mysharedlib_global.h"

 MYSHAREDLIB_EXPORT void foo();
 class MYSHAREDLIB_EXPORT MyClass...

This ensures that the right macro is seen by both library and clients. We also use this technique in Qt's sources.

Header File Considerations

Typically, clients will include only the public header files of shared libraries. These libraries might be installed in a different location, when deployed. Therefore, it is important to exclude other internal header files that were used when building the shared library.

For example, the library might provide a class that wraps a hardware device and contains a handle to that device, provided by some 3rd-party library:

 #include <footronics/device.h>

 class MyDevice {
 private:
     FOOTRONICS_DEVICE_HANDLE handle;
 };

A similar situation arises with forms created by Qt Designer when using aggregation or multiple inheritance:

 #include "ui_widget.h"

 class MyWidget : public QWidget {
 private:
     Ui::MyWidget m_ui;
 };

When deploying the library, there should be no dependency to the internal headers footronics/device.h or ui_widget.h.

This can be avoided by making use of the Pointer to implementation idiom described in various C++ programming books. For classes with value semantics, consider using QSharedDataPointer.

Binary compatibility

For clients loading a shared library, to work correctly, the memory layout of the classes being used must match exactly the memory layout of the library version that was used to compile the client. In other words, the library found by the client at runtime must be binary compatible with the version used at compile time.

This is usually not a problem if the client is a self-contained software package that ships all the libraries it needs.

However, if the client application relies on a shared library that belongs to a different installation package or to the operating system, then we need to think of a versioning scheme for shared libraries and decide at which level Binary compatibility is to be maintained. For example, Qt libraries of the same major version number are guaranteed to be binary compatible.

Maintaining Binary compatibility places some restrictions on the changes you can make to the classes. A good explanation can be found at KDE - Policies/Binary Compatibility Issues With C++. These issues should be considered right from the start of library design. We recommend that the principle of Information hiding and the Pointer to implementation technique be used wherever possible.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web