g++ -c xyz.cpp
creates an object file xyz.o
But when I open it in vim editor it shows some crazy text...
How can I see the machine code?
What package can use to open and see that machine code?
Check out the binutils
package.
In it you'll find objdump - display information from object files.
Then try running objdump -d xyz.o
.
Machine code generated by your compiler is not supposed to be human readable, but rather completely optimized to run as fast as possible on your processor.
However, you can read symbols from the binary files like this:
$ nm main.o | c++filt
U _Unwind_Resume
U FactoryImpl::FactoryImpl()
U UI::UI(Subject*)
U operator delete(void*)
U operator new(unsigned long)
U __gxx_personality_v0
0000000000000000 T main
Notes:
nm
and c++filt
are from the binutils
package you probably have already when developing.I just realized my answer wasn't really answering your question about reading the actual machine code. However, I think it provides some useful information instead. For reading the assembly-level operations for your processor, use objdump -d
as proposed by @hnasarat
main.o
file with the given command. Try to run the same on your .o
file.
Sep 17, 2012 at 20:39
You should use the -S
flag to make the compiler output assembly code.
So for your example the code would be g++ -S xyz.cpp
and the assembly would be printed in xyz.s
.
-O0
is (usually) the default optimization level so i felt safe in leaving it out.