~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qlibrary.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/corelib/plugin/qlibrary.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QLibrary Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QLibrary Class Reference</h1>
 
21
<p>The QLibrary class loads shared libraries at runtime. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QLibrary&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p>Inherits <a href="qobject.html">QObject</a>.</p>
 
24
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
 
25
<ul>
 
26
<li><a href="qlibrary-members.html">List of all members, including inherited members</a></li>
 
27
<li><a href="qlibrary-qt3.html">Qt 3 support members</a></li>
 
28
</ul>
 
29
<a name="properties"></a>
 
30
<h3>Properties</h3>
 
31
<ul>
 
32
<li><div class="fn"/><b><a href="qlibrary.html#fileName-prop">fileName</a></b> : QString</li>
 
33
</ul>
 
34
<ul>
 
35
<li><div class="fn"/>1 property inherited from <a href="qobject.html#properties">QObject</a></li>
 
36
</ul>
 
37
<a name="public-functions"></a>
 
38
<h3>Public Functions</h3>
 
39
<ul>
 
40
<li><div class="fn"/><b><a href="qlibrary.html#QLibrary">QLibrary</a></b> ( QObject * <i>parent</i> = 0 )</li>
 
41
<li><div class="fn"/><b><a href="qlibrary.html#QLibrary-2">QLibrary</a></b> ( const QString &amp; <i>fileName</i>, QObject * <i>parent</i> = 0 )</li>
 
42
<li><div class="fn"/><b><a href="qlibrary.html#dtor.QLibrary">~QLibrary</a></b> ()</li>
 
43
<li><div class="fn"/>QString <b><a href="qlibrary.html#fileName-prop">fileName</a></b> () const</li>
 
44
<li><div class="fn"/>bool <b><a href="qlibrary.html#isLoaded">isLoaded</a></b> () const</li>
 
45
<li><div class="fn"/>bool <b><a href="qlibrary.html#load">load</a></b> ()</li>
 
46
<li><div class="fn"/>void * <b><a href="qlibrary.html#resolve">resolve</a></b> ( const char * <i>symbol</i> )</li>
 
47
<li><div class="fn"/>void <b><a href="qlibrary.html#fileName-prop">setFileName</a></b> ( const QString &amp; <i>fileName</i> )</li>
 
48
<li><div class="fn"/>bool <b><a href="qlibrary.html#unload">unload</a></b> ()</li>
 
49
</ul>
 
50
<ul>
 
51
<li><div class="fn"/>28 public functions inherited from <a href="qobject.html#public-functions">QObject</a></li>
 
52
</ul>
 
53
<a name="static-public-members"></a>
 
54
<h3>Static Public Members</h3>
 
55
<ul>
 
56
<li><div class="fn"/>bool <b><a href="qlibrary.html#isLibrary">isLibrary</a></b> ( const QString &amp; <i>fileName</i> )</li>
 
57
<li><div class="fn"/>void * <b><a href="qlibrary.html#resolve-2">resolve</a></b> ( const QString &amp; <i>fileName</i>, const char * <i>symbol</i> )</li>
 
58
</ul>
 
59
<ul>
 
60
<li><div class="fn"/>4 static public members inherited from <a href="qobject.html#static-public-members">QObject</a></li>
 
61
</ul>
 
62
<h3>Additional Inherited Members</h3>
 
63
<ul>
 
64
<li><div class="fn"/>1 public slot inherited from <a href="qobject.html#public-slots">QObject</a></li>
 
65
<li><div class="fn"/>1 signal inherited from <a href="qobject.html#signals">QObject</a></li>
 
66
<li><div class="fn"/>7 protected functions inherited from <a href="qobject.html#protected-functions">QObject</a></li>
 
67
</ul>
 
68
<a name="details"></a>
 
69
<hr />
 
70
<h2>Detailed Description</h2>
 
71
<p>The QLibrary class loads shared libraries at runtime.</p>
 
