Linux static libraries and shared libraries

Source: Internet
Author: User

1. What is a static library
Static libraries are similar to static lib in Windows

For static Lib in Windows, refer to the Windows Dynamic link library DLL

Features: Contains function code declaration and implementation, after linking all the code is embedded in the host program .
Used only at compile time, the static library is no longer required for execution.

2. Static Library Writing
Examples are as follows:
Addvec.c

void Addvec (intint int int n)  {       int i=0 ;         for (; i< n;++i)             = X[i] + y[i];  }  

Multvec.c

void Multvec (intintint*  int  N)  {       int 0 ;         for (; i < n; + +i            ) = x[i] * y[i];  }

To create a static library file using the AR tool:

3. Static Library use
Examples are as follows:
Test2.c

#include <stdio.h>intx[2] = {1,2}; inty[2] = {3,4}; intz[2]={0}; intMain () {Addvec (x, Y, Z,2); printf ("z = [%d%d]\n", z[0], z[1]); return 0; }

Compile-Link-run Program

1) The-static parameter , which indicates a static link, compiles a complete executable target file.
2) When the linker is linked, it is judged that the Addvec function in ADDVEC.O is called in the main function.
No functions are called in MULTVEC.O, so the linker will only copy the ADDVEC.O to the executable file .

4. What is a shared library
Shared libraries are like dynamic-link library DLLs in Windows

Features: Includes function code declaration and implementation.
Used only at run time, linked and loaded by the dynamic linker.

Depending on the timing of linking and loading shared libraries, the following two classes are available:
1) Self-loading shared library.
2) Run-time load sharing library

5. Self-loading shared library.
Similar to implicit links in windows
when linking , link the declaration information of the shared library to the executable file,
when the application loads , the dynamic-link library resolves the declaration information, loads the implementation of the shared library into storage, and repositions the application to declare the information to the actual address.

6. Example of self-loading shared library usage
Use the-shared parameter to instruct the compiler to create a shared library .
As shown below, we created a shared library and used the shared library through its own loading type.

1) The-fpic parameter, which instructs the compiler to generate code-independent code
2) When linking, there is no copy of the shared library libvec.so implementation, only a few relocation and symbol table information is copied
3) When the program loads, the dynamic linker resolves the reference to the code and data in the shared library libvec.so, relocating the completed link task.
Reposition libvec.so text and data to memory segments
Relocate libvec.so to above memory segment referenced in P2
Finally, the linker passes control to the program, at which point the shared library location is fixed.

7. Run-time load-sharing library
Similar to explicit links in windows
No compile-time links are required to load and unload shared libraries during the run.

8. Run-time load sharing Library use example
Linux provides a set of APIs to load and unload shared libraries during the run, as follows:
#include <dlfcn.h>

/*load and link shared libraries filename: The name of the shared library flag has: Rtld_lazy, Rtld_now, both can and Rtld_global to express take or*/  void*dlopen (Const Char*filename,intFlag);//returns a pointer to the execution handle if successful, otherwise returns null  /*according to the shared library operation handle and symbol, return the address of the symbol handle: Shared library operation handle symbol: Symbolic name to be referenced*/  void*dlsym (void*handle,Char*symbol);//Returns a pointer to the executed symbol (that is, the address) if successful, and returns null if an error occurs  /*if no program is using this shared library, uninstall the shared Library*/  intDlclose (void*handle);//If the uninstallation succeeds, returns 0, otherwise returns-1  /*Catch a recent error*/  Const Char*dlerror (void);//if the previous call to Dlopen,dlsym or dlclose fails, an error message is returned, otherwise null is returned

Based on the above API, we can easily load and unload shared libraries as follows:

#include <stdio.h>#include<stdlib.h>#include<dlfcn.h>intx[2] = {1,2}; inty[2] = {3,4}; intz[2] ={0}; intMain () {void*handle; void(*addvec) (int*,int*,int*,int); Char*error; Handle= Dlopen ("./libvector.so", Rtld_lazy); if(!handle) {fprintf (stderr,"%s\n", Dlerror ()); Exit (1); } Addvec= Dlsym (Handle,"Addvec"); if(Error = Dlerror ())! =NULL) {fprintf (stderr,"%s\n", Dlerror ()); Exit (1); } addvec (x, Y, Z,2); printf ("z = [%d%d]\n", z[0], z[1]); if(Dlclose (handle) <0) {fprintf (stderr,"%s\n", Dlerror ()); Exit (1); }        return 0; }  

To run the program:

Where the-ldl parameter indicates that a shared library is required for the program to run

Linux static libraries and shared libraries

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.