6. Static function library design

Source: Internet
Author: User

6. Static function library design

In program design, the use of functions is inseparable. External functions are required in the design of Linux applications. This is mainly provided by function libraries and system tuning.

?

The difference between the two:

The system calls in the Linux kernel, and the library is in user space.

Function Library Classification:

???? The library can be divided into: Static link library and dynamic link library according to the link way.

?

These libraries used by Linux applications are mainly found in the/lib directory and the/usr/lib directory, where the dynamic library is named *.so.*, and the static function library is named in *.a Way.

/lib:

/usr/lib:

Features of the static link library:

The library function code used by the program is copied to the program at the time of the link. The problem: If more than one process is running in memory and using the same library function, there will be multiple copies, which is a waste of space. Such as:

?

Compile options:

    1. Link Dynamic library By default when linking under Linux
    2. If you need to use a static library, you need to use the compile option:-static

Example: Gcc–static exm.c–o EXM

Let's look at a process where we compile C files often. See below that the program uses the *.so.* dynamic library.

The size of the resulting file:

After adding the-static parameter, the compilation error occurs:

This is because the feature is not available in the version after Redhat 6.4. We need to install the library:

glibc-static-2.12-1.80.el6.i686.rpm

ll look at the static compilation generated by the file relatively large:

?

To make a static library:

Use your own static library:

-LNAME:GCC when linking, only the C function library is linked by default, and for other libraries, you need to use the-l option to indicate which libraries need to be linked:

Example: gcc hello.c–lmylib–o hello.

Note: The name of the static link library:

???? LIBMYLIB.A: Which Lib is required, that is, the static link library name is LIBMYLIB.A at the time of reference is-lmylib.

???? The name of a dynamic-link library is just like static, with a suffix of. So.

?

Instance:

?

/*************************************************************************

> File NAME:MYLIB.C

> Author:forfish

> Mail: [email protected]

> Created time:fri 07:44:10 AM EST

************************************************************************/

#include <stdio.h>

int tax (int salary, int insurance) {

???? int tax_salary = salary-insurance;

???? int temp = tax_salary-3500;

???? if (temp<0) {

???????? printf ("Your salary is too low to hand on tax!\n");

???????? return 0;

????}

???? if (temp<=1500) {

???????? return temp*0.03-0;

????}

???? if ((temp>1500) && (temp<=4500)) {

???????? return temp*0.1-105;

????}

???? if ((temp>4500) && (temp<=9000)) {

???????? return temp*0.2-555;

????}

???? if ((temp>9000) && (temp<=35000)) {

???????? return temp*0.25-1005;

????}

???? if ((temp>35000) && (temp<=55000)) {

???????? return temp*0.3-2755;

????}

???? if ((temp>55000) && (temp<=80000)) {

???????? return temp*0.35-5505;

????}

???? else{

???????? return temp*0.45-13505;

????}

}

To package a build library:

Copy the generated library LIBMYLIB.A to the/usr/lib directory:

Write your application using the static link library we created:

The first is to create a C file mytest.c:

#include <stdio.h>

#include "tax.h"

void Main () {

???? int my_tax = 0;

???? My_tax = Tax (9500,1200);

???? printf ("I had to tax%d", my_tax);

}

The code for the header file Tax.h:

int tax (int salary, int insurance);

The compilation runs as follows:

?

?

?

?

?

6. Static function library design

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.