0

I am working on some (very) low level programming but not everything is completely clear to me. I start by creating a .cpp (or .c) file which is run through gcc to create an elf or object file but what is an object file? I get object files when I use the "as" compiler but how are these used and what is the purpose of having an object file when we could have a straight binary?

dalearn
  • 241
  • 6
  • 18
  • Possible duplicate of [What's an object file in C?](http://stackoverflow.com/questions/7718299/whats-an-object-file-in-c) – Pyves Sep 26 '16 at 15:13

1 Answers1

3

There is a very clear explanation of this question on the this site. I pasted it down below as well. But I strongly suggest you take a look at the diagram on the website. That will give you a much better high-level understanding of what is going on.

Compiling a source code file in C++ is a four-step process. For example, if you have a C++ source code file named prog1.cpp and you execute the compile command

g++ -Wall -ansi -o prog1 prog1.cpp the compilation process looks like this:

  1. The C++ preprocessor copies the contents of the included header files into the source code file, generates macro code, and replaces symbolic constants defined using #define with their values.
  2. The expanded source code file produced by the C++ preprocessor is compiled into the assembly language for the platform.
  3. The assembler code generated by the compiler is assembled into the object code for the platform.
  4. The object code file generated by the assembler is linked together with the object code files for any library functions used to produce an executable file.

By using appropriate compiler options, we can stop this process at any stage.

  1. To stop the process after the preprocessor step, you can use the -E option:

    g++ -E prog1.cpp The expanded source code file will be printed on standard output (the screen by default); you can redirect the output to a file if you wish. Note that the expanded source code file is often incredibly large - a 20 line source code file can easily produce an expanded file of 20,000 lines or more, depending on which header files were included.

  2. To stop the process after the compile step, you can use the -S option:

    g++ -Wall -ansi -S prog1.cpp By default, the assembler code for a source file named filename.cpp will be placed in a file named filename.s.

  3. To stop the process after the assembly step, you can use the -c option:

    g++ -Wall -ansi -c prog1.cpp By default, the assembler code for a source file named filename.cpp will be placed in a file named filename.o

  • brilliant! this exactly answered my question! – dalearn Sep 26 '16 at 15:35
  • but if it compiles into assembly, why does it require a stack in bare metal programming? – dalearn Sep 26 '16 at 15:39
  • Hmm, can you point me to a source from which you are getting this information? It'll be easier to clarify. –  Sep 26 '16 at 18:05
  • Sorry about being a little late, I had been reading [this](http://wiki.osdev.org/Raspberry_Pi_Bare_Bones) I am trying to write an operating system optimized for the raspberry pi and am running into some trouble as far as the whole "actually load the OS" part and why I can't use all features of C++ due to stack issues I assume. – dalearn Sep 27 '16 at 14:36
  • Essentially, [this](http://stackoverflow.com/questions/6870712/beagleboard-bare-metal-programming) question but for the raspberry pi is my longterm goal. – dalearn Sep 27 '16 at 14:39
  • Hmm, the information presented in those 2 links are much beyond me. I am not an expert on OS dev. However, if I were to take a crack at your initial question, i'd assume that because an OS, with all its mechanics, is already in place, it has the knowledge to actually convert the code into assembly. While, if you were just running a code without an OS, then you need some way of mimicking a real OS and so, you might need to store each instruction in a stack and call them one by one. But you can definitely ask another question specifically about what you told me, and see if others can help you. –  Sep 27 '16 at 16:31
  • I tried but it got severely downvoted and I got no productive answers... :( – dalearn Sep 27 '16 at 16:49