An executable file for dynamic GCC compilation (shared library) must be accompanied by a dynamic link library. During execution, you must call the commands in the corresponding dynamic link library.
Advantages: small size, fast Compilation
Disadvantage: high dependence
The Code is as follows:
[[Email protected] shared] # Cat Add. c
Int add (int x, int y ){
Return X + Y;
}
Parsed in 0.007 seconds at 12.13 kb/s
Add. c summation Function
The Code is as follows:
[[Email protected] shared] # Cat print. c
# Include <stdio. h>
Void print (int x ){
Printf ("% d \ n", X );
}
Parsed in 0.007 seconds at 14.78 kb/s
Print function
The Code is as follows:
[[Email protected] shared] # Cat head. h
# Ifndf head_h
# Define head_h
Int add (INT, INT );
Void print (INT );
# Endif
Parsed in 0.007 seconds at 16.34 kb/s
Head. h declares the header file
The Code is as follows:
[[Email protected] shared] # Cat main. c
# Include <stdio. h>
# Include "head. H"
Int main (){
Int x = add (3, 5 );
Print (X );
}
Parsed in 0.007 seconds at 19.70 kb/s
Main. C Main Function
1. Compile the dynamic library
[[Email protected] shared] # gcc-FPIC-shared Add. c print. C-o libd. So
Use the-FPIC-shared parameter to generate the libd. So dynamic library
2. generate an execution File
[[Email protected] shared] # GCC main. c libd. So-O main
Load the dynamic library and generate the main execution File
3. Dynamic Library Loading
[[Email protected] shared] #./main
./Main: Error while loading shared libraries: libd. So: cannot open shared object file: no such file or directory
Because we use a dynamic library for compilation, the execution fails because the libd. So library cannot be found during main execution. You can use LDD main to check whether libd. So is found.
The Code is as follows:
[[Email protected] shared] # LDD main
Linux-gate.so.1 => (0x0070c000)
Libd. So => not found
Libc. so.6 =>/lib/i686/nosegneg/libc. so.6 (0x0050e000)
/Lib/ld-linux.so.2 (0x00ea6000)
Parsed in 0.000 seconds at 434.42 kb/s
There are three solutions to this problem,
1. Add the dynamic library path to the Environment Variable
[[Email protected] shared] # export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/root/C/shared/
2. Add the dynamic library path to lD. So. conf.
[[Email protected] shared] # echo "/root/C/shared">/etc/lD. So. conf
[[Email protected] shared] # ldconfig
Search all dynamic databases again and update them to/etc/lD. So. cache.
[[Email protected] shared] # ldconfig-v | grep libd. So
Check whether the libd. So library is currently included.
3. Directly copy to the system directory
[[Email protected] shared] # cp libd. So/lib/
GCC Parameters
-Shared:
This option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
-FPIC:
Indicates that the compiled code is a code with independent locations. Without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.
dynamic library path for GCC dynamic compilation