Linux dynamic link library use method share _linux shell

Source: Internet
Author: User
Tags mul

1. Preface

In the actual development process, each module will involve some common functions, such as reading and writing files, search, sorting. In order to reduce the redundancy of the Code and improve the quality of the code, these common parts can be extracted and made into a common module library. Dynamic link libraries enable you to share common functions among multiple modules. Before watching the "self-self-cultivation of the programmer" in the program link and loading process, these things are low-level, for understanding the process of compiling the program is good. http://www.ibm.com/developerworks/cn/linux/l-dynlink/Blog Describes the program's link and loading process. This article focuses on the application, how to write and use the dynamic link library, the subsequent use of dynamic link library to implement a plug-in program.

2. Dynamic Link Library production

Dynamic link library compared with ordinary program, there is no main function, it is the realization of a series of functions. Production of so dynamic link library files through shared and fpic compilation parameters. When a program calls a library function, it only needs to connect to the library. For example, the following implementation of a simple integer four transport dynamic link library, the definition of caculate.h and caculate.c two files, production libcac.so dynamic link library.

The program code is as follows:

Copy Code code as follows:

/*caculate.h*/

#ifndef Caculate_head_
#define Caculate_head_
Addition
int add (int a, int b);
Subtraction
int sub (int a, int b);
Division
int div (int a, int b);
Multiplication
int mul (int a, int b);

#endif

/*CACULATE.C File * *
#include "caculate.h"

Ask for two numbers and
int add (int a, int b)
{
return (A + B);
}
Subtraction
int sub (int a, int b)
{
return (A-B);
}
Division
int div (int a, int b)
{
return (int) (A/b);
}
Multiplication
int mul (int a, int b)
{
Return (A * b);
}

Compile production libcac.so files as follows: Gcc-shared-fpic Caculate.c-o libcac.so
Write a function that the test program calls this dynamic-link library, and the program looks like this:

Copy Code code as follows:

#include <stdio.h>
#include "caculate.h"

int main ()
{
int a = 20;
int B = 10;
printf ("%d +%d =%d\n", A, B, add (A, b));
printf ("%d-%d =%d\n", A, B, sub (A, b));
printf ("%d/%d =%d\n", a, B, Div (A, b));
printf ("%d *%d =%d\n", A, B, Mul (A, b));
return 0;
}

Compile the production executable file main as follows: GCC main.c-o main-l./-LCAC (where-L indicates the path of the dynamic link library, and the name of the link library after-L, omitting Lib)
The results of the program execution are as follows:

3, get the function of dynamic link library
Linux provides functions for Dlopen, Dlsym, Dlerror, and dlcolose functions to get dynamic-link libraries. Through this four functions can implement a plug-in program to facilitate the expansion and maintenance of the program. The function format looks like this:

Copy Code code as follows:

#include <dlfcn.h>

void *dlopen (const char *filename, int flag);

Char *dlerror (void);

void *dlsym (void *handle, const char *symbol);

int dlclose (void *handle);

Link WITH-LDL.

Dlopen () is a powerful library function. The function opens a new library and loads it into memory. This function is used primarily to load symbols in libraries that are not known at compile time. Write a test program call the above production libcac.so library looks like this:

Copy Code code as follows:

#include <stdio.h>
#include <dlfcn.h>

#define DLL_FILE_NAME "Libcac.so"

int main ()
{
void *handle;
Int (*func) (int, int);
Char *error;
int a = 30;
int b = 5;

Handle = Dlopen (Dll_file_name, Rtld_now);
if (handle = NULL)
{
fprintf (stderr, "Failed to open Libaray%s error:%s\n", Dll_file_name, Dlerror ());
return-1;
}

Func = Dlsym (handle, "add");
printf ("%d +%d =%d\n", A, B, Func (A, b));

Func = Dlsym (handle, "sub");
printf ("%d +%d =%d\n", A, B, Func (A, b));

Func = Dlsym (handle, "div");
printf ("%d +%d =%d\n", A, B, Func (A, b));

Func = Dlsym (handle, "mul");
printf ("%d +%d =%d\n", A, B, Func (A, b));

Dlclose (handle);
return 0;
}

The results of the program execution are as follows: GCC call_main.c-o call_main-ldl

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.