Tips: Linux dynamic library and static library production and use of detailed

Source: Internet
Author: User

http://www.ibm.com/developerworks/cn/linux/l-cn-linklib/

Tips: Linux dynamic library and static library production and use of detailed

Three ways of connection and static library making and using in standard library

Linux application development usually has to consider three issues, namely: 1) in the Linux application development process encountered the standard library links in different Linux versions of the problem, 2) in the production of the Linux static library is different from the production of Static library under Windows; 3) in Linu X application links to third-party libraries or other static libraries when discovering the annoying problem of link order. This article is about these three questions about the standard library links under Linux and how to build achrive (*.A).

Two basic knowledge to know

Linux applications because of the numerous and independent Linux version, in the production and use of engineering must be proficient in the following two points in order to effectively work and ideal to run.

    1. Linux Standard library links three ways (full static, semi-static (libgcc,libstdc++), full dynamic) and their respective pros and cons.
    2. How to cleverly build achrive (*.A) under Linux, and how to set link options to resolve the issue of the order of GCC's more specific link libraries.
Three standard library link options and comparisons

To demonstrate the difference between the three different standard library links to the final application, here is a demo of the classic sample application HelloWorld, as shown in Listing 1 HelloWorld. The entire project can be downloaded at the end of the article.

Listing 1. HelloWorld
#include <stdio.h>  #include <iostream>  using Std::cout;  Using Std::endl;  int main (int argc, char* argv[])  {   printf ("helloworld! ( Printed by printf) \ n ");   cout<< "helloworld! (Printed by cout) "<<endl;   return 0;  }

Three types of standard library link options and differences are shown in table 1

Table 1. Options and differences for three standard library link types thead>
Standard library connection method example connection options benefits cons
full static -static-pthread-lrt-ldl does not occur when the application has a standard library incompatibility problem with different Linux versions. generates large files,
application functionality is limited (cannot invoke dynamic libraries, etc.)
full dynamic -pthrea The D-LRT-LDL makefile is the smallest of the three, and it is easier for an application to be incompatible with the standard library dependencies under the
different Linux versions.
semi-static (libgcc,libstdc++) -static-libgcc-l.-PTHREAD-LRT -LDL is flexible enough to take a different link strategy for different standard libraries,
to avoid incompatibility issues. The
combines the advantages of both full-static and full-dynamic linking methods.
is difficult to identify which libraries are prone to incompatibility issues, and
is now only built on experience.
Some features are lost due to the selected standard library version.

Of the above three standard library links, the more special is the semi-static link, mainly because it also need to add an additional step before the link:
Ln-s ' g++-print-file-name=libstdc++.a ', which is the role of linking libstdc++.a (Static library of libstdc++) symbols to the local engineering link directory.
-print-file-name's interpretation in GCC is as follows:
-print-file-name=<lib> Display the full path to library <lib>

To differentiate the effects of three different standard library links on the resulting executables, this article analyzes and compares them from two different dimensions:

Dimension one: How the resulting executable files depend on the standard library (analyzed using the LDD command)

LDD Introduction: This command is used to print out the dynamic libraries that an application or dynamic library relies on
Syntax involved: LDD [OPTION] ... FILE ...
Please refer to the man instructions for additional detailed instructions.

Three standard library linking methods the resulting application's executable file for the standard library depends on the specific differences shown in Figure 1, figure 2, and Figure 3:

Figure 1. Full static standard library link mode

Figure 2. Full dynamic standard library link mode

Figure 3. Semi-static (libgcc,libstdc++) standard library link mode


Through the above three graphs, it is clear that when using the full static standard library link mode , the resulting executable file ultimately does not rely on any dynamic standard library,
the link of the full-dynamic standard library causes the final application executable to rely on all the standard dynamic libraries used.
The LIBGCC and libstdc++ two standard libraries are not dynamically linked by a semi-static link that differs from the above two methods.
(In contrast to Figure 2 and Figure 3, the dynamic dependency of the two standard libraries in Figure 3 is missing.)

From the practical application, it is found that the most ideal way to link the standard library is semi-static link, usually choose to LIBGCC and libstdc++ two standard library static link,
This avoids the problem that the application is incompatible with the standard library dependencies between different Linux versions.

Dimension two: The resulting executable file size (parsed using the size command)

Size Introduction: This command is used to show the size of the executable file
Syntax involved: size objfile ...
Please refer to the man instructions for additional detailed instructions.

Three standard library links the actual size of the resulting application's executable file is shown in Figure 4, Figure 5, and Figure 6:

Figure 4. Full static standard library link mode

Figure 5. Full dynamic standard library link mode

Figure 6. Semi-static (libgcc,libstdc++) standard library link mode


As can be seen from the above three graphs, the size of the final executable file decreases with the increase in the number of standard dynamic libraries that ultimately depend on it.
From the actual application, it is found that the most ideal is the semi-static link mode , because this method can avoid the application in
The standard library-dependent incompatibilities between different versions of Linux occur while minimizing the size of the resulting executable file.

The commands involved in the sample link option (referencing the original GCC text):

-llibrary
-L Library: Specify the required additional libraries
-ldir: Specifying a library search path
-static: Statically link all libraries
-STATIC-LIBGCC: statically linked GCC libraries
-static-libstdc++: Statically linked C + + library
For a detailed description of the above command, please refer to the GCC technical manual

Back to top of page

How to make a static library (archive) under Linux: Related commands: AR

