Gcc shared object, gccshared
An example of generating a dynamic link library *. so is introduced: first create a new 1 Head file test. h:
#include <stdio.h>void first();void second();void third();
Create three source files first. c/second. c/third. c:
First. c:
#include "test.h"void first() { printf("this is first.\n");}
Second. c:
#include "test.h"void second() { printf("this is second.\n");}
Third. c:
#include "test.h"void third() { printf("this is third.\n");}
Then, generate the dynamic link library libtest. so: gcc first. c second. c third. c-fPIC-shared-o libtest. so
Note: 1.-fPIC indicates that the code location is independent for program sharing. 2.-shared means to generate a dynamic link library
Next, create a source code test. c that calls the Dynamic Link Library:
#include "test.h"void main(){ first(); second(); third();}
Generate the executable file: gcc test. c-L.-ltest-o test
Note:-L. indicates that the link library is in the current directory;-ltest indicates that the link library libtest. so is found based on implicit rules.
Execute./test to obtain the following results:
this is first.this is second.this is third.
If you don't get this result, you will be prompted ". /test: error while loading shared libraries: libtest. so: cannot open shared object file: No such file or directory ", then in/etc/ld. so. conf. create a test. conf configuration file, and write the access path of your dynamic link library in this file, such as/usr/local/demo/lib, depending on your own situation. After the configuration is completed, type sudo ldconfig to make the configuration take effect.
Over.
Ps:
You can also create a soft link, for example, ln-s/your_folder/* so/usr/lib.