Commonplace C the making and using of dynamic function library (recommended) _c language

Source: Internet
Author: User
Tags mul

>>>>>> cliché C language Connect static function library production and use >> click to enter

2 making and using of dynamic function library

the manufacturing steps of the dynamic function library can be described in the following diagram, including

(1) Write functions of. c files (e.g. ADD.C, SUB.C, MUL.C, and DIV.C)

(2) Write Makefile, then make, realize the function of compiling and archiving storage

Compilation of functions: Use Gcc–c add.c-fpic to compile only the unlinked functions. c files, generating the target files (for example, ADD.O, SUB.O, MUL.O, and DIV.O) of the functions, respectively.

Archive of functions: Use Gcc-o libdynamic.so $ (objects)-share to archive the target files into the library.

(3) Write a header file (such as Kud.h), declare all functions in the function library, the purpose is to KUDMAIN.C function #include header file, you can call the corresponding function, so, complete the production of function library

2.1 Making example of dynamic function library

The content of the example establishes the dynamic function library libdynamic.so, which includes add, sub, mul, and DIV functions, and then references these 4 functions in the KUDMAIN.C function to achieve the subtraction of the two integers, the whole The structure of the file is

(1) Write a function of the. c File

4 function Files add.c, SUB.C, MUL.C, and div.c are used to make static function libraries without any changes.

(2) Writing header files

Change the header file name ku.h when the static function library is made to Kud.h, and the other content does not make any changes.

(3) writing makefile

Because there are differences between commands for making dynamic libraries and commands for making static libraries, you need to modify makefile.

objects = ADD.O sub.o mul.o div.o libdynamic.so

: $ (objects) Gcc-o libdynamic.so
  $ (objects)-shared ADD.O

: ADD.C
  gcc-c add.c-fpic

sub.o:sub.c
  gcc-c

sub.c-fpic mul.o:mul.c gcc-c mul.c-fpic DIV.O:DIV.C
  gcc-c div.c-fpic clean

: 
  RM libdynamic.so $ (objects)

(4) compile. c files using make, generate. o files, archive. o Files to function library libdynamic.so, complete the production of dynamic function library.

the use of 2.2 dynamic function library

(1) Writing kudmain.c, calling Add, Sub, mul, and DIV functions in libdynamic.so

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

int main (void)
{
int a,b;
A = ten;
b = 3;
printf ("a =%D.\NB =%d.\n", a,b);
printf ("Static a+b =%f.\n", add (a,b));
printf ("Static a-b =%f.\n", sub (a,b));
printf ("Static a*b =%f.\n", Mul (A,b));
printf ("static A/b =%f.\n", div (a,b));
return 0;
}

(2) Compile kudmain.c file using gcc kudmain.c–o kudmain.o–l./ku2–ldynamic, run./ KUDMAIN.O View the results of the run, resulting in an error, because the program ran the load dynamic function library, the function library was not found.

(3) Solving method

Method 1: Place the libdynamic.so in the/usr/lib directory, perform the CP Kud2/libdynamic.so/usr/lib, and run./KUDMAIN.O, the program runs normally and the problem is solved.

Method 2: using the environment variable Ld_library_path, when the program runs the load dynamic function library, it will first look up the function library in the directory specified by Ld_library_path, and then go to the/usr/lib to find it, perform the export LD _LIBRARY_PATH=/MNT/HGFS/WINSHARE/KUD/KUD2: $LD _library_path, then run./KUDMAIN.O, the program runs normally and the problem is solved.

2.3 Viewing dependencies using the LDD command

The LDD command is a shell script, not an executable program, that is used to see what dynamic function libraries an executable program contains, or which dynamic libraries are dependent on when the program executes.

$LDD KUDMAIN.O//visible KUDMAIN.O dependent on libdynamic.so

The above Cliché C language dynamic function library production and use (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.