gcc create static library and shared library

Static Libraries and Dynamic (Shared) Libraries
Static Libraries : When a compiler compiles a program that uses the functional code provided by the library, the code is copied to the program and then compiled into an executable program. This kind of library becomes a static library
Shared library : a shared library is more efficient than a static library The processing method of the library is more flexible, so the executable file produced by it is smaller, and its file suffix is ​​.so, which
   means that the executable program linked by the shared object (shared object) only contains the table of the functions it needs. The machine code of the external function is not copied from the object file.
  When the executable file starts to execute, the operating system copies the machine code of the external function from the shared file on disk to the memory. This process is called dynamic linking.
  It makes executables leaner and saves disk space because shared libraries can be shared among multiple programs The
  operating system allows a copy of the shared library in physical memory to be used by all running programs, thus saving memory
  . Shared libraries make Programmers can update new library files at any time as needed. As long as the interface remains unchanged, the source program that uses it does not need to be recompiled.
  Based on these advantages, when there are both shared libraries and static libraries in the system, gcc will use shared library files by default.

When using the lname option to specify the library name, gcc first searches if there is a libname.so file in the path, if so, use the file, if not, continue to find if there is a
libname.a static library file
GCC creates a static library file method

 

calc.h
  int aver ( int , int );
int sum ( int , int );
aver.c
#include "calc.h"
int aver(int num1,int num2)
{
    return (num1+num2)/2;
}
sum.c
#include "calc.h"
int sum(int num1,int num2)
{
    return (num1+num2);
}
mian.c
#include <stdio.h>
#include "calc.h"

int main(){
    int v1,v2,m,sum2;
    v1=12;
    v2=10;
    m=aver(v1,v2);
    sum2=sum(v1,v2);
    printf("aver(%d,%d)=%d\n",v1,v2,m );
    printf("sum(%d,%d)=%d\n",v1,v2,sum2 );

}

 

Compile as object file without assembly and link
gcc -c -o aver.o aver.c
gcc -c -o sum.o sum.c
ar rc libmycalc.a aver.o sum.o
gcc main.c -static - L. -lmycalc -o static-main

Gcc static compilation in linux environment /usr/bin/ld: cannot find -lc Error reason and solution
Reason:
When this problem occurs, the -static option must be in the Makefile. This is actually because libc.a is not found during static linking. You
need to install glibc-static.xxx.rpm, such as glibc-static-2.12-1.107.el6_4.2.i686.rpm, or yum install glibc-static

How to create a shared library file in GCC

 

gcc -c -fPIC aver.c -o aver.o
gcc -c -fPIC sum.c -o sum.o
gcc -shared -o libmycalc.so aver.o sum.o
gcc main.c -L. -lmycalc - o
Before shared-main executes shared-main
vim /etc/profile
plus
export LD_LIBRARY_PATH=.
. /etc/profile
shared-main is much smaller than static-main

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750742&siteId=291194637