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

How to build gitlab on centos6

Preface The original project was placed on the pu...

Detailed explanation of Tomcat directory structure

Table of contents Directory Structure bin directo...

Detailed explanation of MySQL master-slave replication and read-write separation

Article mind map Why use master-slave replication...

Briefly describe how to install Tomcat image and deploy web project in Docker

1. Install Tomcat 1. Find the tomcat image on Doc...

Summary of vue's webpack -v error solution

Xiaobai learned about Vue, then learned about web...

Why should you be careful with Nginx's add_header directive?

Preface As we all know, the nginx configuration f...

Solution to the problem of invalid line-height setting in CSS

About the invalid line-height setting in CSS Let&...

Detailed explanation of CocosCreator project structure mechanism

Table of contents 1. Project folder structure 1. ...

How to use boost.python to call c++ dynamic library in linux

Preface Recently I started using robot framework ...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

Detailed explanation of nginx anti-hotlink and anti-crawler configuration

Create a new configuration file (for example, go ...

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...

How to View All Running Processes in Linux

You can use the ps command. It can display releva...