Use of Linux dynamic link library

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link libraries do not have a main function, but are the implementation of a series of functions. Generate so dynamic link library files through shared and fPIC compilation parameters. When the program calls a library function, it only needs to connect to this library. For example, the following implements a simple integer arithmetic transport dynamic link library, defines two files, calculate.h and calculate.c, and produces the libcac.so dynamic link library.

Useful commands for binary files

Check the file type

file

See which libraries a binary is linked to

ldd

View the symbols contained in the binary file, T means loading, U means undefined symbol

nm

Read information from binary files

readelf -a smu.o

Convert binary files to assembly

objdump -d sum.o

Generation of dynamic link libraries

sum.c

#include <stdio.h>
#include <stdlib.h>

int sum(int x){
 int i, result=0;
 for(i=0; i<=x; i++){
  result+=i;
  }
 if(x > 100)
  exit(-1);
 return result;
};

void display(char* msg){
 printf("%s\n",msg);
}
 
int add(float a,float b){
 return a+b;
}

int sum_array(int array[], int num){
 int i = 0, sum = 0; 
 for(i=0; i<num; ++i) 
  sum += array[i];
 return sum;
}

void modify_array(int array[], int num){
 int i = 0, sum = 0; 
 for(i=0; i<num; ++i) 
  array[i] *= 10;
}

main.c

#include <stdio.h>
#include <stdlib.h>

int main(void){
 int x;
 printf("Input an integer:\n");
 scanf("%d", &x);
 printf("sum=%d\n", sum(x));
 return 0;
};

Generate executable file

gcc -c main.c -o main.o
gcc -c sum.c -o sum.o
gcc sum.o main.o

The main executable file will be generated

file main // ELF 64-bit LSB executable
file sum.o // ELF 64-bit LSB relocatable

Because sum.c contains reusable functions, I want to compile sum.c into a dynamic link library

gcc sum.o -shared -o sum.so

An error occurred, prompting

/usr/bin/ld: sum.o: relocation R_X86_64_PC32 against undefined symbol `exit@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

This means that not all .o files can be compiled into dynamic link libraries. You need to add the parameter -fPIC when generating the .o file.

gcc -c sum.c -fPIC -o sum.o
gcc sum.o -o shared sum.so

Generally, the compilation command for a shared library is (previous experiment)

Dynamic Link Library

gcc -shared -fPIC -o libmyhello.so hello.o
gcc -o hello main.c -L. -lmyhello

Static link

ar rcs libxx.a xx.o 
g++ -o main main.cpp -static -L. -lxx

At this time

g++ -o main main.c sum.so
./main 

Sometimes it will report an error

error while loading shared libraries: sum.so: cannot open shared object file: No such file or directory

ldd main
output:
sum.so => ​​not found

At this time, you need

export $LD_LIBRARY_PATH=pwd:$LD_LIBRARY_PATH

Note: -fPIC is used when generating .o, and -shared is used to generate dynamic link libraries

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A detailed introduction to Linux hard links and soft links
  • Detailed explanation of Linux soft links and hard links
  • What are Linux soft links and Linux hard links
  • Linux kernel device driver kernel linked list usage notes
  • Problems with dynamic link library loading path and search path under Linux
  • Python tutorial on calling dynamic link libraries in Windows and Linux
  • Detailed explanation of Linux link compilation
  • Analysis of the principles and usage of Linux hard links and soft links

<<:  Windows system mysql5.7.18 installation graphic tutorial

>>:  Summary of some efficient magic operators in JS

Recommend

Example code for implementing hollowing effect with CSS

Effect principle Mainly use CSS gradient to achie...

Detailed explanation of MySQL database triggers

Table of contents 1 Introduction 2 Trigger Introd...

Teach you how to use AWS server resources for free

AWS - Amazon's cloud computing service platfo...

Pure CSS to achieve the effect of picture blinds display example

First, let me show you the finished effect Main i...

Analysis of several situations where MySQL index fails

1. Best left prefix principle - If multiple colum...

An example of how to write a big sun weather icon in pure CSS

Effect The effect diagram is as follows Implement...

JS realizes the scrolling effect of announcement online

This article shares the specific code of JS to ac...

Detailed explanation of group by and having in MySQL

The GROUP BY syntax can group and count the query...

Learn the basics of nginx

Table of contents 1. What is nginx? 2. What can n...

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...

How to set a fixed IP in Linux (tested and effective)

First, open the virtual machine Open xshell5 to c...

Detailed explanation of MySQL data rows and row overflow mechanism

1. What are the formats of lines? You can see you...

Detailed explanation of Linux text processing command sort

sort Sort the contents of a text file Usage: sort...

Detailed explanation of nginx forward proxy and reverse proxy

Table of contents Forward Proxy nginx reverse pro...

Detailed explanation of CSS margin collapsing

Previous This is a classic old question. Since a ...