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
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
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:
|
<<: Windows system mysql5.7.18 installation graphic tutorial
>>: Summary of some efficient magic operators in JS
Effect principle Mainly use CSS gradient to achie...
Table of contents 1 Introduction 2 Trigger Introd...
AWS - Amazon's cloud computing service platfo...
First, let me show you the finished effect Main i...
1. Best left prefix principle - If multiple colum...
Effect The effect diagram is as follows Implement...
This article shares the specific code of JS to ac...
The GROUP BY syntax can group and count the query...
Table of contents 1. What is nginx? 2. What can n...
illustrate DML (Data Manipulation Language) refer...
First, open the virtual machine Open xshell5 to c...
1. What are the formats of lines? You can see you...
sort Sort the contents of a text file Usage: sort...
Table of contents Forward Proxy nginx reverse pro...
Previous This is a classic old question. Since a ...