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 Static Libraries Explicitly: the &b-StaticLibrary; Builder</title>
101 The &b-link-Library; function builds a traditional static library.
102 If you want to be explicit about the type of library being built,
103 you can use the synonym &b-link-StaticLibrary; function
104 instead of &b-Library:
108 <scons_example name="StaticLibrary" printme="1">
109 <file name="SConstruct" printme="1">
110 StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
116 There is no functional difference between the
117 &b-link-StaticLibrary; and &b-Library; functions.
124 <title>Building Shared (DLL) Libraries: the &b-SharedLibrary; Builder</title>
128 If you want to build a shared library (on POSIX systems)
129 or a DLL file (on Windows systems),
130 you use the &b-link-SharedLibrary; function:
134 <scons_example name="SharedLibrary" printme="1">
135 <file name="SConstruct" printme="1">
136 SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
139 void f1() { printf("f1.c\n"); }
142 void f2() { printf("f2.c\n"); }
145 void f3() { printf("f3.c\n"); }
155 <scons_output example="SharedLibrary" os="posix">
156 <scons_output_command>scons -Q</scons_output_command>
161 And the output on Windows:
165 <scons_output example="SharedLibrary" os="win32">
166 <scons_output_command>scons -Q</scons_output_command>
171 Notice again that &SCons; takes care of
172 building the output file correctly,
173 adding the <literal>-shared</literal> option
174 for a POSIX compilation,
175 and the <literal>/dll</literal> option on Windows.
184 <title>Linking with Libraries</title>
188 Usually, you build a library
189 because you want to link it with one or more programs.
190 You link libraries with a program by specifying
191 the libraries in the &cv-link-LIBS; construction variable,
192 and by specifying the directory in which
193 the library will be found in the
194 &cv-link-LIBPATH; construction variable:
198 <scons_example name="ex2">
199 <file name="SConstruct" printme="1">
200 Library('foo', ['f1.c', 'f2.c', 'f3.c'])
201 Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')
204 int main() { printf("Hello, world!\n"); }
207 int main() { printf("Hello, world!\n"); }
210 int main() { printf("Hello, world!\n"); }
213 int main() { printf("Hello, world!\n"); }
219 Notice, of course, that you don't need to specify a library
220 prefix (like <literal>lib</literal>)
221 or suffix (like <literal>.a</literal> or <literal>.lib</literal>).
222 &SCons; uses the correct prefix or suffix for the current system.
228 On a POSIX or Linux system,
229 a build of the above example would look like:
233 <scons_output example="ex2" os="posix">
234 <scons_output_command>scons -Q</scons_output_command>
240 a build of the above example would look like:
244 <scons_output example="ex2" os="win32">
245 <scons_output_command>scons -Q</scons_output_command>
250 As usual, notice that &SCons; has taken care
251 of constructing the correct command lines
252 to link with the specified library on each system.
259 if you only have a single library to link with,
260 you can specify the library name in single string,
261 instead of a Python list,
267 Program('prog.c', LIBS='foo', LIBPATH='.')
277 Program('prog.c', LIBS=['foo'], LIBPATH='.')
282 This is similar to the way that &SCons;
283 handles either a string or a list to
284 specify a single source file.
291 <title>Finding Libraries: the &cv-LIBPATH; Construction Variable</title>
295 By default, the linker will only look in
296 certain system-defined directories for libraries.
297 &SCons; knows how to look for libraries
298 in directories that you specify with the
299 &cv-link-LIBPATH; construction variable.
300 &cv-LIBPATH; consists of a list of
301 directory names, like so:
305 <scons_example name="ex3">
306 <file name="SConstruct" printme="1">
307 Program('prog.c', LIBS = 'm',
308 LIBPATH = ['/usr/lib', '/usr/local/lib'])
311 int main() { printf("prog.c\n"); }
317 Using a Python list is preferred because it's portable
318 across systems. Alternatively, you could put all of
319 the directory names in a single string, separated by the
320 system-specific path separator character:
321 a colon on POSIX systems:
326 LIBPATH = '/usr/lib:/usr/local/lib'
331 or a semi-colon on Windows systems:
336 LIBPATH = 'C:\\lib;D:\\lib'
341 (Note that Python requires that the backslash
342 separators in a Windows path name
343 be escaped within strings.)
349 When the linker is executed,
350 &SCons; will create appropriate flags
351 so that the linker will look for
352 libraries in the same directories as &SCons;.
353 So on a POSIX or Linux system,
354 a build of the above example would look like:
358 <scons_output example="ex3" os="posix">
359 <scons_output_command>scons -Q</scons_output_command>
365 a build of the above example would look like:
369 <scons_output example="ex3" os="win32">
370 <scons_output_command>scons -Q</scons_output_command>
375 Note again that &SCons; has taken care of
376 the system-specific details of creating
377 the right command-line options.