Chapter 4. Building and linking with libraries
=====
In large-scale software, it is very convenient to use libraries for management, and it can save a lot of trouble for code reuse.
4.1 building libraries
=====
Like the program function, you only need to call the library function to generate the library.
Library ('foo', ['f1. C', 'f2. C', 'f3. c'])
% Scons-Q
CC-O f1.o-C f1.c
CC-O f2.o-C f2.c
CC-O f3.o-C f3.c
Ar RC libfoo. A f1.o f2.o f3.o
Ranlib libfoo.
In addition to the source code generation library, you can also generate a library from the. o file.
Library ('foo', ['f1. C', 'f2. o', 'f3. C', 'f4. o'])
% Scons-Q
CC-O f1.o-C f1.c
CC-O f3.o-C f3.c
Ar RC libfoo. A f1.o f2.o f3.o f4.o
Ranlib libfoo.
The static library is generated by default, and the specified static library can also be displayed.
Staticlibrary ('foo', ['f1. C', 'f2. C', 'f3. c'])
You can also specify to generate a dynamic library.
Sharedlibrary ('foo', ['f1. C', 'f2. C', 'f3. c'])
% Scons-Q
CC-O f1. OS-C f1.c
CC-O f2. OS-C f2.c
CC-O f3. OS-C f3.c
CC-O libfoo. So-shared f1. OS f2. OS f3. OS
4.2 linking with libraries
=====
When using program compilation, use $ libs to specify the library name and $ libpath to specify the library path.
Library ('foo', ['f1. C', 'f2. C', 'f3. c'])
Program ('prog. C', Libs = ['foo', 'bar'], libpath = '.')
We do not need to display the specified library prefix and suffix. These scons will be automatically completed for us.
% Scons-Q
CC-O f1.o-C f1.c
CC-O f2.o-C f2.c
CC-O f3.o-C f3.c
Ar RC libfoo. A f1.o f2.o f3.o
Ranlib libfoo.
CC-O Prog. O-C Prog. c
CC-O prog Prog. O-L.-lfoo-lbar
We can see that-L. and scons automatically generate flags.