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>
77 scons: warning: No installed VCs
78 File "<stdin>", line 67, in __call__
80 scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
81 File "<stdin>", line 67, in __call__
82 cl /Fof1.obj /c f1.c /nologo
83 cl /Fof2.obj /c f2.c /nologo
84 cl /Fof3.obj /c f3.c /nologo
85 lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
90 The rules for the target name of the library
91 are similar to those for programs:
92 if you don't explicitly specify a target library name,
93 &SCons; will deduce one from the
94 name of the first source file specified,
95 and &SCons; will add an appropriate
96 file prefix and suffix if you leave them off.
101 <title>Building Libraries From Source Code or Object Files</title>
105 The previous example shows building a library from a
106 list of source files.
107 You can, however, also give the &b-link-Library; call
109 and it will correctly realize
110 In fact, you can arbitrarily mix source code files
111 and object files in the source list:
116 Library('foo', ['f1.c', 'f2.o', 'f3.c', 'f4.o'])
121 And SCons realizes that only the source code files
122 must be compiled into object files
123 before creating the final library:
128 % <userinput>scons -Q</userinput>
131 ar rc libfoo.a f1.o f2.o f3.o f4.o
137 Of course, in this example, the object files
138 must already exist for the build to succeed.
139 See <xref linkend="chap-nodes"></xref>, below,
140 for information about how you can
141 build object files explicitly
142 and include the built files in a library.
149 <title>Building Static Libraries Explicitly: the &b-StaticLibrary; Builder</title>
153 The &b-link-Library; function builds a traditional static library.
154 If you want to be explicit about the type of library being built,
155 you can use the synonym &b-link-StaticLibrary; function
156 instead of &b-Library;:
161 StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
166 There is no functional difference between the
167 &b-link-StaticLibrary; and &b-Library; functions.
174 <title>Building Shared (DLL) Libraries: the &b-SharedLibrary; Builder</title>
178 If you want to build a shared library (on POSIX systems)
179 or a DLL file (on Windows systems),
180 you use the &b-link-SharedLibrary; function:
185 SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
195 % <userinput>scons -Q</userinput>
199 cc -o libfoo.so -shared f1.os f2.os f3.os
204 And the output on Windows:
209 C:\><userinput>scons -Q</userinput>
211 scons: warning: No installed VCs
212 File "<stdin>", line 67, in __call__
214 scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
215 File "<stdin>", line 67, in __call__
216 cl /Fof1.obj /c f1.c /nologo
217 cl /Fof2.obj /c f2.c /nologo
218 cl /Fof3.obj /c f3.c /nologo
219 link /nologo /dll /out:foo.dll /implib:foo.lib f1.obj f2.obj f3.obj
220 RegServerFunc(target, source, env)
225 Notice again that &SCons; takes care of
226 building the output file correctly,
227 adding the <literal>-shared</literal> option
228 for a POSIX compilation,
229 and the <literal>/dll</literal> option on Windows.
238 <title>Linking with Libraries</title>
242 Usually, you build a library
243 because you want to link it with one or more programs.
244 You link libraries with a program by specifying
245 the libraries in the &cv-link-LIBS; construction variable,
246 and by specifying the directory in which
247 the library will be found in the
248 &cv-link-LIBPATH; construction variable:
250 <!-- In the preceding paragraph, the "$" notation for
251 LIBS, LIBPATH etc. is used for the first time.
252 Maybe some words of explanation would be nice. -->
257 Library('foo', ['f1.c', 'f2.c', 'f3.c'])
258 Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')
263 Notice, of course, that you don't need to specify a library
264 prefix (like <literal>lib</literal>)
265 or suffix (like <literal>.a</literal> or <literal>.lib</literal>).
266 &SCons; uses the correct prefix or suffix for the current system.
272 On a POSIX or Linux system,
273 a build of the above example would look like:
278 % <userinput>scons -Q</userinput>
282 ar rc libfoo.a f1.o f2.o f3.o
284 cc -o prog.o -c prog.c
285 cc -o prog prog.o -L. -lfoo -lbar
291 a build of the above example would look like:
296 C:\><userinput>scons -Q</userinput>
298 scons: warning: No installed VCs
299 File "<stdin>", line 67, in __call__
301 scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
302 File "<stdin>", line 67, in __call__
303 cl /Fof1.obj /c f1.c /nologo
304 cl /Fof2.obj /c f2.c /nologo
305 cl /Fof3.obj /c f3.c /nologo
306 lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
307 cl /Foprog.obj /c prog.c /nologo
308 link /nologo /OUT:prog.exe /LIBPATH:. foo.lib bar.lib prog.obj
313 As usual, notice that &SCons; has taken care
314 of constructing the correct command lines
315 to link with the specified library on each system.
322 if you only have a single library to link with,
323 you can specify the library name in single string,
324 instead of a Python list,
330 Program('prog.c', LIBS='foo', LIBPATH='.')
340 Program('prog.c', LIBS=['foo'], LIBPATH='.')
345 This is similar to the way that &SCons;
346 handles either a string or a list to
347 specify a single source file.
354 <title>Finding Libraries: the &cv-LIBPATH; Construction Variable</title>
358 By default, the linker will only look in
359 certain system-defined directories for libraries.
360 &SCons; knows how to look for libraries
361 in directories that you specify with the
362 &cv-link-LIBPATH; construction variable.
363 &cv-LIBPATH; consists of a list of
364 directory names, like so:
369 Program('prog.c', LIBS = 'm',
370 LIBPATH = ['/usr/lib', '/usr/local/lib'])
375 Using a Python list is preferred because it's portable
376 across systems. Alternatively, you could put all of
377 the directory names in a single string, separated by the
378 system-specific path separator character:
379 a colon on POSIX systems:
384 LIBPATH = '/usr/lib:/usr/local/lib'
389 or a semi-colon on Windows systems:
394 LIBPATH = 'C:\\lib;D:\\lib'
399 (Note that Python requires that the backslash
400 separators in a Windows path name
401 be escaped within strings.)
407 When the linker is executed,
408 &SCons; will create appropriate flags
409 so that the linker will look for
410 libraries in the same directories as &SCons;.
411 So on a POSIX or Linux system,
412 a build of the above example would look like:
417 % <userinput>scons -Q</userinput>
418 cc -o prog.o -c prog.c
419 cc -o prog prog.o -L/usr/lib -L/usr/local/lib -lm
425 a build of the above example would look like:
430 C:\><userinput>scons -Q</userinput>
432 scons: warning: No installed VCs
433 File "<stdin>", line 67, in __call__
435 scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
436 File "<stdin>", line 67, in __call__
437 cl /Foprog.obj /c prog.c /nologo
438 link /nologo /OUT:prog.exe /LIBPATH:\usr\lib /LIBPATH:\usr\local\lib m.lib prog.obj
440 <!-- The link command is too wide in the PDF version.
441 There are some other examples of this throughout the document. -->
445 Note again that &SCons; has taken care of
446 the system-specific details of creating
447 the right command-line options.