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;:
45 <scons_example name="ex1" printme="1">
46 <file name="SConstruct" printme="1">
47 Library('foo', ['f1.c', 'f2.c', 'f3.c'])
50 void f1() { printf("f1.c\n"); }
53 void f2() { printf("f2.c\n"); }
56 void f3() { printf("f3.c\n"); }
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):
69 <scons_output example="ex1" os="posix">
70 <scons_output_command>scons -Q</scons_output_command>
76 a build of the above example would look like:
80 <scons_output example="ex1" os="win32">
81 <scons_output_command>scons -Q</scons_output_command>
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.
97 <title>Building Libraries From Source Code or Object Files</title>
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
105 and it will correctly realize
106 In fact, you can arbitrarily mix source code files
107 and object files in the source list:
111 <scons_example name="objects" printme="1">
112 <file name="SConstruct" printme="1">
113 Library('foo', ['f1.c', 'f2.o', 'f3.c', 'f4.o'])
116 void f1() { printf("f1.c\n"); }
122 void f3() { printf("f3.c\n"); }
131 And SCons realizes that only the source code files
132 must be compiled into object files
133 before creating the final library:
137 <scons_output example="objects" os="posix">
138 <scons_output_command>scons -Q</scons_output_command>
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.
155 <title>Building Static Libraries Explicitly: the &b-StaticLibrary; Builder</title>
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;:
166 <scons_example name="StaticLibrary" printme="1">
167 <file name="SConstruct" printme="1">
168 StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
174 There is no functional difference between the
175 &b-link-StaticLibrary; and &b-Library; functions.
182 <title>Building Shared (DLL) Libraries: the &b-SharedLibrary; Builder</title>
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:
192 <scons_example name="SharedLibrary" printme="1">
193 <file name="SConstruct" printme="1">
194 SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
197 void f1() { printf("f1.c\n"); }
200 void f2() { printf("f2.c\n"); }
203 void f3() { printf("f3.c\n"); }
213 <scons_output example="SharedLibrary" os="posix">
214 <scons_output_command>scons -Q</scons_output_command>
219 And the output on Windows:
223 <scons_output example="SharedLibrary" os="win32">
224 <scons_output_command>scons -Q</scons_output_command>
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.
242 <title>Linking with Libraries</title>
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:
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. -->
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='.')
266 int main() { printf("Hello, world!\n"); }
269 int main() { printf("Hello, world!\n"); }
272 int main() { printf("Hello, world!\n"); }
275 int main() { printf("Hello, world!\n"); }
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.
290 On a POSIX or Linux system,
291 a build of the above example would look like:
295 <scons_output example="ex2" os="posix">
296 <scons_output_command>scons -Q</scons_output_command>
302 a build of the above example would look like:
306 <scons_output example="ex2" os="win32">
307 <scons_output_command>scons -Q</scons_output_command>
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.
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,
329 Program('prog.c', LIBS='foo', LIBPATH='.')
339 Program('prog.c', LIBS=['foo'], LIBPATH='.')
344 This is similar to the way that &SCons;
345 handles either a string or a list to
346 specify a single source file.
353 <title>Finding Libraries: the &cv-LIBPATH; Construction Variable</title>
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:
367 <scons_example name="ex3">
368 <file name="SConstruct" printme="1">
369 Program('prog.c', LIBS = 'm',
370 LIBPATH = ['/usr/lib', '/usr/local/lib'])
373 int main() { printf("prog.c\n"); }
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:
388 LIBPATH = '/usr/lib:/usr/local/lib'
393 or a semi-colon on Windows systems:
398 LIBPATH = 'C:\\lib;D:\\lib'
403 (Note that Python requires that the backslash
404 separators in a Windows path name
405 be escaped within strings.)
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:
420 <scons_output example="ex3" os="posix">
421 <scons_output_command>scons -Q</scons_output_command>
427 a build of the above example would look like:
431 <scons_output example="ex3" os="win32">
432 <scons_output_command>scons -Q</scons_output_command>
434 <!-- The link command is too wide in the PDF version.
435 There are some other examples of this throughout the document. -->
439 Note again that &SCons; has taken care of
440 the system-specific details of creating
441 the right command-line options.