Compile link, static library, dynamic library, gdb

Posted by jestercobblepot on Fri, 24 Dec 2021 02:03:28 +0100

Vim

vim filename		When on, it is command mode.	// filename is opened when it exists, and created and opened when it does not exist

gg Is to go directly to the top of the file content,
shift + g Directly to the bottom of the file.
dd  Delete the line where the cursor is located
3dd Delete the line where the cursor is located and the following 2 lines(A total of three lines were deleted)
5G Move the cursor to the fifth line
u  revoke
yy	Copy cursor line
3yy	Copy the line where the cursor is located and the following 2 lines(A total of three lines are copied)
p	Paste below the line where the cursor is located
P	Paste above the line of the cursor
\ Find
:set nu 		 	Yes display line number
:set ts=4		 	Is set tab The key is 4
# You can also write directly to the bottom of sudo vim /etc/vim/vimrc 
# set ts=4 
# set nu

knock a Enter write mode, After writing in writing mode, Esc Switch to command mode, then : wq Yes, save and exit vim ;   q!Yes exit without saving;   

Install gcc

yum install gcc
# gcc test.c -o test / / generate an executable file in the current directory / / the - o parameter specifies that the file name of the generated executable file is test, and the default is a.out 
# ./ test 						//   Execute the generated executable file test

Installation g++

yum install gcc-c++

The following command can save the temporary files left during the compilation process

g++ test.cpp -o test --save-temps
# ls -l
total 684
-rwx------ 1 root root  17248 Nov 28 17:04 test
-rw------- 1 root root    228 Nov 28 16:39 test.cpp
-rw------- 1 root root 664410 Nov 28 17:04 test.ii				// You can see i file is much larger (664410 bytes)
-rw------- 1 root root   2664 Nov 28 17:04 test.o
-rw------- 1 root root   2220 Nov 28 17:04 test.s

In the offline state, it needs to be installed in the form of rpm package
For Ubuntu:

sudo apt update
sudo apt install build-essential

gcc --version
gdb --version
g++ --version

sudo apt install cmake
cmake --version

Basic use

gcc commands are called according to different parameters: precompiler (preprocessing and compiling) cc1, assembler as, linker ld

Pretreatment: g++ -E test.cpp 			// E should be capitalized, preprocessed, and the results are directly output to the terminal.

g++ -s test.cpp				// An a.out executable file is generated by default
 compile:   g++ -S test.cpp				// By default, a test is generated S assembly language file
 assembly:   g++ -c test.s/test.cpp		// The default is to generate the machine language test O documentation 		 Equivalent to as test s
 link:	g++ test.o					// Generate a.out executable
-g Generation can be gdb Debug information used
-O2 Optimize the source code,Make execution more efficient
-l Link library file,Followed by the library name
-L If the library you want to link is not in(/lib,/usr/lib,/usr/local/lib)In these three directories, You need to specify the directory where the library is located

link mytest library, Where is this library/home/bing/mytestlibfolder Directory
g++ -L/home/bing/mytestlibfolder -lmytest test.cpp

-I If the header file used is not in /usr/include Directory,Just use it -I To specify
g++ -I/myinclude test.cpp		// The header file used is placed in the / myinclude directory

-Wall					// Print warning message
g++ -Wall test.cpp

-std=c++11		Set compilation criteria

nm command, check and analyze the target file, library file and executable file, and analyze the symbol table

Generate static and dynamic libraries

For a custom function io:
Undeclared, undefined, direct use:

error: 'io' was not declared in this scope

Declared but not defined:

