linux programming library

Bowen link Reference:

https://www.cnblogs.com/guochaoxxl/p/7141447.html

https://www.cnblogs.com/tuhooo/p/8757192.html

A: library functions Introduction

These functions are compiled in advance, and then place them in a special file, these files are called object code libraries. Library file can be used by the linker to link with the program, so that you do not always perform all of these programs is to compile a general function.

C standard library function name libc, contains the basic memory management functions, or input and output operations.

Two forms of libraries: static and shared libraries.

II: static library

Static library called by the document text. It is a collection of .o files. linux in the suffix .a static library

Static library of code at compile time has been linked to the application.

Use ar tool maintenance and management of static libraries

Establishing and using static libraries

1: write the source file

Source 1:

one.c

#include"one.h"

 

void one(int 1,int 2)

{

  a = int1+int2;

  printf("%d",a);

}

one.h

#ifndef one_h

#define one_h

 

#include<stdio.h>

void one(int1,int2);

 

#endif

 

Source 2:

main.c

#include"one.h"

Mian you (you argc, char ** ARGV)

{

  int a,b;

  scanf("%d%d",&a,&b);

  one(a,b);

  return 0;

}

2: .o files are generated

gcc -c  one.c

3: create a static library linking ar rcs libmylib.a one.o

This establishes a good static library in the current directory

4: Test static link library

gcc -o testc main.c -static -L. -lmylib

-static compiler specified static link library, -L. specify the path to a static library for the current path

summary

使用静态库可以使程序不依赖任何其他库而独立运行,但是会占用很多的内存空间已及磁盘空间,而且如果库文件更新则要重新编译源码,不灵活。

附加Makefile 文件

 OBJ=main.o one.o                            
  2 testc:$(OBJ) main.h
  3     gcc -o testc $(OBJ)
  4 main.o:main.c
  5 one.o:one.c
    
  8 .PHONY:cleanA clean
  9 cleanA:
 10     rm testc $(OBJ)
 11 clean:
 12      rm $(OBJ)
~                         

动态库编译

源码同上

gcc -shared -fPIC -o libcal.so one.o


gcc -o testCal testCal.c -L. -lcal

 

Guess you like

Origin www.cnblogs.com/zoutingrong/p/12542728.html