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