source: trunk/doc/src/sharedlibrary.qdoc@ 551

Last change on this file since 551 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/****************************************************************************
43**
44** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
45** Contact: Qt Software Information ([email protected])
46**
47** This file is part of the Qt GUI Toolkit.
48** EDITIONS: FREE, PROFESSIONAL, ENTERPRISE
49**
50****************************************************************************/
51
52/*!
53 \group deployment
54 \page sharedlibrary.html
55 \ingroup buildsystem
56
57 \title Creating Shared Libraries
58 The following sections list certain things that should be taken into
59 account when creating shared libraries.
60
61 \section1 Using Symbols from Shared Libraries
62
63 Symbols - functions, variables or classes - contained in shared libraries
64 intended to be used by \e{clients}, such as applications or other
65 libraries, must be marked in a special way. These symbols are called
66 \e{public symbols} that are \e{exported} or made publicly visible.
67
68 The remaining symbols should not be visible from the outside. On most
69 platforms, compilers will hide them by default. On some platforms, a
70 special compiler option is required to hide these symbols.
71
72 When compiling a shared library, it must be marked for \e{export}. To use
73 the shared library from a client, some platforms may require a special
74 \e{import} declaration as well.
75
76 Depending on your target platform, Qt provides special macros that contain
77 the necessary definitions:
78 \list
79 \o \c{Q_DECL_EXPORT} must be added to the declarations of symbols used
80 when compiling a shared library.
81 \o \c{Q_DECL_IMPORT} must be added to the declarations of symbols used
82 when compiling a client that uses the shared library.
83 \endlist
84
85 Now, we need to ensure that the right macro is invoked -- whether we
86 compile a share library itself, or just the client using the shared
87 library.
88 Typically, this can be solved by adding a special header.
89
90 Let us assume we want to create a shared library called \e{mysharedlib}.
91 A special header for this library, \c{mysharedlib_global.h}, looks like
92 this:
93
94 \code
95 #include <QtCore/QtGlobal>
96
97 #if defined(MYSHAREDLIB_LIBRARY)
98 # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
99 #else
100 # define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
101 #endif
102 \endcode
103
104 In the \c{.pro} file of the shared library, we add:
105
106 \code
107 DEFINES += MYSHAREDLIB_LIBRARY
108 \endcode
109
110 In each header of the library, we specify the following:
111
112 \code
113 #include "mysharedlib_global.h"
114
115 MYSHAREDLIB_EXPORT void foo();
116 class MYSHAREDLIB_EXPORT MyClass...
117 \endcode
118 This ensures that the right macro is seen by both library and clients. We
119 also use this technique in Qt's sources.
120
121
122 \section1 Header File Considerations
123
124 Typically, clients will include only the public header files of shared
125 libraries. These libraries might be installed in a different location, when
126 deployed. Therefore, it is important to exclude other internal header files
127 that were used when building the shared library.
128
129 For example, the library might provide a class that wraps a hardware device
130 and contains a handle to that device, provided by some 3rd-party library:
131
132 \code
133 #include <footronics/device.h>
134
135 class MyDevice {
136 private:
137 FOOTRONICS_DEVICE_HANDLE handle;
138 };
139 \endcode
140
141 A similar situation arises with forms created by Qt Designer when using
142 aggregation or multiple inheritance:
143
144 \code
145 #include "ui_widget.h"
146
147 class MyWidget : public QWidget {
148 private:
149 Ui::MyWidget m_ui;
150 };
151 \endcode
152
153 When deploying the library, there should be no dependency to the internal
154 headers \c{footronics/device.h} or \c{ui_widget.h}.
155
156 This can be avoided by making use of the \e{Pointer to implementation}
157 idiom described in various C++ programming books. For classes with
158 \e{value semantics}, consider using QSharedDataPointer.
159
160
161 \section1 Binary compatibility
162
163 For clients loading a shared library, to work correctly, the memory
164 layout of the classes being used must match exactly the memory layout of
165 the library version that was used to compile the client. In other words,
166 the library found by the client at runtime must be \e{binary compatible}
167 with the version used at compile time.
168
169 This is usually not a problem if the client is a self-contained software
170 package that ships all the libraries it needs.
171
172 However, if the client application relies on a shared library that belongs
173 to a different installation package or to the operating system, then we
174 need to think of a versioning scheme for shared libraries and decide at
175 which level \e{Binary compatibility} is to be maintained. For example, Qt
176 libraries of the same \e{major version number} are guaranteed to be binary
177 compatible.
178
179 Maintaining \e{Binary compatibility} places some restrictions on the changes
180 you can make to the classes. A good explanation can be found at
181 \l{http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++}
182 {KDE - Policies/Binary Compatibility Issues With C++}. These issues should
183 be considered right from the start of library design.
184 We recommend that the principle of \e{Information hiding} and the
185 \e{Pointer to implementation} technique be used wherever possible.
186*/
Note: See TracBrowser for help on using the repository browser.