[Scons translation scons learning] 3. generate and use a library

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.