Linux C: Generation and use of static and dynamic libraries

Source: Internet
Author: User

Scene:

The main function requires two interfaces, a summation function, and a print function.

int sum (int i, int j); The number of two int is calculated.

void Show (int i, char* name); Prints the value of I and its name.

Now, you need to make:

A static library LIBCALC.A, providing the interface of sum;

A dynamic library libshow.so that provides the interface for show.

#include <stdio.h>#include"calc.h"#include"show.h"intMainvoid){    ints; inti =1; intj =2; S=sum (I,J); Show (I,"I"); Show (J,"J"); Show (S,"sum");}

CALC.C, calc.h

" calc.h " int sum (intint  j) {    return(i+j);} ~        
#ifndef __calc_h__ #define __calc_h__int sum (intint  j); #endif

SHOW.C, Show.h

" show.h " void Show (intChar* name) {    printf ("%s=%d\n " , name, i);}
#ifndef __show_h__ #define __show_h__void SHOW (intChar* name); #endif~         

Makefile:

CC = gccall:mainmain:libcalc.a main.c libshow.so    -o main main.c-l.-lcalc  -lshow
LIB:LIBCALC.A libshow.solibcalc.a:calc.o ar CRS libcalc.a calc.ocalc.o:calc.c -C calc.c-o Calc. OLIBSHOW.SO:SHOW.O -shared-o libshow.so show.oshow.o:show.c -fpic-wall-c show.c-o show.o
Clean: -rf *.o *.so *.a Main

Input: Make all---build libcalc.a,libshow.so Library and main executable program

Make lib---build libcalc.a,libshow.so library

Make clean---Clear the target file

Input: export ld_library_path= ' pwd '---Add current path to dynamic library PATH environment variable ld_library_path

[[email protected] libshow]# export ld_library_path= ' pwd '
[Email protected] libshow]# echo $LD _library_path
/work/jz2440/test/libshow\

Enter:./main test results.

I=1
j=2
Sum=3

=====================

Knowledge about dynamic libraries and static libraries:

1. Makefile example of Linux compiled dynamic libraries and static libraries http://blog.csdn.net/shaoxiaohu1/article/details/46943417

2. Three ways to properly locate and load the loader into the dynamic library:

A. Move libx.so to the system library file directory

B. Export ld_library_path= $LD _library_path: ' pwd '

C. Use the configuration file to tell the system the path to the dynamic library.

Vi/etc/ld.so.conf.d/xxxx.conf

/work/jz2440/test/libshow

3. ldd xxxx View dependent dynamic library files

[Email protected] libshow]# LDD main
Linux-vdso.so.1 = (0x00007fff1edc0000)
libshow.so =/work/jz2440/test/libshow/libshow.so (0x00002ad8d3511000)
libc.so.6 =/lib64/libc.so.6 (0x0000003a4fc00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003a4f800000)

Linux C: Generation and use of static and dynamic libraries

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.