96814a70964899c814bf13b31eb4823e0bbc0bfa
[scons.git] / doc / user / libraries.sgml
1 <!--
2
3   __COPYRIGHT__
4
5   Permission is hereby granted, free of charge, to any person obtaining
6   a copy of this software and associated documentation files (the
7   "Software"), to deal in the Software without restriction, including
8   without limitation the rights to use, copy, modify, merge, publish,
9   distribute, sublicense, and/or sell copies of the Software, and to
10   permit persons to whom the Software is furnished to do so, subject to
11   the following conditions:
12
13   The above copyright notice and this permission notice shall be included
14   in all copies or substantial portions of the Software.
15
16   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17   KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18   WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 -->
25
26   <para>
27
28   It's often useful to organize large software projects
29   by collecting parts of the software into one or more libraries.
30   &SCons; makes it easy to create libraries
31   and to use them in the programs.
32
33   </para>
34
35   <section>
36   <title>Building Libraries</title>
37
38     <para>
39
40     You build your own libraries by specifying &b-link-Library;
41     instead of &b-link-Program;:
42
43     </para>
44
45     <programlisting>
46       Library('foo', ['f1.c', 'f2.c', 'f3.c'])
47     </programlisting>
48
49     <para>
50
51     &SCons; uses the appropriate library prefix and suffix for your system.
52     So on POSIX or Linux systems,
53     the above example would build as follows
54     (although &ranlib; may not be called on all systems):
55
56     </para>
57
58     <screen>
59       % <userinput>scons -Q</userinput>
60       cc -o f1.o -c f1.c
61       cc -o f2.o -c f2.c
62       cc -o f3.o -c f3.c
63       ar rc libfoo.a f1.o f2.o f3.o
64       ranlib libfoo.a
65     </screen>
66
67     <para>
68
69     On a Windows system,
70     a build of the above example would look like:
71
72     </para>
73
74     <screen>
75       C:\><userinput>scons -Q</userinput>
76       cl /nologo /c f1.c /Fof1.obj
77       cl /nologo /c f2.c /Fof2.obj
78       cl /nologo /c f3.c /Fof3.obj
79       lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
80     </screen>
81
82     <para>
83
84     The rules for the target name of the library
85     are similar to those for programs:
86     if you don't explicitly specify a target library name,
87     &SCons; will deduce one from the
88     name of the first source file specified,
89     and &SCons; will add an appropriate
90     file prefix and suffix if you leave them off.
91
92     </para>
93
94     <section>
95     <title>Building Libraries From Source Code or Object Files</title>
96
97       <para>
98
99       The previous example shows building a library from a
100       list of source files.
101       You can, however, also give the &b-link-Library; call
102       object files,
103       and it will correctly realize
104       In fact, you can arbitrarily mix source code files
105       and object files in the source list:
106
107       </para>
108
109       <para>
110
111       <programlisting>
112         Library('foo', ['f1.c', 'f2.o', 'f3.c', 'f4.o'])
113       </programlisting>
114
115       <para>
116
117       And SCons realizes that only the source code files
118       must be compiled into object files
119       before creating the final library:
120
121       </para>
122
123       <screen>
124         % <userinput>scons -Q</userinput>
125         cc -o f1.o -c f1.c
126         cc -o f3.o -c f3.c
127         ar rc libfoo.a f1.o f2.o f3.o f4.o
128         ranlib libfoo.a
129       </screen>
130
131       <para>
132
133       Of course, in this example, the object files
134       must already exist for the build to succeed.
135       See <xref linkend="chap-nodes">, below,
136       for information about how you can
137       build object files explicitly
138       and include the built files in a library.
139
140       </para>
141
142     </section>
143
144     <section>
145     <title>Building Static Libraries Explicitly:  the &b-StaticLibrary; Builder</title>
146
147       <para>
148
149       The &b-link-Library; function builds a traditional static library.
150       If you want to be explicit about the type of library being built,
151       you can use the synonym &b-link-StaticLibrary; function
152       instead of &b-Library:
153
154       </para>
155
156       <programlisting>
157         StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
158       </programlisting>
159
160       <para>
161
162       There is no functional difference between the
163       &b-link-StaticLibrary; and &b-Library; functions.
164
165       </para>
166
167     </section>
168
169     <section>
170     <title>Building Shared (DLL) Libraries:  the &b-SharedLibrary; Builder</title>
171
172       <para>
173
174       If you want to build a shared library (on POSIX systems)
175       or a DLL file (on Windows systems),
176       you use the &b-link-SharedLibrary; function:
177
178       </para>
179
180       <programlisting>
181         SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
182       </programlisting>
183
184       <para>
185
186       The output on POSIX:
187
188       </para>
189
190       <screen>
191         % <userinput>scons -Q</userinput>
192         cc -o f1.os -c f1.c
193         cc -o f2.os -c f2.c
194         cc -o f3.os -c f3.c
195         cc -o libfoo.so -shared f1.os f2.os f3.os
196       </screen>
197
198       <para>
199
200       And the output on Windows:
201
202       </para>
203
204       <screen>
205         C:\><userinput>scons -Q</userinput>
206         cl /nologo /c f1.c /Fof1.obj
207         cl /nologo /c f2.c /Fof2.obj
208         cl /nologo /c f3.c /Fof3.obj
209         link /nologo /dll /out:foo.dll /implib:foo.lib f1.obj f2.obj f3.obj
210         RegServerFunc(target, source, env)
211       </screen>
212
213       <para>
214
215       Notice again that &SCons; takes care of
216       building the output file correctly,
217       adding the <literal>-shared</literal> option
218       for a POSIX compilation,
219       and the <literal>/dll</literal> option on Windows.
220
221       </para>
222
223     </section>
224
225   </section>
226
227   <section>
228   <title>Linking with Libraries</title>
229
230     <para>
231
232     Usually, you build a library
233     because you want to link it with one or more programs.
234     You link libraries with a program by specifying
235     the libraries in the &cv-link-LIBS; construction variable,
236     and by specifying the directory in which
237     the library will be found in the 
238     &cv-link-LIBPATH; construction variable:
239
240     </para>
241
242     <programlisting>
243       Library('foo', ['f1.c', 'f2.c', 'f3.c'])
244       Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')
245     </programlisting>
246
247     <para>
248
249     Notice, of course, that you don't need to specify a library
250     prefix (like <literal>lib</literal>)
251     or suffix (like <literal>.a</literal> or <literal>.lib</literal>).
252     &SCons; uses the correct prefix or suffix for the current system.
253
254     </para>
255
256     <para>
257
258     On a POSIX or Linux system,
259     a build of the above example would look like:
260
261     </para>
262
263     <screen>
264       % <userinput>scons -Q</userinput>
265       cc -o f1.o -c f1.c
266       cc -o f2.o -c f2.c
267       cc -o f3.o -c f3.c
268       ar rc libfoo.a f1.o f2.o f3.o
269       ranlib libfoo.a
270       cc -o prog.o -c prog.c
271       cc -o prog prog.o -L. -lfoo -lbar
272     </screen>
273
274     <para>
275
276     On a Windows system,
277     a build of the above example would look like:
278
279     </para>
280
281     <screen>
282       C:\><userinput>scons -Q</userinput>
283       cl /nologo /c f1.c /Fof1.obj
284       cl /nologo /c f2.c /Fof2.obj
285       cl /nologo /c f3.c /Fof3.obj
286       lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
287       cl /nologo /c prog.c /Foprog.obj
288       link /nologo /OUT:prog.exe /LIBPATH:. foo.lib bar.lib prog.obj
289     </screen>
290
291     <para>
292
293     As usual, notice that &SCons; has taken care
294     of constructing the correct command lines
295     to link with the specified library on each system.
296
297     </para>
298
299     <para>
300
301     Note also that,
302     if you only have a single library to link with,
303     you can specify the library name in single string,
304     instead of a Python list,
305     so that:
306
307     </para>
308
309     <programlisting>
310       Program('prog.c', LIBS='foo', LIBPATH='.')
311     </programlisting>
312
313     <para>
314
315     is equivalent to:
316
317     </para>
318
319     <programlisting>
320       Program('prog.c', LIBS=['foo'], LIBPATH='.')
321     </programlisting>
322
323     <para>
324
325     This is similar to the way that &SCons;
326     handles either a string or a list to
327     specify a single source file.
328
329     </para>
330
331   </section>
332
333   <section>
334   <title>Finding Libraries:  the &cv-LIBPATH; Construction Variable</title>
335
336     <para>
337
338     By default, the linker will only look in
339     certain system-defined directories for libraries.
340     &SCons; knows how to look for libraries
341     in directories that you specify with the
342     &cv-link-LIBPATH; construction variable.
343     &cv-LIBPATH; consists of a list of
344     directory names, like so:
345
346     </para>
347
348     <programlisting>
349       Program('prog.c', LIBS = 'm',
350                         LIBPATH = ['/usr/lib', '/usr/local/lib'])
351     </programlisting>
352
353     <para>
354
355     Using a Python list is preferred because it's portable
356     across systems.  Alternatively, you could put all of
357     the directory names in a single string, separated by the
358     system-specific path separator character:
359     a colon on POSIX systems:
360
361     </para>
362
363     <programlisting>
364       LIBPATH = '/usr/lib:/usr/local/lib'
365     </programlisting>
366
367     <para>
368
369     or a semi-colon on Windows systems:
370
371     </para>
372
373     <programlisting>
374       LIBPATH = 'C:\\lib;D:\\lib'
375     </programlisting>
376
377     <para>
378
379     (Note that Python requires that the backslash
380     separators in a Windows path name
381     be escaped within strings.)
382
383     </para>
384
385     <para>
386
387     When the linker is executed,
388     &SCons; will create appropriate flags
389     so that the linker will look for
390     libraries in the same directories as &SCons;.
391     So on a POSIX or Linux system,
392     a build of the above example would look like:
393
394     </para>
395
396     <screen>
397       % <userinput>scons -Q</userinput>
398       cc -o prog.o -c prog.c
399       cc -o prog prog.o -L/usr/lib -L/usr/local/lib -lm
400     </screen>
401
402     <para>
403
404     On a Windows system,
405     a build of the above example would look like:
406
407     </para>
408
409     <screen>
410       C:\><userinput>scons -Q</userinput>
411       cl /nologo /c prog.c /Foprog.obj
412       link /nologo /OUT:prog.exe /LIBPATH:\usr\lib /LIBPATH:\usr\local\lib m.lib prog.obj
413     </screen>
414
415     <para>
416
417     Note again that &SCons; has taken care of
418     the system-specific details of creating
419     the right command-line options.
420
421     </para>
422
423   </section>