Introduction to AR: Handling operations to create, modify, and extract static libraries

Options involved:
T-Display the contents of a static library
R[AB][F][U]-Update or add new files to the static library
[s]-Create document Index
ar-m [<mri-script]-processing with AR script
Please refer to the man instructions for additional detailed instructions.

Example scenario:

Consider the existing two library files as shown in Figure 7

Figure 7. Sample Static library file

As you can tell from Figure 7, CDTLOG.A contains only cdtlog.o an object file, and Xml.a contains TXMLPARSER.O and xmlparser.o two object files
Now extract the CDTLOG.O, and then create a new static library by using Figure 8 DEMO.A, you can see, DEMO.A contains CDTLOG.O and XML.A,
Rather than the CDTLOG.O,TXMLPARSER.O and xmlparser.o that we expected. This is exactly the same as the creation of a static library under Windows.

Figure 8. Example Static Library Authoring Method 1

Such a demo.a when chained to a project, all the symbols defined in TXMLPARSER.O and XMLPARSER.O are not found, resulting in link errors,
Tip the corresponding symbol cannot be found. Obviously, it is not correct to create a Linux static library by using Figure 8.

There are two ways to do it right:

    1. Extract the object files contained in all the static libraries and repackage them into a new static library file.
    2. Create a new static library file in a more flexible way:ar script .

Obviously, Mode 1 is cumbersome because it involves too much file processing and may also be used to save intermediate files by constantly creating temporary directories.
It is recommended to create the ar script as shown in Listing 2 createlib.sh:

Listing 2 createlib.sh
RM demo.a  RM ar.mac  echo CREATE demo.a > Ar.mac  echo SAVE >> ar.mac  echo END >> ar.mac  Ar-m < Ar.mac  ar-q demo.a cdtlog.o  echo OPEN demo.a > Ar.mac  echo addlib xml.a >> ar.mac  Ech o SAVE >> ar.mac  echo END >> ar.mac  ar-m < Ar.mac  RM Ar.mac

If you want to create a static library using ar scripting in Linux makefile, you can write the code shown in Listing 3 build_library:

Listing 3 Build_library
Define Build_library  $ (if $ (wildcard [email protected]), @$ (RM) [email protected])  $ (if $ (wildcard Ar.mac), @$ ( RM) Ar.mac  $ (if $ (filter%.a, $^),  @echo CREATE [email protected] > Ar.mac  @echo SAVE >> ar.mac  @echo END >> ar.mac  @$ (AR)-m < Ar.mac  )  $ (if $ (filter%.o,$^), @$ (AR)-Q [email protected] $ (filter% . O, $^))  $ (if $ (filter%.a, $^),  @echo OPEN [email protected] > Ar.mac  $ (foreach LIB, $ (filter%.a, $^), c12/> @echo Addlib $ (LIB) >> ar.mac  )  @echo SAVE >> ar.mac  @echo END >> ar.mac  @$ ( AR)-M < Ar.mac  @$ (RM) Ar.mac  )  endef  $ (TargetDir)/$ (TargetFileName): $ (OBJS)     $ (build_ LIBRARY)

In Figure 9, we can see that the DEMO.A produced in this way is the result we want.

Figure 9. Cleverly created static library file results

Back to top of page

Linux Static Library link order problem and solution:

As mentioned in the GCC Handbook:
It makes a difference where in the command you write this option; The linker
Searches and processes libraries and object files in the order they is specified.
Thus, ' Foo.o-lz bar.o ' searches library ' z ' after file ' foo.o ' but before
' BAR.O '. If ' bar.o ' refers to functions in ' Z ', those functions is not being loaded.

To solve this problem of library link order, we need to add some link options:

$ (CXX) $ (linkflags) $ (OBJS)-xlinker "-(" $ (LIBS)-xlinker "-)"-o [email protected]

By placing all static libraries that need to be linked into the-xlinker "-(" and-xlinker "-)", you can automatically loop through all the static libraries during the g++ link process, thus resolving the original link order problem.

Related link Options:-xlinker

-xlinker option
Pass option as an option to the linker. can use this to supply system-specific
Linker options which GCC does not know how to recognize.

Back to top of page

Summary

This paper introduces the ways of three standard library links in Linux and their pros and cons, and also introduces how to make and use the static library under Linux, which is believed to provide useful help for most engineers who need to deploy Linux applications and write Linux Makefile.

Download
Description name size
The HelloWorld code example used in this article Helloworld.zip 2.49KB
Reference to the GCC PDF document Gcc.pdf 2.88MB

Reference Learning
    • For AR, refer to: Linux AR Packaging Library to another library.
    • For static links, refer to: Linking libstdc++ statically.
    • Find out more about our most popular articles and tutorials in the DeveloperWorks Linux zone for more reference materials for Linux developers, including beginners for Linux.
    • Check out all Linux tips and Linux tutorials on the developerWorks.
    • Stay tuned for DeveloperWorks technical activities and webcasts.
Access to products and technologies
    • Download the IBM software trial and experience the powerful db2®,lotus®,rational®,tivoli® and websphere® software.
Discuss
    • Participate in forum discussions.
    • View the latest information on the DeveloperWorks blog.
    • Joining the DeveloperWorks Chinese community, the DeveloperWorks community is a professional social networking community for global IT professionals who can provide community functions such as blogs, bookmarks, wikis, groups, contacts, sharing, and collaboration.
Article comments

Tips: Linux dynamic library and static library production and use of detailed

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.