Is it possible to see optimized C++ code by compiler in training reasons? Interesting for clang and g++. For example, I expect to see files like:
main.cpp
main.cpp.optimized
Related
When I run my code (any code), in debug mode I have the option of "Disassembly".
I know that "Disassembly" is the creation of assembly code from machine code ('1' and '0' that stored in object files).
So, I can assume that when I compile my code in Visual Studio, there is no "Assembly/Assembler" stage between the C/C++ code to the machine code/object file, and if I want to see the Assembly code I need to ask Visual Studio to disassemble it from object file to assembly.
So my questions are:
am I right, is there no way to see asm other than disassembly?
(if 1 is no): why does Visual Studio's debugger show a "Disassembly" option, not "Assembly"?
Does Clang/GCC have an assembly stage, or does the C/C++ code compile to object code directly?
editor's note: the answer to 3 actually differs for GCC vs. most other mainstream C/C++ compilers like clang/LLVM and MSVC. Since that's kind of a separate question from how MSVC works, that part is a duplicate of these other SO Q&As:
Does a compiler always produce an assembly code?
What do C and Assembler actually compile to?
What is the need to generate ASM code in gcc, g++
When a debugger provides a disassembly option, it is generally examining memory of the running process, augmented by debugging information embedded in or associated with the executable file, which can include references that help the debugger identify associated source code.
Separately, compilers usually have an option to show you the assembly language that is either the assembly language they use internally (perhaps in temporary files passed to specific subprocesses, such as an assembler) or its equivalent (what it would generate if it were not generating object code directly). For GCC and Clang, the -S switch does this. For Visual Studio, /FA does this.
Sorry if this is a really stupid question.
I am trying to track down a bug in some C++. The code works in debug mode, but when I apply optimizations with -03 in the makefile it gives incorrect results.
Is it possible to turn off the optimizations for certain functions in the source so that I can locate which routine it is that is broken?
I'm using g++ with -std=c++11
I searched the gcc 4.8.1 documents but couldn't find an answer to this:
I have some SSE4.1 code and fallback code, at runtime I detect whether the system supports SSE4.1 and in case it doesn't, I use the fallback code.
So far so good, but with latest gcc versions this is what happens:
- my application crashes because SSE4.1 instructions are being spread throughout the code every time a string comparison is performed
Since I'm compiling all my files with -msse41 this sounds reasonable but crashes my code. My question is this: is there any way to restrict SSE41 usage to just that code which makes use of SSE4.1? Unfortunately these are header files used everywhere so it would be rather difficult to just compile those translation units with msse41
As of GCC 4.8, you can use multi-versioned functions, see http://gcc.gnu.org/gcc-4.8/changes.html, look for "Function Multiversioning Support with G++". Disclaimer: I did not use this (as of yet).
I am trying to compile an old C++ software project in Code::Blocks using the gcc compiler, and after fixing a few other issues, I've hit a wall: the project has a file with Intel-style inline ASM written as
_asm {
code here
}
and the compiler refuses to compile it with "error: '_asm' was not declared in this scope".
I've spent a while Googling around looking for solutions, but the only ones I can find are to add -masm=intel to the build options (which I've tried and can't get to work), or to convert the code to asm ("code here"); (which isn't feasible because of the sheer amount of ASM). Does anyone know how I can get gcc to compile this code as-is, or should I give up and use a different compiler?
GCC uses a very different syntax for inline assembler, so you won't be able to handle it with trivial changes. I see the following options:
Rewrite everything in GCC syntax or as C code
Make some script to translate to GCC syntax (non-trivial task)
Compile the code with whatever compiler it was written for (MSVC?)
You simply can't get gcc to compile the code 'as is'. If you need to compile this thing using gcc, you have to rewrite the code, in C++ or gcc-compatible asm. If there really is a lot of assembly code -- say, 200 instructions or more -- it might be worthwhile learning the gcc assembler syntax; if not, code it in C++.
Does C++ code compile to assembly code? If we have C++ code, will we be able to get assembly code?
The vast majority of C++ compilers will convert the C++ source into object files (machine code with enough control information to be linked into an executable). They may actually generate assembly language as an interim step and even use a separate assembler for processing the assembler source, but you'll generally never see that. For example, you have to actually go out of your way to get gcc to generate assembly code (.s file) by using the -S flag. Normally, you would never see the assembly.
But the C++ standard doesn't mandate the final form that's output from the compiler, just that the code has to behave in a certain way when you run it.
In fact, the earliest C++ "compilers" actually generated C source code and then compiled that.
You can have your C++ compiler generate object code, Java byte code, or even GWBASIC, should you be feeling masochistic.
Your code has to be understood by the machine, and, as it is not interpreted nor running in a VM, it is first transformed in assembly. You can get this assembly code by using the -S flag in your g++ compile options (as long as you are using g++ of course).
g++ -S -o file.s file.cpp
should do the trick.
It depends on the compiler. There are no real rules regarding what C++ compiles into, except at some point it should be able run on a computer. Most compilers have a switch to compile to assembly.
With gcc you can add -S to compile into a .asm file.
For visual studio see: http://codegem.org/2008/10/generate-assembly-from-c-code-in-visual-studio