1. What is the target file?
- Let’s start with Hello World
“Hello World” is a must for every programmer. In Linux development, we compile the code and just use the following command to print the Hello World;
$gcc hello.c
$./a.out
But in fact, the above process can be broken down into four steps: preprocessing, compilation, assembly, and linking.
$gcc --help -E preprocess only; do not compile, assemble or link -S compile only, do not assemble or link -c compile andn assemble, but do not link
The above 4 steps but more description, this is a lot of information on the Internet
- What is assembly
What assembly does is the assembler actually converts assembly code into machine code
- Object file definition
After pretreatment, compilation, assembly process generated by the file is called the object file; In a broad sense, an executable can also be considered an object file because the format of the executable is basically the same as the object file.
2. Classification of target files
ELF file type | instructions | The instance |
---|---|---|
Relocatable files | These files contain code and data segments that can be used to link into executable files or to share object files | .o/.a on Linux and.obj on Windows |
Executable file | Can be executed directly, which is represented by an ELF executable file | .exe on Windows, a.out on Linux |
Shared object file | Contains code and data and can be used in the following two situations: 1. The linker can use this file to link with other relocatable files to produce new object files; 2. 2. The dynamic linker can combine shared object files with other executable files to run as part of the process | So under Linxu and.dll under Windows |
3. Use tools to analyze target files
-
Readelf tools
- display information about the contents of ELF format file
- -s option, display the sections’ header, (various information about the target file is stored in sections, such as.text,.data,.bss, etc.)
- The -h option displays information about the ELF file’s header file
-
Objdump tools
- display information from object
- The -D option disassembles all sections
-
Nm tools
- list symbols in files
4. Interface of links — Symbol
The essence of link is to glue multiple different object files together. In the link, functions and variables are collectively called symbols, and function names or variable names are called symbol names.
-
C and C ++ compatibility
- C ++ supports function overloading, but C does not. The key underlying reason is that the compiler handles the two differently. C will change the sign of a function to _. C ++ supports overloading because its function parameters can be changed and compile without errors
- Once you understand why, you’ll see that C/C ++ hybrid programming just needs to use C’s compiler to handle the required parts, plus extern c
code example:
//hello.c void hello(void) { printf("hello! \n"); } //hello.h #ifdef __cplusplus extern "C" { #endif void hello(void); #ifdef __cplusplus } #endif //main.cpp #include "hello.h" int main(void) { hello(); return 0; } $gcc -c hello.c $nm hello.o ..... -c main.cpp $nm main.o = _hello $g++ -c main.cpp $nm main.o = _hello $g++ -c main.cpp $nm main.o = _hello $g++ -c main.cpp $nm main.o = _hello