How to enable optimization in G++ with #pragma

前端 未结 1 1070
闹比i
闹比i 2021-02-15 14:38

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 i

1条回答
  •  难免孤独
    2021-02-15 15:19

    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 
    : 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 讨论(0)
提交回复
热议问题