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:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
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.
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.
36 <title>Building Libraries</title>
40 You build your own libraries by specifying &b-link-Library;
41 instead of &b-link-Program;:
46 Library('foo', ['f1.c', 'f2.c', 'f3.c'])
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):
59 % <userinput>scons -Q</userinput>
63 ar rc libfoo.a f1.o f2.o f3.o
70 a build of the above example would look like:
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
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.
95 <title>Building Libraries From Source Code or Object Files</title>
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
103 and it will correctly realize
104 In fact, you can arbitrarily mix source code files
105 and object files in the source list:
112 Library('foo', ['f1.c', 'f2.o', 'f3.c', 'f4.o'])
117 And SCons realizes that only the source code files
118 must be compiled into object files
119 before creating the final library:
124 % <userinput>scons -Q</userinput>
127 ar rc libfoo.a f1.o f2.o f3.o f4.o
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.
145 <title>Building Static Libraries Explicitly: the &b-StaticLibrary; Builder</title>
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:
157 StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
162 There is no functional difference between the
163 &b-link-StaticLibrary; and &b-Library; functions.
170 <title>Building Shared (DLL) Libraries: the &b-SharedLibrary; Builder</title>
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:
181 SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
191 % <userinput>scons -Q</userinput>
195 cc -o libfoo.so -shared f1.os f2.os f3.os
200 And the output on Windows:
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)
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.
228 <title>Linking with Libraries</title>
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:
243 Library('foo', ['f1.c', 'f2.c', 'f3.c'])
244 Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')
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.
258 On a POSIX or Linux system,
259 a build of the above example would look like:
264 % <userinput>scons -Q</userinput>
268 ar rc libfoo.a f1.o f2.o f3.o
270 cc -o prog.o -c prog.c
271 cc -o prog prog.o -L. -lfoo -lbar
277 a build of the above example would look like:
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
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.
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,
310 Program('prog.c', LIBS='foo', LIBPATH='.')
320 Program('prog.c', LIBS=['foo'], LIBPATH='.')
325 This is similar to the way that &SCons;
326 handles either a string or a list to
327 specify a single source file.
334 <title>Finding Libraries: the &cv-LIBPATH; Construction Variable</title>
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:
349 Program('prog.c', LIBS = 'm',
350 LIBPATH = ['/usr/lib', '/usr/local/lib'])
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:
364 LIBPATH = '/usr/lib:/usr/local/lib'
369 or a semi-colon on Windows systems:
374 LIBPATH = 'C:\\lib;D:\\lib'
379 (Note that Python requires that the backslash
380 separators in a Windows path name
381 be escaped within strings.)
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:
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
405 a build of the above example would look like:
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
417 Note again that &SCons; has taken care of
418 the system-specific details of creating
419 the right command-line options.