72
<p>An instance of a QLibrary object operates on a single shared object file (which we call a &quot;library&quot;, but is also known as a &quot;DLL&quot;). A QLibrary provides access to the functionality in the library in a platform independent way. You can either pass a file name in the constructor, or set it explicitly with <a href="qlibrary.html#fileName-prop">setFileName</a>(). When loading the library, QLibrary searches in all the system-specific library locations (e.g. <tt>LD_LIBRARY_PATH</tt> on Unix), unless the file name has an absolute path. If the file cannot be found, QLibrary tries the name with different platform-specific file suffixes, like &quot;.so&quot; on Unix, &quot;.dylib&quot; on the Mac, or &quot;.dll&quot; on Windows. This makes it possible to specify shared libraries that are only identified by their basename (i.e. without their suffix), so the same code will work on different operating systems.</p>
 
73
<p>The most important functions are <a href="qlibrary.html#load">load</a>() to dynamically load the library file, <a href="qlibrary.html#isLoaded">isLoaded</a>() to check whether loading was successful, and <a href="qlibrary.html#resolve">resolve</a>() to resolve a symbol in the library. The <a href="qlibrary.html#resolve">resolve</a>() function implicitly tries to load the library if it has not been loaded yet. Multiple instances of QLibrary can be used to access the same physical library. Once loaded, libraries remain in memory until the application terminates. You can attempt to unload a library using <a href="qlibrary.html#unload">unload</a>(), but if other instances of QLibrary are using the same library, the call will fail, and unloading will only happen when every instance has called <a href="qlibrary.html#unload">unload</a>().</p>
 
74
<p>A typical use of QLibrary is to resolve an exported symbol in a library, and to call the C function that this symbol represents. This is called &quot;explicit linking&quot; in contrast to &quot;implicit linking&quot;, which is done by the link step in the build process when linking an executable against a library.</p>
 
75
<p>The following code snippet loads a library, resolves the symbol &quot;mysymbol&quot;, and calls the function if everything succeeded. If something goes wrong, e.g. the library file does not exist or the symbol is not defined, the function pointer will be 0 and won't be called.</p>
 
76
<pre>&nbsp;   QLibrary myLib(&quot;mylib&quot;);
 
77
    typedef void (*MyPrototype)();
 
78
    MyPrototype myFunction = (MyPrototype) myLib.resolve(&quot;mysymbol&quot;);
 
79
    if (myFunction)
 
80
        myFunction();</pre>
 
81
<p>The symbol must be exported as a C function from the library for <a href="qlibrary.html#resolve">resolve</a>() to work. This means that the function must be wrapped in an <tt>extern &quot;C&quot;</tt> block if the library is compiled with a C++ compiler. On Windows, this also requires the use of a <tt>dllexport</tt> macro; see <a href="qlibrary.html#resolve">resolve</a>() for the details of how this is done. For convenience, there is a static <a href="qlibrary.html#resolve">resolve</a>() function which you can use if you just want to call a function in a library without explicitly loading the library first:</p>
 
82
<pre>&nbsp;   typedef void (*MyPrototype)();
 
83
    MyPrototype myFunction =
 
84
            (MyPrototype) QLibrary::resolve(&quot;mylib&quot;, &quot;mysymbol&quot;);
 
85
    if (myFunction)
 
86
        myFunction();</pre>
 
87
<p>See also <a href="qpluginloader.html">QPluginLoader</a>.</p>
 
88
<hr />
 
89
<h2>Property Documentation</h2>
 
90
<h3 class="fn"><a name="fileName-prop"></a>fileName : <a href="qstring.html">QString</a></h3>
 
91
<p>This property holds the file name of the library.</p>
 
92
<p>We recommend omitting the file's suffix in the file name, since <a href="qlibrary.html">QLibrary</a> will automatically look for the file with the appropriate suffix (see <a href="qlibrary.html#isLibrary">isLibrary</a>()).</p>
 
93
<p>When loading the library, <a href="qlibrary.html">QLibrary</a> searches in all system-specific library locations (e.g. <tt>LD_LIBRARY_PATH</tt> on Unix), unless the file name has an absolute path. After loading the library successfully, fileName() returns the fully qualified file name of the library. For example, after successfully loading the &quot;GL&quot; library on unix, fileName() will return &quot;libGL.so&quot;.</p>
 
94
<p>Access functions:</p>
 
95
<ul>
 
96
<li><div class="fn"/><b>QString fileName () const</b></li>
 
97
<li><div class="fn"/><b>void setFileName ( const QString &amp; <i>fileName</i> )</b></li>
 
98
</ul>
 
99
<hr />
 
