Linux GCC compilation uses dynamic, static link libraries

Source: Internet
Author: User

Original source: http://blog.csdn.net/a600423444/article/details/7206015

Under Windows, the dynamic-link library is a file with a. dll suffix, and two in Linux, a file with a suffix of. So.
The advantage of a dynamic link library is that it saves memory space.


1. Create a dynamic link library under Linux
When using GCC to compile the program, just add the-shared option, so that the generated execution program is the dynamic link library.
For example, there are files: hello.c x.h main.c

[Plain]View Plaincopy
    1. Compiling: gcc hello.c-fpic-shared-o libhello.so



Where the-fpic option is used to represent code compiled as location-Independent, the compiled code is location-dependent without this option,
Therefore, dynamic loading is a way to satisfy different calls by means of code copy, and not to achieve the purpose of real code snippet sharing.


The MAIN.C and hello.so Dynamic Library

[Plain]View Plaincopy
    1. GCC main.c-l.-lhello-o Main


First, dynamic link library

1. Create a hello.so dynamic library

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. void Hello () {
    3. printf ("Hello world\n");
    4. }
    5. Compilation: Gcc-fpic-shared Hello.c-o libhello.so


2.hello.h header File

[CPP]View Plaincopy
    1. void Hello ();


3. Link the dynamic Library

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. #include "hello.h"
    3. int main () {
    4. printf ("Call Hello ()");
    5. Hello ();
    6. }
    7. Compiling: GCC main.c-l.-lhello-o Main

The-l option here is to specify the path that the compiler searches for when searching for dynamic libraries, and tells the compiler the location of the Hello Library. "." means the current path.


3. After compiling enough to execute./main, you will be prompted:

[Plain]View Plaincopy
    1. In function ' main ':
    2. MAIN.C: (. text+0x1d): Undefined reference to ' hello '
    3. Collect2:ld returned 1 exit status

This is because the compiler was not found when linking the Hello Dynamic library.
Workaround:

[Plain]View Plaincopy
    1. sudo cp libhello.so/usr/lib/

In this way, execution succeeds in entering:
Call Hello ()


Second, Static library

Files are: main.c, hello.c, hello.h
1. Compile the static library hello.o:

[Plain]View Plaincopy
    1. GCC hello.c-o hello.o #这里没有使用-shared


2. Archive the target document

[Plain]View Plaincopy
    1. Ar-r libhello.a hello.o #这里的ar相当于tar的作用, package multiple targets.

Program ar mate parameter-R creates a new library libhello.a and packages the files listed in the command line into it. This method, if libhello.a already exists, will overwrite the current file, otherwise it will be newly created.

3. Link Static Library

[Plain]View Plaincopy
    1. GCC main.c-lhello-l.-static-o Main

The-static option here is to tell the compiler that Hello is a static library.
Or:

[Plain]View Plaincopy
    1. GCC main.c libhello.a-l.-O Main

So you don't have to add-static.

4. Execution./main

Output: Call Hello ()


Third, with the help of the LDD implementation program to analyze the dynamic library search situation

LDD Main

Results:
Linux-gate.so.1 = (0x00efd000)
libhello.so =/usr/lib/libhello.so (0x00f6b000)
libc.so.6 =/lib/libc.so.6 (0x001a5000)
/lib/ld-linux.so.2 (0x00eb8000)
Print "Not a dynamic executable" if the target program does not have a link to the dynamic library

Linux GCC compilation uses dynamic, static link libraries (GO)

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.