Compile and link summary in c++

1 The compilation and linking process is divided into preprocessing ---> compilation ----> assembly ----> linking . As shown below

2 What does the preprocessing do?

(1) Delete all #defines and expand all macros

(2) Process all conditional precompiled directives such as #if #ifdef

(3) Process the #Incldue precompile directive, insert the included file into the precompiled file. The purpose of using the header file is that it can be used by multiple different cpp source programs. If you define it yourself, you usually use "", and if you include library files, it is usually "<>".

(4) Filter all comment symbols

(5) Add line numbers and file identifiers. It is convenient for the compiler to generate line number information for debugging, etc.

(6) All #pragma compiler directives are preserved.

3 Compile

(1) Perform a series of lexical analysis, syntax analysis, etc. on the preprocessed files to generate assembly code files.

g++ -S hello.i -o hello.s You can view the assembly code by opening helloword.s.

4 links

(1) Compile and assemble the linked modules independently, this is the link

(2) The most basic static linking process is shown in the following figure

(3) The library is a package of a set of object files, and the code is compiled into object files and then packaged and stored.

(4) Static linking

The link to the function library is statically linked at compile time. Object files and related function libraries are combined into an executable file. Usually libxxx.a

example:

code show as below

 1 //main.cpp
 2 
 3 #include "add.h"
 4 #include "sub.h"
 5 #include "iostream"
 6 using namespace std;
 7 //演示静态链接
 8 int main(){
 9     cout<<"1+2="<<add(1,2)<<endl;
10     cout<<"1-2="<<sub(1,2)<<endl;
11     return 0;
12 }
View Code
1 #include "sub.h" 
2 int sub(int a,int b){ 
3  return a-b; 
4 }
View Code
1 //sub.h
2 #ifndef _SUB_H_
3 #define _SUB_H_
4 int sub(int a, int b);
5 #endif 
View Code
1 //add.cpp
2 #include "add.h"
3 int add(int a, int b){
4     return a+b;
5 }
View Code
1 //add.h
2 #ifndef _ADD_H_
3 #define _ADD_H_
4 int add(int a, int b);
5 #endif
View Code

(4-1 ) First compile cpp into .o file

g++ -c add.cpp

g++ -c sub.cpp Note that both dynamic and static linking are done by .o file ships

(4-2) .o---->.a file ar cr libmymath.a sub.o add.o This will generate the libmymath.c file as shown below

Note: The library file specification generally starts with lib and then the static library name. a suffix name

r: insert module in library

c: create a library

(4-3) Use static library

g++ -o main main.cpp -L -lmymath generates the main file like this

(5) Dynamic link

(5-1) Static linking is at compile time, so dynamic can be deferred to run time. extension .so

(5-2) Generation method

g++ -fPIC -shared -o libmymath.so add.cpp sub.cpp

Compile parameter parsing:

-fPIC: compile as position independent code

-Lpath:path directory search library file -L is the current directory

-Ipath: Indicates the search file in the path directory

-ltest: There is an implicit naming rule when the compiler looks for a dynamic link library, and the name is preceded by a lib suffix .so

Note : When running g++ -o main main.cpp -L. -lmymath link is normal but execution will fail. Mainly the .so is not found. because the program runs

The corresponding dynamic library files will be found in /usr/lib and /lib. Workaround: Copy libmymath.so to the directory /user/lib

(6) The respective characteristics of static linking and dynamic linking

6-1: When the program is running, the operating system will check whether there is a copy of the library function in the memory. If there is, it will not be linked and loaded, so as to save memory resources. The static library is different, each program will copy this library function into its own code segment, which takes up memory resources.

6-2 Use a static library. If the library has changed, the library used needs to be recompiled. The dynamic library will not, the provided interface will not change, just replace it with the newly generated dynamic library and it will be ok

 

Come on -----> The day is almost over. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324898378&siteId=291194637