Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enable optimization in G++ with #pragma
    primarykey
    data
    text
    <p>I want to enable optimization in g++ without command line parameter. I know GCC can do it by writing <code>#pragma GCC optimize (2)</code> in my code. But it seems won't work in G++.</p> <p>This page may help: <a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html">http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html</a></p> <p>My compiler version:</p> <pre><code>$ g++ --version g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 &lt;suppressed copyright message&gt; $ gcc --version gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 &lt;suppressed copyright message&gt; </code></pre> <p>I worte some code like this:</p> <pre><code>#pragma GCC optimize (2) int main(){ long x; x=11; x+=12; x*=13; x/=14; return 0; } </code></pre> <p>And compiled it with GCC <strong>Not G++</strong>. Then I used objdump, which output</p> <pre><code>08048300 &lt;main&gt;: 8048300: 55 push %ebp 8048301: 31 c0 xor %eax,%eax 8048303: 89 e5 mov %esp,%ebp 8048305: 5d pop %ebp 8048306: c3 ret 8048307: 90 nop </code></pre> <p>When I removed <code>#param GCC optimize(2)</code> . objdump output:</p> <pre><code>080483b4 &lt;main&gt;: 80483b4: 55 push %ebp 80483b5: 89 e5 mov %esp,%ebp 80483b7: 83 ec 10 sub $0x10,%esp 80483ba: c7 45 fc 0b 00 00 00 movl $0xb,-0x4(%ebp) 80483c1: 83 45 fc 0c addl $0xc,-0x4(%ebp) 80483c5: 8b 55 fc mov -0x4(%ebp),%edx 80483c8: 89 d0 mov %edx,%eax 80483ca: 01 c0 add %eax,%eax 80483cc: 01 d0 add %edx,%eax 80483ce: c1 e0 02 shl $0x2,%eax 80483d1: 01 d0 add %edx,%eax 80483d3: 89 45 fc mov %eax,-0x4(%ebp) 80483d6: 8b 4d fc mov -0x4(%ebp),%ecx 80483d9: ba 93 24 49 92 mov $0x92492493,%edx 80483de: 89 c8 mov %ecx,%eax 80483e0: f7 ea imul %edx 80483e2: 8d 04 0a lea (%edx,%ecx,1),%eax 80483e5: 89 c2 mov %eax,%edx 80483e7: c1 fa 03 sar $0x3,%edx 80483ea: 89 c8 mov %ecx,%eax 80483ec: c1 f8 1f sar $0x1f,%eax 80483ef: 89 d1 mov %edx,%ecx 80483f1: 29 c1 sub %eax,%ecx 80483f3: 89 c8 mov %ecx,%eax 80483f5: 89 45 fc mov %eax,-0x4(%ebp) 80483f8: b8 00 00 00 00 mov $0x0,%eax 80483fd: c9 leave 80483fe: c3 ret 80483ff: 90 nop </code></pre> <p>However, it won't work with G++!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload