How to read a .obj file?

13,376

Solution 1

Use the DUMPBIN tool from Visual Studio command prompt. Specifically, the /DISASM option shows you the disassembly. Do note the if you have link-time optimization enabled, then at least theoretically the final code may change after the .obj files are linked to form the final binary (.exe or .dll). You can disassemble those with DUMPBIN as well.

Solution 2

Dumpbin at the /all setting shows code sections & directories. Detailed explanations of things like code section characteristics is not common knowledge, but may be found in certain books. Here's a precis on COFF, here's a more detailed description, or there's even a WYSINWYX thesis.

Adding to @cynic's answer, in Visual Studio (15 & 17) it's possible to run DumpBin via the menu. Go to Tools/External Tools and click Add. Put in the Title box something like DumpBin Your_Exe or Your_Obj name and find the path for DumpBin if it isn't in the environment settings. It can be similar to the following:

c:\program files (x86)\microsoft visual studio\2017\community\VC\Tools\MSVC\SDK-Version\bin\Host $(Platform)\$(Platform)\dumpbin.exe

and add it to the Command box. For arguments try something like:

/ALL /OUT:C:\Users\New\Desktop\dumpofYour_ExeName.txt "Pathname to your obj/executable file"

and yes, the quotes will work.

If using the desktop, put that in the initial directory box as well. Select "prompt for arguments" if you wish to leave the Argument box blank.

Solution 3

You're sort of asking the wrong question: you say you want to see compiler optimizations but then you draw conclusions leading you to think you .obj files are required for that. That works, as cynic's answer shows, but there are alternatives which can be handier/better depending on the situation:

  • run code under the debugger, break, right-click any source file, select 'Go To Disassembly' and you can view source and assembly inline
  • have the compiler output assembly code (again optionally including source and machine code): go to project settings->Compiler->Output Files and set 'Assembler Output'
Share:
13,376
snb
Author by

snb

Having around 11+ years of software development and project management experience. Looking for opportunities to excel my strength and use my skills to contribute to the success and long-term growth of the company by capitalizing on my project management experience and applying my expertise in the various areas of business to ensure the timely accomplishment of all identified goals and schedules Summery Strong Knowledge in C++ and Microsoft technologies 11 Years’ Experience in Software Design, Development and Project Management. Requirement tracking, analysis and refinement Data analysis and Business Intelligence Software Project Estimation and Scheduling Problem-solving and analytical skills Innovative Solution development Business Process Optimization Excellent leadership skills Leading and supporting team Designing and Maintaining DevOps platform for Applications. Good Knowledge of Agile / Scrum process Excellent managing skills in all conditions Trained for Agile/Scrum process

Updated on June 04, 2022

Comments

  • snb
    snb almost 2 years

    In visual studio a object file (.obj) is generating after compiling a c++ file. How to read and understand it? Also how to see the code after compiler optimization in Visual studio 2015. Please redirect if this is already answered.