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
|
<!--
Copyright (c) 2001, 2002, 2003 Steven Knight
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>
One of the more useful ways in which you can use multiple
construction environments is to link programs
with different sets of libraries.
</para>
<section>
<title>Building Libraries</title>
<para>
You build your own libraries by specifying &Library;
instead of &Program;:
</para>
<programlisting>
env = Environment()
env.Library('foo', ['f1.c', 'f2.c', 'f3.c'])
</programlisting>
<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>
<literallayout>
% <userinput>scons</userinput>
cc -c f1.c -o f1.o
cc -c f2.c -o f2.o
cc -c f3.c -o f3.o
ar r libfoo.a f1.o f2.o f3.o
ranlib libfoo.a
</literallayout>
<para>
On a Windows system,
a build of the above example would look like:
</para>
<literallayout>
C:\><userinput>scons</userinput>
cl /Fof1.obj f1.c
cl /Fof2.obj f2.c
cl /Fof3.obj f3.c
lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
</literallayout>
<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>
<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 &LIBS; construction variable,
and by specifying the directory in which
the library will be found in the
&LIBPATH; construction variable:
</para>
<programlisting>
env = Environment(LIBS = 'foo', LIBPATH = '.')
env.Library('foo', ['f1.c', 'f2.c', 'f3.c'])
env.Program('prog.c')
</programlisting>
<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>
<literallayout>
% <userinput>scons</userinput>
cc -c f1.c -o f1.o
cc -c f2.c -o f2.o
cc -c f3.c -o f3.o
ar r libfoo.a f1.o f2.o f3.o
ranlib libfoo.a
cc -c prog.c -o prog.o
cc -o prog -L. -lfoo prog.o
</literallayout>
<para>
On a Windows system,
a build of the above example would look like:
</para>
<literallayout>
C:\><userinput>scons</userinput>
cl /Fof1.obj f1.c
cl /Fof2.obj f2.c
cl /Fof3.obj f3.c
lib /nologo /OUT:foo.lib f1.obj f2.obj f3.obj
cl /Foprog.obj prog.c
link /OUT:prog.exe /LIBPATH:. foo.lib prog.obj
</literallayout>
<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>
</section>
<section>
<title>Finding Libraries: the &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
&LIBPATH; construction variable.
&LIBPATH; consists of a list of
directory names, like so:
</para>
<programlisting>
env = Environment(LIBS = 'm',
LIBPATH = ['/usr/lib', '/usr/local/lib'])
env.Program('prog.c')
</programlisting>
<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>
<programlisting>
LIBPATH = '/usr/lib:/usr/local/lib'
</programlisting>
<para>
or a semi-colon on Windows systems:
</para>
<programlisting>
LIBPATH = 'C:\lib;D:\lib'
</programlisting>
<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>
<literallayout>
% <userinput>scons</userinput>
cc -c prog.c -o prog.o
cc -o prog -L/usr/lib -L/usr/local/lib -lm prog.o
</literallayout>
<para>
On a Windows system,
a build of the above example would look like:
</para>
<literallayout>
C:\><userinput>scons</userinput>
cl /Foprog.obj prog.c
link /nologo /OUT:program.exe /LIBPATH:\usr\lib;\usr\local\lib m.lib prog.obj
</literallayout>
<para>
Note again that &SCons; has taken care of
the system-specific details of creating
the right command-line options.
</para>
</section>
|