g ++ basic usage and how to generate static link library and dynamic link library

 

Reference: https://blog.csdn.net/woshinia/article/details/11060797?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

.cpp .c-> preprocessing-> compiled into assembly language-> assembled into object file-> connected into executable file

g++ :

-E: Only do preprocessing and get .i file

-S: Compile, and the resulting file is an assembly language file.s

-c: Assemble into the target file. O

-L: connect

-o: add the output file name after the end (-o can be interspersed at any position in the g ++ instruction, in front of the file, behind, or between the two files)

Without parameters , it is to complete all the above processes (preprocessing, compilation, assembly, connection), if you need to connect, you can follow multiple files. Finally, an executable file is generated. Generally use -o to specify the file name, if not specified, an .out file with the same name will be generated

Pay attention to capitalization

1. Preprocess, generate .i file [preprocessor cpp]

Command: g ++ -E Test.cpp If you want to save the preprocessed file to a file, you can add> Test.i (output redirection) or -o Test.i

Function: output the preprocessed file, with .i as the suffix under linux. Only preprocessing is activated, this does not generate a file, you need to redirect it to an output file. This step mainly does these things: the replacement of macros, as well as the elimination of comments, and find the relevant library files. Open Test.i with the editor and you will find that there are a lot of codes. You only need to look at the last part to find that the preprocessing has replaced the macros and the comments are eliminated, which can be understood as the removal of irrelevant code. The following is the last part of the Test.i file, you can see the replacement of macros and the elimination of comments.

 

2. Convert the pre-processed file into assembly language and generate the file .s [compiler egcs]

Command: g ++ -S Test.cpp (-o Test.s)

Function: A Test.s file will be generated. The .s file indicates that it is an assembly file, and it is an assembly instruction when opened with an editor.

 

3. Have the assembly turned into the target code (machine code) to generate .o files [assembler as]

Command: g ++ -c Test.cpp (-o Test.o)

Function: .o is the object file generated by GCC. Unless you are doing compiler and linker debugging and development, opening this .o has no meaning. Binary machine code can't be read by most people.

 

4. Connect the target code to generate an executable program [linker ld]

命令:g++ Test.o -L F:\vs2008\VC\include\iostream

Function: Integrate the .o file with the required library file to form an .exe file. This is the executable file. -L indicates a link, here is the absolute path, which is different from each computer

 

In the above steps, you can use the -o command to output the various names you want. For example the last command, use the following output Test.exe

You can g ++ Test.o -o Test.exe -LF: \ vs2008 \ VC \ include \ iostream

 

5. Implementation

./ Test.exe

 

 

1. If there is only one file and no library file is needed, you can directly generate an executable file

g++ -o main main.cpp

2. If you need to compile the file into a library file

Both static library files and dynamic library files are created by .o files, so the first step is to compile to .o object files

The naming convention for library files starts with lib (prefix), followed by the suffix name, static link library with .a as the suffix name, dynamic link library with .so as the suffix name

(1) Dynamic link library

Example one;

g++ -fPIC -o math.o -c math.cpp

g++ -shared -o libmath.so math.o

You can also get it directly with one command:

g++ -fPIC -shared -o libmath.so math.cpp

Example two:

g++ fPIC -o add.o -c add. cpp

g++ fPIC -o sub.o -c sub.cpp

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

You can also get it directly with one command:

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

-fPIC: means to compile to position independent code. Without this option, the compiled code is position-dependent, so when dynamic loading, the code is copied to meet the needs of different processes, and the purpose of true code segment sharing cannot be achieved.

(2) Static link library

g++ -c math.o math.cpp

Use the ar tool:

av -cr libmath.a math.o

The r option of the ar command: insert a module (replace) in the library. When the inserted module name already exists in the library, the module with the same name is replaced. If one of several modules does not exist in the library, ar displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, you can use other options to change the location of the increase.

The c option of the ar command: Create a library. It will be created whether or not the library exists.

(3) Use library files

Static library: g ++ -o main main.cpp -L libmath.a In this way, the functions in the libmath.a library can be used in the main.cpp file

Dynamic library: same method

 

Published 59 original articles · Likes46 · Visits 30,000+

Guess you like

Origin blog.csdn.net/sinat_41852207/article/details/104846327