I have a simple code and was learning how a C program get's compiled
This code was given to make the intermediate temporary files .i, .o and .s I was able to view and sort of get the .i and .s files but couldn't view the .o file. In a website "$vi video.o" was recommended but this doesn't work
My code is:
#include <stdio.h>
float div(float, float);
#define div(x, y) x / y
int main()
{
printf("%0.2f", div(10.0, 5.0));
#undef div
printf("\n%0.2f", div(10.0, 5.0));
int n = 0;
while(n < 100)
{
printf("%d", n);
n = n 10;
}
return 0;
}
float div(float x, float y) { return y / x; }
An answer in Stack Overflow had recommended using hexdump but that only works for Linux while I have a Windows laptop
There are already questions on Stack Overflow but they are mostly explaing what the .o file is and I couldn't find any answer recommending how to open and view this file in Windows
CodePudding user response:
In a website "$vi video.o" was recommended but this doesn't work
"vi" is an editor existing in POSIX environments (like Linux, or Cygwin, perhaps even your installation since you are working with GCC). "vim" is an even better editor also available in those environments.
However, your command does not work for a different reason: That $ in the command is part of a shell prompt (like your "PS D:\C>"), and not part of the command.
The result is that your PowerShell didn't understand your command as meaning "start vi with video.o as argument", but something else entirely it could not make head or tails of. The correct command -- if you have vi installed -- would be
vi video.o
(Without the leading $)
But more importantly: An object file is binary. Which means that opening it in any kind of generic editor (like e.g. notepad) will not tell you anything of value. It would be like opening a JPEG or MP4 file in a text editor: Gibberish. Object files are input for linkers (or the linker step of the compiler). There are tools to inspect your object files -- like objdump, for example -- but unless you know what you are looking for specifically, that will not tell you anything useful either.
If you have a specific question about your object file -- like, "what symbols are defined by it", "what strings are contained in it", "what format is it in" etc. -- post a specific question mentioning you are working in a GCC-based environment, and we can give specific answers.