First understand the four steps of compilation
Pre-compiler compile link
Use the following one by one
Precompiled: macro substitution, introducing document, remove blank lines, comments, in preparation for compilation
Precompiled command:
gcc -E test.c -o test.i
test.c中内容
#include <stdio.h>
int main(){
printf(“hello world!\n”);
return 0;
}
-E represents gcc stop after the end of pre-compiled, test.i is preprocessed output file
-o to specify the output file
Compile: pretreatment code compiled into assembly code.
Compile command
gcc -S test.i -o test.s
option -S let gcc compiler stops at the end of the compilation process
test.s is compiled assembly code generated
Assembly: the resulting compilation phase ".s" file into a binary object code, which is machine code (01 series)
Compilation command:
gcc -c test.s -o test.o
-C let gcc compiler stops at the end of the assembly process
test.o compiled machine code is generated after the target file
link
It is to link multiple object files and library files needed to link generation process executable object file
Links command:
gcc test.o -o test
./test
-o rename option, do not write -o, the default a.out file to generate an executable file test here
./test output after execution
The following describes the concept of static and dynamic libraries and use
Prepare the code
to create three files
tool.h tool.c main.c
in the tool.h
int find_max(int arr[],int n);
In the tool.c
#include "tool.h"
int find_max(int arr[],int n){
int max= arr[0];
int i;
for(i= 0;i<n;i++){
if(arr[i]>max){
max= arr[i];
}
}
return max;
}
In main.c,
#include <stdio.h>
#include "tool.h"
int main(){
int arr[]= {1,3,5,8,2};
int max= find_max(arr,5);
printf("max=%d\n",max);
}
Static library:
Some static library is a collection of object files ending in .o, static libraries usually end with .a, only executable files used to generate phase
In the link step, the linker take the code from the library file, copied to the executable file. This library is called a static library
Executable file a complete copy of the library code, there will be multiple redundant copies of multiple use, if there are updates, all using his program must be recompiled release
command:
Generate an object file
gcc -c test.c -o test.o
Ar command file package a static library
ar rcs libtest.a test.o
View static library contents ar t xxx.a
ar t libtest.a
text
Specifically full operation
{
gcc -c tool.c
ar rcs libtool.a tool.o
gcc -o main main.c -L. -ltool
The main.c and tool libraries connected
After -l want to connect directly with the library name
L represents the specified path to take to find the library file. Represents the current directory to find
./main
}
Dynamic library
Is not copied during the link stage into the program, the Department has the program runs
The system dynamically loaded into memory
Just load a dynamic library, different programs can get a copy of the same dynamic memory pool.
command:
gcc -c test.c
gcc -shared -fPIC -o libtest.so test.o
Specific complete code
{
gcc -c tool.c
gcc -shared -fPIC -o libtool.so tool.o
gcc -o main main.c -L. -ltool
When the static and dynamic library of the same name gcc priority to load dynamic library
Run the ./main found File not found
ldd main see which files can not be found
Configuration environment variable
LD_LIBRARY_PATH=. ./main
}
Supplementary command
View executable file depends which libraries
ldd main
View File Size
ls -lh