100
<h2>Member Function Documentation</h2>
 
101
<h3 class="fn"><a name="QLibrary"></a>QLibrary::QLibrary ( <a href="qobject.html">QObject</a> * <i>parent</i> = 0 )</h3>
 
102
<p>Constructs a library with the given <i>parent</i>.</p>
 
103
<h3 class="fn"><a name="QLibrary-2"></a>QLibrary::QLibrary ( const <a href="qstring.html">QString</a> &amp; <i>fileName</i>, <a href="qobject.html">QObject</a> * <i>parent</i> = 0 )</h3>
 
104
<p>Constructs a library object with the given <i>parent</i> that will load the library specified by <i>fileName</i>.</p>
 
105
<p>We recommend omitting the file's suffix in <i>fileName</i>, since <a href="qlibrary.html">QLibrary</a> will automatically look for the file with the appropriate suffix in accordance with the platform, e.g. &quot;.so&quot; on Unix, &quot;.dylib&quot; on Mac OS X, and &quot;.dll&quot; on Windows. (See <a href="qlibrary.html#fileName-prop">fileName</a>.)</p>
 
106
<h3 class="fn"><a name="dtor.QLibrary"></a>QLibrary::~QLibrary ()</h3>
 
107
<p>Destroys the <a href="qlibrary.html">QLibrary</a> object.</p>
 
108
<p>Unless <a href="qlibrary.html#unload">unload</a>() was called explicitly, the library stays in memory until the application terminates.</p>
 
109
<p>See also <a href="qlibrary.html#isLoaded">isLoaded</a>() and <a href="qlibrary.html#unload">unload</a>().</p>
 
110
<h3 class="fn"><a name="isLibrary"></a>bool QLibrary::isLibrary ( const <a href="qstring.html">QString</a> &amp; <i>fileName</i> )&nbsp;&nbsp;<tt> [static]</tt></h3>
 
111
<p>Returns true if <i>fileName</i> has a valid suffix for a loadable library; otherwise returns false.</p>
 
112
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
113
<tr valign="top" bgcolor="#a2c511"><th>Platform</th><th>Valid suffixes</th></tr>
 
114
<tr valign="top" bgcolor="#f0f0f0"><td>Windows</td><td><tt>.dll</tt></td></tr>
 
115
<tr valign="top" bgcolor="#e0e0e0"><td>Unix/Linux</td><td><tt>.so</tt></td></tr>
 
116
<tr valign="top" bgcolor="#f0f0f0"><td>AIX</td><td><tt>.a</tt></td></tr>
 
117
<tr valign="top" bgcolor="#e0e0e0"><td>HP-UX</td><td><tt>.sl</tt></td></tr>
 
118
<tr valign="top" bgcolor="#f0f0f0"><td>Mac OS X</td><td><tt>.dylib</tt>, <tt>.bundle</tt>, <tt>.so</tt></td></tr>
 
119
</table>
 
120
<p>Trailing versioning numbers on Unix are ignored.</p>
 
121
<h3 class="fn"><a name="isLoaded"></a>bool QLibrary::isLoaded () const</h3>
 
122
<p>Returns true if the library is loaded; otherwise returns false.</p>
 
123
<p>See also <a href="qlibrary.html#load">load</a>().</p>
 
124
<h3 class="fn"><a name="load"></a>bool QLibrary::load ()</h3>
 
125
<p>Loads the library and returns true if the library was loaded successfully; otherwise returns false. Since <a href="qlibrary.html#resolve">resolve</a>() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the library loaded in advance, in which case you would use this function.</p>
 
126
<p>On Darwin and Mac OS X this function uses code from dlcompat, part of the OpenDarwin project.</p>
 
127
<pre>&nbsp;   Copyright (c) 2002 Jorge Acereda and Peter O'Gorman.
 
128
 
 
129
    Permission is hereby granted, free of charge, to any person obtaining
 
130
    a copy of this software and associated documentation files (the
 
131
    &quot;Software&quot;), to deal in the Software without restriction, including
 
132
    without limitation the rights to use, copy, modify, merge, publish,
 
133
    distribute, sublicense, and/or sell copies of the Software, and to
 
134
    permit persons to whom the Software is furnished to do so, subject to
 
135
    the following conditions:
 
