Linux under C programming: about Static link libraries

Source: Internet
Author: User
Tags copy final printf linux

In the C language level, the reuse of code is usually done through libraries (library). Libraries in the traditional sense refer to files that end with suffix. A. Strictly speaking, the function library should be divided into two kinds: static link library and dynamic link library, also called Dynamic Shared library. A static link library usually refers to a file with a suffix of. A, whereas a dynamic-link library often has a. so suffix name.

A static link library is a file in which one or more of the target files (that is, compiled. o files) are archived. Then, when you need to use a feature in this static library, link the static library with the application that you want to build.

Speaking of AR tools ~ ~ ~

The most common archiving tool on Linux platforms is GNU tar, but instead of using tar to build a static library, use another tool, AR. Both tar and AR are archiving tools, but their purpose is different. Tar is simply used to create an archive file (that is, usually. tar as the suffix of the file), AR also completes the above work, but has done some extra processing, it will be indexed in the archived target files, when the link with the application, the established indexes will recycle the link process.

AR comparisons often use three command options: R (Insert), C (create), and S (indexed), and these three options are often used together. Parameter r: Inserts a module (replace) in the library. Replaces a module with the same name when the inserted module name already exists in the library. If a module in several modules does not exist in the library, AR displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, and other options can be used to change the added position. Parameter c: Create a library. is created regardless of whether the library exists. Parameter s: Creates a target file index, which speeds up time when creating a larger library. (Supplemental: If you do not need to create an index, you can change it to uppercase S parameters; A file is missing an index and can be added using the ranlib command

Now suppose there are two C files, foo.cbar.c. First compile the foo.c and bar.c into the target file foo.o and BAR.O, and then archive the two target files as a static link library.

Bar.c     
         
#include "foobar.h"     
         
char * bar (void)     
         
{     
         
printf ("This is bar! Library1 iscalled\n ");     
         
Return ("Bar");     
         
}
FOO.C     
         
#include "foobar.h"     
         
char * foo (void)     
         
{     
         
printf ("This is Foo!library2 iscalled!\n");     
         
Return ("foo");     
         
}
Foobar.h     
         
#ifndef _foobar_h_     
         
#define _foobar_h_     
         
          
         
#include <stdlib.h>     
         
#include <string  .h>     
         
#include <stdio.h>     
         
extern char *foo (void);     
         
extern char *bar (void);     
         
#endif

Execute Order: ~ ~ ~ ~ ~ ~

#gcc-C foo.c-o foo.o     
         
#gcc-C bar.c-o bar.o     
         
#ar RCS libfoobar.a foo.o BAR.O

This is based on the PC platform, if the embedded platform for the construction of static link library, the process is exactly the same, the only thing that needs to be changed is the tool name used. For example, if you build a static library for Arm-linux, you might want to use Arm-linux-ar. There is also a tool that is NM, which can be used to obtain symbol information for the target file. Here, NM prints out the two symbols in the LIBFOOBAR.A: Foo and bar. Both symbols represent functions, so they have a symbolic value of 0 and a symbol type T (text, which means that the symbol is in a code snippet). The last column gives the name of the symbol.

#nm libfoobar.a     
         
foo.o:     
         
0000000000000000 t foo     
         
u puts     
         
bar.o:     
         
0000000000000000 T bar     
         
u puts

Now the static library is there, how to use such a static library. The application must be linked to a static library if it is to use a static library. This assumes that there is a main.c C file. The link between the application and the static library is completed at compile time.

#gcc-G-O foobar main.c-l.–lfoobar     
         
, or directly: Gcc–o foobar main.c libfoobar.a zfz@zfz:~/program$     
         
./foobar     
         
T The ' is ' Foo!library2 is 
         
foo () =foo-is-     
         
library1 is called     
         
Bar () =bar

Sum it up ~ ~ ~ ~ ~

A static-link library is a "replicated" link process. What is a "copy" link process, when a static link library is linked to an application, the linker copies the static link library to the resulting executable code. For example: There are now two applications A and B, both of which use the functionality provided by LIBFOOBAR.A. So, when you compile link a, the linker copies a copy of LIBFOOBAR.A to a final executable code, and the debug information in LIBFOOBAR.A is replicated, as well, in link B, the linker copies a copy of LIBFOOBAR.A to B's final executable code. This is the meaning of the "copy" link.

To view the dynamic link libraries used by the Foobar program:

$ ldd foobar     
linux-gate.so.1 => (0xffffe000) libc.so.6 =>/lib/libc.so.6     
(0xb7e29000)     
/lib/ Ld-linux.so.2 (0xb7f6e000)

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.