Linux C Programming learning-static Link Library and linux programming static Link

Source: Internet
Author: User

Linux C Programming learning-static Link Library and linux programming static Link

At the C language level, code reuse is usually implemented through libraries. In the traditional sense, a library refers to a file ending with the suffix.. Strictly speaking, the function library should be divided into two types: static Link Library and dynamic link library, also known as dynamic sharing library. A static Link Library is usually a file suffixed with. a, while a dynamic link library is often suffixed with. so. The static Link Library archives one or more target files (The. o files generated by compilation) in one file. Then, when you need to use a function in the static library, link the static library with the application to be generated.

ArTools

On the Linux platform, the most commonly used archive tool is GNU tar. But to build a static library, tar is not used, but ar is another tool. Tar and ar are both archive tools, but they have different purposes. Tartar is used to create a archive file (that is, a file with a suffix of .tar). ar also completes the above work. However, it performs some additional processing to create an index for the symbols in the archive target file, when linked to an application, these indexes are recycled. Ar usually uses three Command Options: r (insert), c (create), and s (create Index). These three options are often used together. Parameter r: Insert a module (replace) into the database ). If the inserted Module name already exists in the Database, replace the module with the same name. If one of the 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 database. You can use other options to change the position of the added member. Parameter c: Create a database. Whether or not the database exists will be created. Parameter s: Create the index of the target file, which can speed up the creation of a large library. (Supplement: if you do not need to create an index, you can change it to the uppercase S parameter. If. File a lacks an index. You can use the ranlib command to add the index)

Now we assume there are two C files: foo. c, bar. c. Compile foo. c and bar. c as the target file foo. o and bar. o, and archive the two 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  

Run the command:

#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. For the construction of static link libraries on the embedded platform, the process is the same. The only change may be the name of the tool used. For example, if you want to build a static library for ARM-Linux, you may need to use arm-linux-ar. Another tool here is nm, which can be used to obtain the symbol information of the target file. Here, nm prints two symbols in libfoobar. a: foo and bar. These two symbols represent functions, so their symbol values are 0 and the symbol type is T (text, indicating that the symbol is in the code segment ). The last column is the name of the symbol.

# Nm libfoobar.

Foo. o:

0000000000000000 T foo

U puts

Bar. o:

0000000000000000 TB bar

U puts

The current static library is available. How can I use such a static library. To use a static library, the application must be linked to the static library. Suppose there is a c file of main. C. The link between the application and the static library is completed during the compilation period.

# Gcc-g-o foobar main. c-L.-lfoobar

Or directly: gcc-o foobar main. c libfoobar.

Zfz @ zfz :~ /Program $./foobar

This is foo! Library2 is

Foo () = foo

This is library1 is called

Bar () = bar

A static Link Library is a copy-based link process. What is a "replicaset" link process? When a static Link Library is connected to an application, the linker copies the static Link Library to the final executable code. For example, there are two applications A and B, both of which must use the functions provided by libfoobar.. Then, when compiling link A, the linker will copy A libfoobar. the final executable code from a to A goes to libfoobar. the debugging information in a is also copied. Similarly, when you connect to B, the linker also copies a libfoobar. the final executable code from a to B goes through. This is the meaning of the "replicaset" link.

View the dynamic link library 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)

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.