Create and call dynamic and static libraries in Linux

Source: Internet
Author: User
Each program is essentially linked to one or more databases. For example, a program using the C function is linked to the C Runtime Library, and a GUI program is linked to the window library. In either case, you have to decide whether to link to static library (static libary) or dynamic library (Dynamic libary ).
Link to the static library will make your program bloated and difficult to upgrade, but it may be easier to deploy.
Link to the dynamic library will make your program light and easy to upgrade, but it will be difficult to deploy

Recently written. the C file is relatively large. First, we decided to generate a library under Linux to call our compiled functions. In this way, our main functions seem concise and not very crowded, the following is a sample instance.

File directory:

/Mnt/Windows/linuxso/

Files under the file directory:

Test1 (). c test2.c main. c

File test1 (). C:

 

#include<stdio.h>
#include<stdlib.h>
void hello()
{
printf("hello ,xmphoenix\n");
}
void linuxso()
{
printf("i can use the linux so\n");
}
void parso(int a)
{
int c;
int b=7;
c=b+a;
printf("i get the sum is %d\n",c);
}

 

File Test2 (). C content:

#include<stdio.h>
#include<stdlib.h>
void xmphoenix(int a)
{
if(a==9)
{
printf("I come frome hubei\n");
}
else
printf("i cannot receive your command\n");
}

 

File main. C content:

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
hello();
linuxso();
parso(9);
xmphoenix(9);
return 0;
}

 

The specific compilation process is as follows:

Static Library:

The static library puts a series of object files in the same file (similar to the. Lib file in Windows ). When you provide a static library to the linker, the connector will search for the static library, find the object files he needs, extract them, and link them to your program, just like the files you directly provide.

The CR Mark tells ar to encapsulate the object file (archive). We can use the NM-s command to view the content of the. A file;

-L specifies the Lib search path, and-L specifies the name of the Linked Library-ltest, that is, the link libtest.

Dynamic library compilation and calling:

The dynamic library (static Lib) can also be a shared library (shared Lib). The general suffix is. So. Dynamic and Static libraries are similar. They are a collection of object files, but they are organized differently. Similarly, their connection methods are different. The dynamic library is used only for links during execution and does not compile the corresponding parts into the program, A library can be used by multiple programs. Therefore, it can be called a shared library. The static library is integrated into the program, and each program uses its own library when linking.

-FPIC tells GCC to compile the source code into a shared object file, and the position-independent code is a non-location-dependent code.

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.