73ba6c92c758060b284560c77783f32846604058
[scons.git] / doc / user / libraries.in
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     <scons_example name="ex1" printme="1">
46       <file name="SConstruct" printme="1">
47       Library('foo', ['f1.c', 'f2.c', 'f3.c'])
48       </file>
49       <file name="f1.c">
50       void f1() { printf("f1.c\n"); }
51       </file>
52       <file name="f2.c">
53       void f2() { printf("f2.c\n"); }
54       </file>
55       <file name="f3.c">
56       void f3() { printf("f3.c\n"); }
57       </file>
58     </scons_example>
59
60     <para>
61
62     &SCons; uses the appropriate library prefix and suffix for your system.
63     So on POSIX or Linux systems,
64     the above example would build as follows
65     (although &ranlib may not be called on all systems):
66
67     </para>
68
69     <scons_output example="ex1" os="posix">
70       <scons_output_command>scons -Q</scons_output_command>
71     </scons_output>
72
73     <para>
74
75     On a Windows system,
76     a build of the above example would look like:
77
78     </para>
79
80     <scons_output example="ex1" os="win32">
81       <scons_output_command>scons -Q</scons_output_command>
82     </scons_output>
83
84     <para>
85
86     The rules for the target name of the library
87     are similar to those for programs:
88     if you don't explicitly specify a target library name,
89     &SCons; will deduce one from the
90     name of the first source file specified,
91     and &SCons; will add an appropriate
92     file prefix and suffix if you leave them off.
93
94     </para>
95
96     <section>
97     <title>Building Libraries From Source Code or Object Files</title>
98
99       <para>
100
101       The previous example shows building a library from a
102       list of source files.
103       You can, however, also give the &b-link-Library; call
104       object files,
105       and it will correctly realize
106       In fact, you can arbitrarily mix source code files
107       and object files in the source list:
108
109       </para>
110
111       <scons_example name="objects" printme="1">
112         <file name="SConstruct" printme="1">
113         Library('foo', ['f1.c', 'f2.o', 'f3.c', 'f4.o'])
114         </file>
115         <file name="f1.c">
116         void f1() { printf("f1.c\n"); }
117         </file>
118         <file name="f2.o">
119         object file
120         </file>
121         <file name="f3.c">
122         void f3() { printf("f3.c\n"); }
123         </file>
124         <file name="f4.o">
125         object file
126         </file>
127       </scons_example>
128
129       <para>
130
131       And SCons realizes that only the source code files
132       must be compiled into object files
133       before creating the final library:
134
135       </para>
136
137       <scons_output example="objects" os="posix">
138         <scons_output_command>scons -Q</scons_output_command>
139       </scons_output>
140
141       <para>
142
143       Of course, in this example, the object files
144       must already exist for the build to succeed.
145       See <xref linkend="chap-nodes"></xref>, below,
146       for information about how you can
147       build object files explicitly
148       and include the built files in a library.
149
150       </para>
151
152     </section>
153
154     <section>
155     <title>Building Static Libraries Explicitly:  the &b-StaticLibrary; Builder</title>
156
157       <para>
158
159       The &b-link-Library; function builds a traditional static library.
160       If you want to be explicit about the type of library being built,
161       you can use the synonym &b-link-StaticLibrary; function
162       instead of &b-Library;:
163
164       </para>
165
166       <scons_example name="StaticLibrary" printme="1">
167         <file name="SConstruct" printme="1">
168         StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
169         </file>
170       </scons_example>
171
172       <para>
173
174       There is no functional difference between the
175       &b-link-StaticLibrary; and &b-Library; functions.
176
177       </para>
178
179     </section>
180
181     <section>
182     <title>Building Shared (DLL) Libraries:  the &b-SharedLibrary; Builder</title>
183
184       <para>
185
186       If you want to build a shared library (on POSIX systems)
187       or a DLL file (on Windows systems),
188       you use the &b-link-SharedLibrary; function:
189
190       </para>
191
192       <scons_example name="SharedLibrary" printme="1">
193         <file name="SConstruct" printme="1">
194         SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
195         </file>
196         <file name="f1.c">
197         void f1() { printf("f1.c\n"); }
198         </file>
199         <file name="f2.c">
200         void f2() { printf("f2.c\n"); }
201         </file>
202         <file name="f3.c">
203         void f3() { printf("f3.c\n"); }
204         </file>
205       </scons_example>
206
207       <para>
208
209       The output on POSIX:
210
211       </para>
212
213       <scons_output example="SharedLibrary" os="posix">
214         <scons_output_command>scons -Q</scons_output_command>
215       </scons_output>
216
217       <para>
218
219       And the output on Windows:
220
221       </para>
222
223       <scons_output example="SharedLibrary" os="win32">
224         <scons_output_command>scons -Q</scons_output_command>
225       </scons_output>
226
227       <para>
228
229       Notice again that &SCons; takes care of
230       building the output file correctly,
231       adding the <literal>-shared</literal> option
232       for a POSIX compilation,
233       and the <literal>/dll</literal> option on Windows.
234
235       </para>
236
237     </section>
238
239   </section>
240
241   <section>
242   <title>Linking with Libraries</title>
243
244     <para>
245
246     Usually, you build a library
247     because you want to link it with one or more programs.
248     You link libraries with a program by specifying
249     the libraries in the &cv-link-LIBS; construction variable,
250     and by specifying the directory in which
251     the library will be found in the 
252     &cv-link-LIBPATH; construction variable:
253
254     <!-- In the preceding paragraph, the "$" notation for
255          LIBS, LIBPATH etc. is used for the first time.
256          Maybe some words of explanation would be nice. -->
257
258     </para>
259
260     <scons_example name="ex2">
261       <file name="SConstruct" printme="1">
262       Library('foo', ['f1.c', 'f2.c', 'f3.c'])
263       Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')
264       </file>
265       <file name="f1.c">
266       int main() { printf("Hello, world!\n"); }
267       </file>
268       <file name="f2.c">
269       int main() { printf("Hello, world!\n"); }
270       </file>
271       <file name="f3.c">
272       int main() { printf("Hello, world!\n"); }
273       </file>
274       <file name="prog.c">
275       int main() { printf("Hello, world!\n"); }
276       </file>
277     </scons_example>
278
279     <para>
280
281     Notice, of course, that you don't need to specify a library
282     prefix (like <literal>lib</literal>)
283     or suffix (like <literal>.a</literal> or <literal>.lib</literal>).
284     &SCons; uses the correct prefix or suffix for the current system.
285
286     </para>
287
288     <para>
289
290     On a POSIX or Linux system,
291     a build of the above example would look like:
292
293     </para>
294
295     <scons_output example="ex2" os="posix">
296       <scons_output_command>scons -Q</scons_output_command>
297     </scons_output>
298
299     <para>
300
301     On a Windows system,
302     a build of the above example would look like:
303
304     </para>
305
306     <scons_output example="ex2" os="win32">
307       <scons_output_command>scons -Q</scons_output_command>
308     </scons_output>
309
310     <para>
311
312     As usual, notice that &SCons; has taken care
313     of constructing the correct command lines
314     to link with the specified library on each system.
315
316     </para>
317
318     <para>
319
320     Note also that,
321     if you only have a single library to link with,
322     you can specify the library name in single string,
323     instead of a Python list,
324     so that:
325
326     </para>
327
328     <sconstruct>
329       Program('prog.c', LIBS='foo', LIBPATH='.')
330     </sconstruct>
331
332     <para>
333
334     is equivalent to:
335
336     </para>
337
338     <sconstruct>
339       Program('prog.c', LIBS=['foo'], LIBPATH='.')
340     </sconstruct>
341
342     <para>
343
344     This is similar to the way that &SCons;
345     handles either a string or a list to
346     specify a single source file.
347
348     </para>
349
350   </section>
351
352   <section>
353   <title>Finding Libraries:  the &cv-LIBPATH; Construction Variable</title>
354
355     <para>
356
357     By default, the linker will only look in
358     certain system-defined directories for libraries.
359     &SCons; knows how to look for libraries
360     in directories that you specify with the
361     &cv-link-LIBPATH; construction variable.
362     &cv-LIBPATH; consists of a list of
363     directory names, like so:
364
365     </para>
366
367     <scons_example name="ex3">
368       <file name="SConstruct" printme="1">
369       Program('prog.c', LIBS = 'm',
370                         LIBPATH = ['/usr/lib', '/usr/local/lib'])
371       </file>
372       <file name="prog.c">
373       int main() { printf("prog.c\n"); }
374       </file>
375     </scons_example>
376
377     <para>
378
379     Using a Python list is preferred because it's portable
380     across systems.  Alternatively, you could put all of
381     the directory names in a single string, separated by the
382     system-specific path separator character:
383     a colon on POSIX systems:
384
385     </para>
386
387     <sconstruct>
388       LIBPATH = '/usr/lib:/usr/local/lib'
389     </sconstruct>
390
391     <para>
392
393     or a semi-colon on Windows systems:
394
395     </para>
396
397     <sconstruct>
398       LIBPATH = 'C:\\lib;D:\\lib'
399     </sconstruct>
400
401     <para>
402
403     (Note that Python requires that the backslash
404     separators in a Windows path name
405     be escaped within strings.)
406
407     </para>
408
409     <para>
410
411     When the linker is executed,
412     &SCons; will create appropriate flags
413     so that the linker will look for
414     libraries in the same directories as &SCons;.
415     So on a POSIX or Linux system,
416     a build of the above example would look like:
417
418     </para>
419
420     <scons_output example="ex3" os="posix">
421       <scons_output_command>scons -Q</scons_output_command>
422     </scons_output>
423
424     <para>
425
426     On a Windows system,
427     a build of the above example would look like:
428
429     </para>
430
431     <scons_output example="ex3" os="win32">
432       <scons_output_command>scons -Q</scons_output_command>
433     </scons_output>
434     <!-- The link command is too wide in the PDF version. 
435          There are some other examples of this throughout the document. -->
436
437     <para>
438
439     Note again that &SCons; has taken care of
440     the system-specific details of creating
441     the right command-line options.
442
443     </para>
444
445   </section>