Linux shared libraries

Source: Internet
Author: User

Library: A set of pre-compiled functions that are typically stored in the/lib and/usr/lib directories in Linux standard system library files

Static Library: In the process of compiling the program, it is linked to the source code written by the program writer, and the static library ends with. A.

Shared libraries: When the program is running, you simply need to describe the shared library files as you compile. Shared libraries are accepted with. So.

Header file: The declaration used to save the program.

Makefile file: Makefile brings the advantage is-"automated compilation", once the dependencies of each file and its compilation method, only need a make command, the entire project is automatically compiled, greatly improving the efficiency of software development.

A. Shared Library example:

1, in bill.h header file, write function to find the largest element of the function declaration;

$ more Bill.h
Bill Int (int a[], int n);

2, the introduction of the header file in the Bill.c file, write the implementation of the function;
$ more BILL.C
#include "bill.h"
Bill Int (int a[], int n)
{
int i,max = A[i];
for (i = 0; i < n; ++i)
{
if (Max < a[i])
{
max = A[i];
}
}
return Max;
}
3, write the main program content in MAIN.C, call the function, at this time, need to introduce the header file
$ more MAIN.C
#include <stdio.h>
#include "bill.h"
int main ()
{
int MAX;
int ar[10] = {1,23,21,34,45,56,57,48,90,100};
MAX = Bill (ar,10);
printf ("%d\n", MAX);
return 0;
}

4, compile the shared library and MAIN.C;
$ gcc-shared-fpic Bill.c-o libbill.so

The results are as follows:

$ ls
BILL.C bill.h libbill.so main.c

5, set the environment variable (environment variable is not set, then the system only finds the shared library under the default standard path, after setting the environment variable, the system first looks for the shared library under the path specified by the environment variable)

/* This environment variable is set only for shared libraries */
$ Export ld_library_path=./

6. Compile and generate executable program/* where-L. Indicates that the lookup path to the library file is the current path (.),-lbill means that the file share library file you want to use is named Bill

Header file because it is in the current path, it can be omitted to write, when in the system standard path can also omit do not write */
$ gcc-o main main.c-l.-lbill

7, compile successfully, execute main program
$ ls
BILL.C bill.h libbill.so Main MAIN.C
$./main
100

two. Makefile of shared libraries:

still above for example:

1. The source code in the above example is:

$ ls
BILL.C bill.h main.c Makefile
2. Makefile files are as follows:

$ more Makefile
MAIN:MAIN.O libbill.so
Export ld_library_path=./
Gcc-o main main.c-l.-lbill
MAIN.O:MAIN.C bill.h
Gcc-c MAIN.C
Libbill.so:bill.c
Gcc-shared-fpic Bill.c-o libbill.so

3, make the process:

$ make
Gcc-shared-fpic Bill.c-o libbill.so
Export ld_library_path=./
Gcc-o main main.c-l.-lbill

4. The results of the implementation are:

$./main

100

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.