Linux library function creation (static library, dynamic Library)

Source: Internet
Author: User

Linux library function creation (static library, dynamic Library)

Static libraries and dynamic libraries

How to link

There are two types of links: static links, dynamic links

Static Links:

Add the contents of the library to the executable program by the linker at link time

Static links are characterized by:

Advantages:

Less dependency on the operating environment, with better compatibility

Disadvantages:

The resulting program is larger, requires more system resources, and consumes more time when it is loaded into memory.

Library functions have been updated and the application must be recompiled

Dynamic Links:

The connector only establishes a link to the desired library function when it is linked, and the required resources are transferred into the executable program when the program is run.

Features of dynamic Links:

Advantages:

The corresponding resource function is only transferred when needed.

Simplified program upgrade with smaller program volume

Implement resource sharing between processes (avoid duplicate copies)

Disadvantages:

relies on dynamic libraries and cannot run independently

Dynamic library dependent version issue critical

/*************************************************************************
> File NAME:MYPRINTF.C
> Author:lsgxeva
> Mail: [email protected]
> Created time:2017 September 28 Thursday 11:52 57 seconds
************************************************************************/

#include <stdio.h>

void myprintf (void)
{
printf ("Hello, world!\n");
}

/*************************************************************************
> File name:myprintf.h
> Author:lsgxeva
> Mail: [email protected]
> Created time:2017 September 28 Thursday 11:53 15 seconds
************************************************************************/

#ifndef _myprintf_h_
#define _myprintf_h_

extern void myprintf (void);

#endif//_myprintf_h_

/*************************************************************************
> File name:mytest.c
> Author:lsgxeva
> Mail: [email protected]
> Created time:2017 September 28 Thursday 11:54 26 seconds
************************************************************************/

#include "myprintf.h"

int main ()
{
Myprintf ();
return 0;
}

Directory structure

Drwxr-xr-x 5 root root 94 September 28 12:22.
Drwxr-xr-x 5 root root 54 September 28 11:08..
-rw-r--r--1 root root 360 September 11:53 MYPRINTF.C
-rw-r--r--1 root root 380 September 11:54 myprintf.h
-rw-r--r--1 root root 351 September 12:22 mytest.c
Drwxr-xr-x 2 root root 6 September 12:24 output
Drwxr-xr-x 2 root root 6 September 11:56 shared
Drwxr-xr-x 2 root root 6 September 12:23 static

Make a static link library

Make a dynamic link library

Troubleshoot common and easy ways to open a dynamic library:
Declaring a temporary variable environment
Export Ld_library_path=./lib: $LD _library_path

or modify the search path where the/etc/ld.so.conf file is added to the library, one line at a path.
sudo ldconfig update/etc/ld.so.cache file
The library files for all paths in/etc/ld.so.conf are cached to/etc/ld.so.cache.

Note: Compiling parameters for generating shared libraries-shared incorrectly for building executables will result in a segment error when the program runs!

Compilation produces dynamic-link libraries and supports major and minor version numbers.

The difference between executable files when dynamic linking and static linking:

Linux library function creation (static library, dynamic Library)

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.