136
 
 
137
    The above copyright notice and this permission notice shall be
 
138
    included in all copies or substantial portions of the Software.
 
139
 
 
140
    THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
 
141
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
142
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
143
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
144
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
145
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
146
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</pre>
 
147
<p>See also <a href="qlibrary.html#unload">unload</a>().</p>
 
148
<h3 class="fn"><a name="resolve"></a>void * QLibrary::resolve ( const char * <i>symbol</i> )</h3>
 
149
<p>Returns the address of the exported symbol <i>symbol</i>. The library is loaded if necessary. The function returns 0 if the symbol could not be resolved or if the library could not be loaded.</p>
 
150
<p>Example:</p>
 
151
<pre>&nbsp;   typedef int (*AvgFunction)(int, int);
 
152
 
 
153
    AvgFunction avg = (AvgFunction) library-&gt;resolve(&quot;avg&quot;);
 
154
    if (avg)
 
155
        return avg(5, 8);
 
156
    else
 
157
        return -1;</pre>
 
158
<p>The symbol must be exported as a C function from the library. This means that the function must be wrapped in an <tt>extern &quot;C&quot;</tt> if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the <tt>__declspec(dllexport)</tt> compiler directive, for example:</p>
 
159
<pre>&nbsp;   extern &quot;C&quot; MY_EXPORT int avg(int a, int b)
 
160
    {
 
161
        return (a + b) / 2;
 
162
    }</pre>
 
163
<p>with <tt>MY_EXPORT</tt> defined as</p>
 
164
<pre>&nbsp;   #ifdef Q_WS_WIN
 
165
    #define MY_EXPORT __declspec(dllexport)
 
166
    #else
 
167
    #define MY_EXPORT
 
168
    #endif</pre>
 
169
<p>On Darwin and Mac OS X this function uses code from dlcompat, part of the OpenDarwin project.</p>
 
170
<pre>&nbsp;   Copyright (c) 2002 Jorge Acereda and Peter O'Gorman.
 
171
 
 
172
    Permission is hereby granted, free of charge, to any person obtaining
 
173
    a copy of this software and associated documentation files (the
 
174
    &quot;Software&quot;), to deal in the Software without restriction, including
 
175
    without limitation the rights to use, copy, modify, merge, publish,
 
176
    distribute, sublicense, and/or sell copies of the Software, and to
 
177
    permit persons to whom the Software is furnished to do so, subject to
 
178
    the following conditions:
 
179
 
 
180
    The above copyright notice and this permission notice shall be
 
181
    included in all copies or substantial portions of the Software.
 
182
 
 
183
    THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
 
184
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
185
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
186
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
187
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
188
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
189
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</pre>
 
190
<h3 class="fn"><a name="resolve-2"></a>void * QLibrary::resolve ( const <a href="qstring.html">QString</a> &amp; <i>fileName</i>, const char * <i>symbol</i> )&nbsp;&nbsp;<tt> [static]</tt></h3>
 
191
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
192
<p>Loads the library <i>fileName</i> and returns the address of the exported symbol <i>symbol</i>. Note that <i>fileName</i> should not include the platform-specific file suffix; (see <a href="qlibrary.html#fileName-prop">fileName</a>). The library remains loaded until the application exits.</p>
 
193
<p>The function returns 0 if the symbol could not be resolved or if the library could not be loaded.</p>
 
194
<p>See also <a href="qlibrary.html#resolve">resolve</a>().</p>
 
195
<h3 class="fn"><a name="unload"></a>bool QLibrary::unload ()</h3>
 
196
<p>Unloads the library and returns true if the library could be unloaded; otherwise returns false.</p>
 
197
<p>This happens automatically on application termination, so you shouldn't normally need to call this function.</p>
 
198
<p>If other instances of <a href="qlibrary.html">QLibrary</a> are using the same library, the call will fail, and unloading will only happen when every instance has called unload().</p>
 
199
<p>Note that on Mac OS X, dynamic libraries cannot be unloaded.</p>
 
200
<p>See also <a href="qlibrary.html#resolve">resolve</a>() and <a href="qlibrary.html#load">load</a>().</p>
 
201
<p /><address><hr /><div align="center">
 
202
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
203
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
204
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
205
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
206
</tr></table></div></address></body>
 
207
</html>