The production and invocation of static library and dynamic library

Source: Internet
Author: User
Library: Essentially, a library is a binary form of executable code, and Linux systems support dynamic libraries (shared libraries) and static libraries. Linux under the library generally placed in B or/USRB. The header file is placed in the/usr/include.
Dynamic library: Libxxx.so with ". So" as the suffix. Because the compile time is not compiled into the target code, the program executes to the relevant function
function library to call the corresponding functions, so the file size is small, easy to upgrade.
Static Library: All the data in the library is integrated into the target code, and his advantage is obvious, that is, the compiled execution program does not need external function library support, because all the functions used are already compiled into the executable file. Of course this will also be called its disadvantage, because if the static function library changes, then your program must be recompiled, and the volume is larger.

Here we make the dynamic and static libraries separately in the Linux system:

First write two source programs (. c) and the corresponding header file:
[Yangni@yangni dir1]$ ls
fun1.c  fun1.h   fun2.c  fun2.h

Then generate fun1.c and fun2.c target files (. o) Separately:
[Yangni@yangni Dir1] $GCC-C fun1.c
[Yangni@yangni dir1]$ gcc-c fun2.c
[Yangni@yangni Dir1] $ls
fun1.c  fun1.h  fun1.o  fun2.c  fun2.h fun2.o

Generate Static library (. A) file static link (. o) for all destination files (. A):
[Yangni@yangni dir1]$ ar-rcs libname.a fun1.o fun2.o

To generate a dynamic library:

[Yangni@yangni dir1]$ gcc-shared-fpic-o libname.so *.c
[yangni@yangni dir1]$ ls
fun1.c fun1.h  FUN1.O  fun2.c  fun2.h  fun2.o  libname.a  libname.so

Write main.c run but found error:
[Yangni@yangni ku]$ gcc-i./dir1 main.c-o Test(Dir1 is the path to the header file)

[Yangni@yangni dir1]$ gcc main.c
/tmp/cclfo4fm.o:in function ' main ':
MAIN.C: (. text+0x5): Undefined reference to ' fun1 '
MAIN.C: (. text+0xa): Undefined reference to ' fun2 '

Because MAIN.C contains only the fun1 fun2 function body, FUN.C is integrated into the library, so a link library is required:

[Yangni@yangni dir1]$ gcc main.c-o app. Bname.a(App rename a.out)
[Yangni@yangni dir1]$ ls


Rely on Dynamic Library compilation:
Direct compilation will show errors, and you will need to copy the. So file to the folder under Linux for storing the library files:

[Yangni@yangni dir2]$ sudo cp libname.so/lib
Or you can set the current path to a standard dynamic library by modifying the environment variable:
[Yangni@yangni dir2]$ Export ld_library_path= ' current path '

And then compile:
[Yangni@yangni dir2]$ gcc-i./dir2 main.c-o app-l./dir2-lname       
[yangni@yangni dir2]$ ls
app     fun1.h  fun2.c  fun2.o     libname.so
fun1.c  fun1.o  fun2.h libname.a main.c
-I Specifies the additional library path that is required, and-l specifies the search path for the library.
[Yangni@yangni dir2]$./app
Hello World!i can ' t believe it!


invoke Static Library compilation:
[Yangni@yangni dir2]$ Gcc-static-i./dir2 main.c-o app-l./dir2–lname
Can not FIND–LC

Note: You will be prompted not to find-LC
Because our system is not installed with-LC corresponding static library, so we need to use Yum install glibc-static installation
[Yangni@yangni dir2]$ Yum Install glibc-static
Compile again:
Yangni@yangni ku]$ gcc-static Main.c-i./dir2-o app-l./dir2-lname (static compilation must specify the location of the static library with-L)
[Yangni@yangni ku]$ ls
App Dir1 Dir2 main.c



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.