The compilation process is distributed in 4 stages:
- Preprocessor -Expanding Macros and header files.
- Compiler -Converts source code to assembly language
- Assembler -Converts assembly code to machine code
- Linker -links the machine code to form single executable.
Assuming we have source code named test.cpp that we need to compile so the commands required will be:
- cpp test.cpp>test.i [The output is .i file, an expansion of header file and macros]
- g++ -S test.i [The output is .s file, the assembly language file]
- as -o test.o test.s [The output is .o file,the machine dependent machine code]
- ld -o test.exe test.o [the output is .exe file,an executable which can be run directly by operating system]
So the problem is at the last step i am getting following error:
test.o:test.cpp:(.text+0x32): undefined reference to __mingw_vprintf'
test.o:test.cpp:(.text+0x4a): undefined reference to
__main'
test.o:test.cpp:(.text+0x75): undefined reference to std::ios_base::Init::~Init()'
test.o:test.cpp:(.text+0xa5): undefined reference to
std::ios_base::Init::Init()'
test.o:test.cpp:(.text+0xb1): undefined reference to atexit'
ld: test.o: bad reloc address 0x0 in section
.pdata'
ld: final link failed: Invalid operation
Note: Yes we can avoid these steps by just using "g++ test.cpp" to get executable a.exe but intention is to understand the each step of build process.