简体   繁体   中英

How to check code generated by C++ compiler?

just like in topic - is there any software to open (what?) and here I don't even know what to open - file with object code or exe? My today's questions (if only todays ;)) may seem bit odd but I'm going through excersises in "The C++ Programming Language" by BS and sometimes I'm just stuck on particular question. I'm sometimes bit irritated by style of this book (excellent in many aspects) that he (BS) asks some questions which you won't find answer in his book on how to do it or even where to start. Like this one for example:

Run some tests to see if your compiler really generates equivalent code for iteration using pointers and iteration using indexing. If different degrees of opimization can be requested, see if and how that affects the quality of the generated code.

Thats from chapter 5 question 8. Up to this point nowhere in this book is even mentioning testing and analyzing code generated by compiler. Anyway, if someone could help me with this I'll be greatful. Thank you.

The debugger will help you. Most debuggers let you halt the program and look into disassembly. The nice thing is they point you right to disassembly of the line you set the breakpoint to, not to just all the compilation result.

Once in a while I do that in Visual Studio - compile the program, put a breakpoint onto the beginning of code of interest, start the program, then when it is halted I open the disassembly and immediately see the code corresponding to that C++ code.

If you're using g++, you can do g++ -S main.cpp . This will output the assembly in a file called main.s . However, if the functions you're interested in are spread across different .cpp files, it might be more convenient to do an objdump on the final executable.

There's also a nice tool called embroider that pretty-prints the objdump output for you as HTML, crosslinking the various function calls and jumps.

Many compilers can generate "listing" files of the assembly code they are generating during compilation, interspersed with the statements from the C source code. Also, there are tools which disassemble object and executable files.

How these tools are actually activated is depending on your toolchain, obviously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM