Use of "Go" Linux Static Library and shared library

Source: Internet
Author: User

Original URL: http://blog.csdn.net/heyabo/article/details/11688517

Disclaimer : As the title shows, this article is about the static library and the shared library under Linux, and the Dynamic Link library under window is more visible in this post: Analysis of Windows dynamic link library DLL. Although the principle, the idea is similar, but the detail is different.

First, Static library

  1. Concept: The static library refers to packaging all the relevant target files into a single file-the static library file, ending with. A. Static libraries can be used as Linker Input, the linker copies the code of the function used in the program from the library file to the application. once the link is complete, the static library is not required to execute the program .Note 1: Because every application that uses a static library needs to copy the code of the function used, the statically linked file is larger. NOTE 2: In UNIX systems, static libraries reside on disk in a special file format called archive (archive). Archive Fileis a set of linked relocatable target files with a header that describes the size and location of each member's target file (the archive file name is identified by the suffix. a). 2. Creation and applicationSuppose we want to provide the following vector functions in a static library called LIBVECTOR.A: [CPP]View Plaincopyprint?
    1. //ADDVEC.C
    2. void Addvec (int* x, int* y, int*z, int n)
    3. {
    4. int i=0;
    5. For (; i< n;++i)
    6. Z[i] = X[i] + y[i];
    7. }

[CPP]View Plaincopyprint?
    1. Multvec.c
    2. void Multvec (int*x, int* y, int* z, int n)
    3. {
    4. int i = 0;
    5. For (; i < n; ++i)
    6. Z[i] = x[i] * Y[i];
    7. }


Use ARTool to create a static library file: to UseThis library, write an application (which calls the functions in the Addvec Library): [CPP]View Plaincopyprint?
  1. /* MAIN2.C */
  2. #include <stdio.h>
  3. int x[2] = {1, 2};
  4. int y[2] = {3, 4};
  5. int z[2]={0};
  6. int main ()
  7. {
  8. Addvec (x, Y, Z, 2);
  9. printf ("z = [%d%d]\n", z[0], z[1]);
  10. return 0;
  11. }


compiling- links- RunProgram: Note 1-static ParametersTells the compiler driver that the linker should build a fully executable target file that can be loaded into storage and run without further linking at load time- that is, a one-time static link is complete, does not allow the existence of dynamicNOTE 2: When the linker runs, it determines that the ADDVEC symbol defined by the ADDVEC.O is MAIN2.O referenced, so it copies addvec.o to the executable file. Because no symbols defined by MULTVEC.O are referenced in the program, the linker does not copy the module to the executable file. At the same time, the linker copies the PIRINTF.O module in LIBC.A and many other modules in the C runtime system. The complete behavior of the linker can be as follows: second, shared library 1. Concept: A shared library is a target module (denoted by a. So suffix) that, at run time, can be loaded into any memory address and linked to a program in memory, a process called dynamic linking, by a call Dynamic linkerProgram to execute. 2. Classification: Based on the loading and linking of shared libraries Timingcan also be divided into: A)Application when loading itselfDynamically link and load shared libraries; B)Application During the runTwo cases of dynamically linking and loading shared libraries. 2-a: Dynamically linking and loading shared libraries when the application itself loads 2-a.1 Basic idea is: When you create an executable, you statically execute some links (the Relocation and symbol table information of the shared library, not code and data), and then dynamically complete the linking process when the application loads. 2-a.2 creation and applicationCreate a creation similar to a static library, assuming we now want to provide the following Addvec and Multvec functions in a shared library called libvector.so: the following uses -shared OptionsTo instruct the linker to create a shared target file (that is, a shared library), link and run the program:   Note 1-fpic OptionsInstructs the compiler to generate location-independent code whose dynamic linking process can be as follows: NOTE 2: Instead of copying any libvector.so real code and data sections in the executable P2, the linker copies some relocation and symbol table information, which enables the runtime dynamic linker to resolve references to code and data in libvector.so and relocate the completed link task. Where relocation is required:
    • 1) Reposition the text and data of the libc.so to a memory segment;
    • 2) reposition the text and data of the libvector.so to another memory segment;
    • 3) Reposition all references to libc.so and libvector.so defined symbols in the P2.
The last linker passes control to the application. from this point on, the location of the shared library is fixed and will not change during the execution of the program.2-b: Dynamically linking and loading shared libraries during application run 2-b.1 Concept: Unlike a case, in this case: the application in the process of runningRequires the dynamic linker to load and link to any shared library without having to link those libraries to the app at compile time. 2-b.2 Application ExamplesThe Linux system provides a set of APIs for the application to load and link shared libraries during the run: [CPP]View Plaincopyprint?
  1. #include <dlfcn.h>
  2. /* load and link shared library filename
  3. FileName: The name of the shared library
  4. Flag has: Rtld_lazy, Rtld_now, both can and Rtld_global Express take or
  5. */
  6. void *dlopen (const char *filename, int flag); //Joschengong Returns a pointer to the execution handle, otherwise returns null
  7. /* Returns the corresponding address of the symbol according to the handle and symbol of the shared library operation
  8. Handle: Shared library operation handle
  9. Symbol: The symbolic name to be referenced
  10. */
  11. void *dlsym (void *handle, char *symbol); //Joschengong Returns a pointer to the execution symbol (that is, the address) and returns null if an error occurs
  12. /* If no program is using this shared library, uninstall the shared library */
  13. int Dlclose (void *handle); //If the unload succeeds, return 0, otherwise return-1
  14. /* Catch a recent error */
  15. Const Char *dlerror (void); //If the previous call to Dlopen,dlsym or dlclose fails, an error message is returned, otherwise null is returned


Example :

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dlfcn.h>
  4. int x[2] = {1, 2};
  5. int y[2] = {3, 4};
  6. int z[2] ={0};
  7. int main ()
  8. {
  9. void *handle;
  10. void (*addvec) (int *, int *, int *,int);
  11. Char *error;
  12. Handle = Dlopen ("./libvector.so", Rtld_lazy);
  13. if (!handle) {
  14. fprintf (stderr, "%s\n", Dlerror ());
  15. Exit (1);
  16. }
  17. Addvec = Dlsym (handle, "Addvec");
  18. if (Error = Dlerror ()) = NULL) {
  19. fprintf (stderr, "%s\n", Dlerror ());
  20. Exit (1);
  21. }
  22. Addvec (x, Y, Z, 2);
  23. printf ("z = [%d%d]\n", z[0], z[1]);
  24. if (dlclose (handle) < 0) {
  25. fprintf (stderr, "%s\n", Dlerror ());
  26. Exit (1);
  27. }
  28. return 0;
  29. }


Run Result:-ldl parameter: Indicates that the generated object module needs to use the shared library

Referebces:

1. "In-depth understanding of computer Systems" Chapter 7th: Link p448-p479

2. Creation and use of static libraries, shared libraries, and dynamic libraries: http://bbs.chinaunix.net/thread-2037617-1-1.html

3. Analysis of Linux Dynamic library: http://www.ibm.com/developerworks/cn/linux/l-dynamic-libraries/

4. dlopen:http://baike.baidu.com/link?url=vswi42a-ixfuf5selbjxdrexuy0bvywhedccyozsnh93ark0ntmi4ydhhrvt-bio2_ F-swu2onuymnwxeugvmq

"Go" Linux Static library and shared library use

Related Article

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.