Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:15:20+00:00 2026-05-29T21:15:20+00:00

I want to enable optimization in g++ without command line parameter. I know GCC

  • 0

I want to enable optimization in g++ without command line parameter.
I know GCC can do it by writing #pragma GCC optimize (2) in my code.
But it seems won’t work in G++.

This page may help: http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html

My compiler version:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
<suppressed copyright message>

$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
<suppressed copyright message>

I worte some code like this:

#pragma GCC optimize (2)
int main(){
    long x;
    x=11;
    x+=12;
    x*=13;
    x/=14;
    return 0;
}

And compiled it with GCC Not G++. Then I used objdump, which output

08048300 <main>:
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

When I removed #param GCC optimize(2) . objdump output:

080483b4 <main>:
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

However, it won’t work with G++!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:15:21+00:00Added an answer on May 29, 2026 at 9:15 pm

    This appears to be a bug in g++ (Bug 48026, references another related issue.)

    As a workaround, you can mark each function with __attribute__((optimize("whatever"))). Not great.

    int main() __attribute__((optimize("-O2")));
    int main()
    {
        long x;
        x=11;
        x+=12;
        x*=13;
        x/=14;
        return 0;
    }
    
    $ g++ -Wall -c t.c
    $ objdump -d t.o
    
    t.o:     file format elf64-x86-64
    
    
    Disassembly of section .text.startup:
    
    0000000000000000 <main>:
       0:   55                      push   %rbp
       1:   31 c0                   xor    %eax,%eax
       3:   48 89 e5                mov    %rsp,%rbp
       6:   5d                      pop    %rbp
       7:   c3                      retq   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to enable auto update script in flex3. i am using below code
I want to enable GZIP compression on my gSOAP client. How can I do
i want to disable right click on the webpage but i want enable right
I want to enable Encode::HanExtra on Windows XP environment. I can't find the name
I want to enable Access for assistive devices in System Preferences programmatically. But Problem
I want to enable code coverage for some unit test in my solution. After
I want to enable template path hints in admin panel. I know how to
I want to enable/disable clipboard in an edittext. How can I do this?
I am compiling my code using following command: gcc -O3 -ftree-vectorizer-verbose=6 -msse4.1 -ffast-math With
I want to enable file download in my MVC application, without simply using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.