undefined reference to `io(char*)'				// Reference without definition, illegal

Declare and define the implementation, and the cpp file defining the implementation is compiled together with the main file:

g++ main.cpp hanshu.cpp -o ma
./ma
 Implemented correctly

Compile it into a target file, and then link it together when linking: (this avoids the compilation process of hanshu.cpp)

g++ hanshu.o main.cpp -o ma
./ma
 The correct results are also obtained

If there are more than one similar to Hanshu O files are commonly used. If you press the above, you need to add parameters to each.
At this point, you can use the ar tool to package it into a library. A static library is a collection of target files.

ar [-dmpqrtx][cfosSuvV][a<Member file>][b<Member file>][i<Member file>][Library file][Member file]

Required parameter:
d:Delete the first mock exam in the library
t:List the modules included in the stock out
r:Insert module into Library,If there is a library with the same name,Replacement occurs
q:Insert module into Library,But do not check whether replacement is required
x:Extract the module from the library (unzip)
$ ar -t libhanshu.a
hanshu.o						// View included modules
$ ar -q libhanshu.a hanshu.o
$ ar -t libhanshu.a
hanshu.o						// You can see that -q no replacement occurs
hanshu.o			
$ ar -d libhanshu.a hanshu.o		// Delete a
g++ main.cpp -lhanshu -L./ -o main				// The linked library has also been executed correctly

The advantage is that there is no longer any dependency.
Disadvantages of static libraries:
If the static library is linked by multiple programs, and the static library is bound with the program, there will be multiple copies in memory. In particular, a considerable number of other library functions in the static library are not used, which will cause a great waste of space.
Once a module in the library is updated, the whole program must be recompiled, re linked and then released to the user.

Dynamic library:
The process of linking occurs when the program is running. The dynamic library file is independent of the program using the library.
At this time, the library file is shared. For multiple programs that use the file, there will only be one copy of the library file in memory.
When you want to upgrade a module shared by a program, you only need to overwrite the old target file without reconnecting all programs.

Generate dynamic library:

g++ hanshu.cpp -fPIC -shared -o libhanshu.so
// -Shared solves the relocation of shared objects during loading (the modifiable data part in the dynamic link library has multiple copies for different processes)
// The instruction part in the fPIC generated address independent code module will not change due to the change of loading address during loading

Link to compile:

$ g++ main.cpp -o main ./libhanshu.so
$ ./main
 perhaps
$ g++ main.cpp -o main -lhanshu -L./     // At this time, the default is the linked dynamic library. If there is a duplicate name
$ ./main		
At this time, an error will be reported and the library file cannot be found,So you need to specify a path
$ LD_LIBRARY_PATH=./ ./main			// So it can run normally

$ ldd main // View dynamic library dependencies for executables
		linux-vdso.so.1 (0x00007ffe15bfd000)
        ./libhanshu.so (0x00007f408442a000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f40840a1000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4083cb0000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4083912000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f408482e000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f40836fa000)

When the static library and dynamic library have the same name, the dynamic library takes precedence.
Specify a static library:

g++ main.cpp -o main -static -lhanshu -L./
$ ll main main2
 	8664 12 June 13-17:53 main
2251400 12 June 13-18:14 main2		// You can see that the executable generated through static links is much larger, because many static libraries are bound, but the advantage is that there is no dependency

gdb debugging

g++ tiaoshi.cpp -g -o tiaoshi
gdb tiaoshi

break 13			# Set a breakpoint on line 13
info breakpoints	# View all breakpoint information
delete 2			# Delete breakpoint with num 2

run					# Start the program and run it to the breakpoint
continue			# Continue running until the program ends or the next breakpoint is encountered
next				# Next, do not enter the function
step				# Next, go inside the function
 enter				# Execute the previous command

list Line number			# View the source code near this line number
print variable			# View the value of the variable
ptype variable			# View the type of variable
display variable		# You can see the value of the variable at each step of the program. When you don't want to see it, undo num

quit				# Exit gdb

Installing gcc and g under Linux++
Compiling cpp project with g + + under linux
How to use Tail command in Linux
Hybrid connection of linux dynamic library and static library
The difference between header file and library file
C + + static library and dynamic library
linux -- nm command
nm: file format analysis
nm command details
Powerful file analysis tool under linux - nm
gdb common commands
list usage in gdb

Topics: Linux vim compiler