-3

The compilation process is distributed in 4 stages:

  1. Preprocessor -Expanding Macros and header files.
  2. Compiler -Converts source code to assembly language
  3. Assembler -Converts assembly code to machine code
  4. 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:

  1. cpp test.cpp>test.i [The output is .i file, an expansion of header file and macros]
  2. g++ -S test.i [The output is .s file, the assembly language file]
  3. as -o test.o test.s [The output is .o file,the machine dependent machine code]
  4. 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 tostd::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.

Wasim
  • 29
  • 1
  • 10
  • 1
    You've been a member for 11 months according to your profile. You should know better how to ask (and format) questions – UnholySheep Oct 24 '16 at 19:14
  • 1
    Where do you link the C++ standard library (more generally your toolchain's runtime)? Why are you invoking `ld` directly? What is your goal? – Lightness Races in Orbit Oct 24 '16 at 19:19
  • @UnholySheep: agreed! i'll rephrase. – Wasim Oct 25 '16 at 02:37
  • @LightnessRacesinOrbit: I am getting error while invoking linker (ld). The previous stages [preprocessor,compiler,assembler] are working fine. I wanted to know the complete functionality of compilation process. – Wasim Oct 25 '16 at 02:40
  • @LightnessRacesinOrbit: question,whose answer i am seeking is, "how to convert a machine code file(*.o file) to an executable file(*.exe)?" – Wasim Oct 25 '16 at 13:33
  • @Vickey: Answer my questions. – Lightness Races in Orbit Oct 25 '16 at 14:10
  • @LightnessRacesinOrbit: Apologies now i really understood your question! I have no idea where to link C++ standard library. Even i do not know what is path of C++ standard library in my MinGW folder. I guess i am not invoking ld directly, i am invoking ld after preprocessing,compiling and assembling. So my question is how can i link my machine code (.o file) to c++ standard library to create an executable. Thanks! – Wasim Oct 25 '16 at 17:29
  • Get GCC to do it for you. :) I have no idea why you're trying to make this hard for yourself, and you have not given a satisfying reason why that would be. – Lightness Races in Orbit Oct 25 '16 at 19:09
  • @LightnessRacesinOrbit: i figured out that linker requires minimum -lstdc++ -lmingw32 -lgcc -lmingwex -lmsvcrt -lkernel32 libraries along with crt2.o – Wasim Oct 26 '16 at 19:52
  • @LightnessRacesinOrbit : One question if you could help! how an executable (.exe file) is loaded into RAM which is then treated as an process and gets executed. – Wasim Oct 26 '16 at 19:55
  • @Vickey: That's basically "how do computers work" - far too broad to answer in a comment. Whole books have been written on the topic. You should get one! – Lightness Races in Orbit Oct 26 '16 at 20:45
  • @LightnessRacesinOrbit: Ya the information is way beyond to fit in a comment:) Can you please suggest some good book from which i can understand the internal implementation of compiler,assembler,linker and loader of GNU. – Wasim Oct 27 '16 at 03:16

1 Answers1

1

Link with gcc instead of ld:

gcc -o test.exe test.o
  • ya that is the short way, i wanted to know underlying working of compilation process which involves preprocessor(cpp),compiler(gcc),assembler(as)and linker(ld).Now ld is the place where i am getting stuck! i.e. while using ld command i am getting error! – Wasim Oct 25 '16 at 02:36
  • You incorrectly guess what the `ld` command line should be. Link with `g++ -o test.exe test.o -Wl,-v` to see the actual `ld` command line. – Vincent Rivière Oct 25 '16 at 12:19
  • Riviere: compiling the program in verbose mode (-v) gives lots of information. How can i make out that what are libraries required when i want to create a executable through invoking linker (ld) rather then general g++ command. – Wasim Oct 25 '16 at 17:52
  • @Vickey: Did you try the exact command line I gave you, with `-Wl,-v`? It precisely tells you what you want to know for your system, no more, no less. – Vincent Rivière Oct 25 '16 at 19:13
  • Riviere: Thanks a lot, by using the option you told i could figure out what libraries are required by the linker to create an executable – Wasim Oct 26 '16 at 19:49