Compilation steps in detail?

This is a discussion on Compilation steps in detail? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by hardi What is the extension of the preprocessed file? If I type g++ -E main.cpp, then it ...

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    Quote Originally Posted by hardi
    What is the extension of the preprocessed file? If I type g++ -E main.cpp, then it writes the preprocessed file into the output, but I see no output when the -E is done in the background (when I only use g++ main.cpp), thus, it should write it to a file.
    Actually, a preprocessed file is not necessarily produced by the compiler. It could be written to a file, but there is no technical requirement to do so. In practice, the communication between phases of the compiler occurs via temporary files (eg files that are placed in a directory like /tmp) with a pseudorandomly generated name. There is also a command line option -pipe which causes communication between compilation phases to occur via pipes rather than file temporary files; this is not usually used as it requires more machine resources (and also relies on having an assembler program that can receive data via pipes: not all assembler programs can do that).
    Quote Originally Posted by hardi
    then it compiles the preprocessed file into assembler code(this is human readable, right?).
    getting only the assembler code should be g++ -S main.cpp and it will produce main.s. Correct?
    Technically, an assembler code is human readable. Whether the human will want to read it is another matter. Without the -S option, there will not necessarily be a main.S file produced: the communication between compiler and assembler will happen via temporary files.
    Quote Originally Posted by hardi
    Then, the file is assembled - that means, the assembler code from the main.s file will be translated into pure machine code, that is not human-readable anymore and the result is written into main.o
    This step depends on the machine and the implementation. For example, some gnu compilers targeting MS-DOS (eg djgpp) target a 32 bit environment that runs on top of the MS-DOS.
    Quote Originally Posted by hardi
    linker takes main.o and starts looking for the #includes and stuff(#include is somehow marked in the .o file), to link all the necessary files toghether.
    This is incorrect. Preprocessor actions (#include, macro expansion, etc) occur in the preprocessor, which occurs (logically) before compilation. The linker accepts object files and libraries, and produces an executable. The linker does not see preprocessed codes.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by grumpy
    This is incorrect. Preprocessor actions (#include, macro expansion, etc) occur in the preprocessor, which occurs (logically) before compilation. The linker accepts object files and libraries, and produces an executable. The linker does not see preprocessed codes.
    .o files are really small - comparing to the resulting executable. There is no way that the iostream library is in the .o file(with all its subincluded files). So, where does the iostream go? When is it made into machine code?

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    g++ -v main.cpp

    It shows you all the steps which happen.

    Compare with
    g++ -v -c main.cpp

    Guess you still didn't do this.


    Here's the difference
    Code:
     /usr/lib/gcc/i686-pc-cygwin/3.4.4/collect2.exe -Bdynamic --dll-search-prefix=cyg
     /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../crt0.o -L/usr/lib/gcc/i686-pc-cygwin/3.4.4
     -L/usr/lib/gcc/i686-pc-cygwin/3.4.4 -L/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../.. 
     /Temp/cctG7FO6.o -lstdc++ -lgcc -lcygwin -luser32 -lkernel32 -ladvapi32 -lshell32 -lgcc
    All the stuff which isn't in your .o file comes from the system object and library files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by Salem
    Guess you still didn't do this.
    I did it when you told me to, but at that time I didn't know what to look for, thus it was useless. But ty for pointing that out now

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 08:50 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. MS VC++ Crash on compilation
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-23-2003, 07:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21