Linux GCC compilation uses dynamic and static link libraries

In Windows, the dynamic link library is a file with a suffix of .dll, and in Linux, it is a file with a suffix of .so.

The advantage of dynamic link library is to save memory space.


1. Create a dynamic link library under Linux
When using GCC to compile a program, just add the -shared option, and the resulting executable program is a dynamic link library.
For example there are files: hello.c xh main.c

[plain] view plain copy

  1. Compile: gcc hello.c -fPIC -o libhello.so  



The function of the -fPIC option is: it means that the code is compiled into position-independent code. If this option is not used, the compiled code is position-dependent,
so when dynamically loading, the code copy is used to satisfy different calls, but it cannot achieve real The purpose of the code segment is shared.


The main.c is shared with the hello.so dynamic library

[plain] view plain copy

  1. gcc main.c -L. -lhello -o main  

 

1. Dynamic Link Library

1. Create hello.so dynamic library

[cpp] view plain copy

  1. #include <stdio.h>  
  2. void hello(){  
  3.     printf("hello world\n");  
  4. }  
  5. Compile: gcc -fPIC -shared hello.c -o libhello.so  


2.hello.h header file

[cpp] view plain copy

  1. void hello();  


3. Link dynamic library

[cpp] view plain copy

  1. #include <stdio.h>  
  2. #include "hello.h"  
  3.   
  4. int main(){  
  5.     printf("call hello()");  
  6.     hello();  
  7. }  
  8. Compile: gcc main.c -L. -lhello -o main  

The -L option here is to specify the path that the compiler searches when searching for dynamic libraries, telling the compiler the location of the hello library. "." means the current path.

 

3. After compiling enough, execute ./main, it will prompt:

[plain] view plain copy

  1. In function `main':  
  2.    
  3. main.c:(.text+0x1d): undefined reference to `hello'  
  4. collect2: ld returned 1 exit status  

This is because the compiler did not find it when linking the hello dynamic library.
Solution:

[plain] view plain copy

  1. sudo cp libhello.so /usr/lib/  

In this way, the execution is successful again:
call hello()

 

2. Static library

The files are: main.c, hello.c, hello.h
1. Compile the static library hello.o: 

[plain] view plain copy

  1. gcc hello.c -o hello.o #-shared is not used here  


2. File the target document

[plain] view plain copy

  1. ar -r libhello.a hello.o #The ar here is equivalent to the role of tar, packaging multiple targets.  

The program ar with the parameter -r creates a new library libhello.a and packs the files listed on the command line into it. This method, if libhello.a already exists, will overwrite the existing file, otherwise it will create a new one.

3. Link static library

[plain] view plain copy

  1. gcc main.c -lhello -L. -static -o main  

The -static option here tells the compiler that hello is a static library.
or:

[plain] view plain copy

  1. gcc main.c libhello.a -L. -o main  

This way you don't need to add -static

4. Execute ./main

Output: call hello()

 

3. Use the built-in ldd implementation program to analyze the dynamic library search situation

ldd main

Result:
linux-gate.so.1 => (0x00efd000)
libhello.so => ​​/usr/lib/libhello.so (0x00f6b000)
libc.so.6 => /lib/libc.so.6 (0x001a5000)
/lib /ld-linux.so.2 (0x00eb8000)
If the target program does not link dynamic libraries, print "not a dynamic executable"

Guess you like

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