• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A static variable compiles with -O1 and above but not with -O0

 
Greenhorn
Posts: 18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm trying to understand why a member static variable does not compile with the flag -O0 but it does with -O1 and above. A snippet code:


I've been reading about One Definition Rule and it seems that the way SKILL is being used is the issue but I don't quite understand it. Why would the compiler complaint with -O0 but not with -O1? I know static variables should be declared in the cpp file always but I'm trying to understand the difference between -O0 and the other optimization flags. The command I used to compile was:
 
Rancher
Posts: 508
15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question - I don't know, but it can be demonstrated using the Online C++ Compiler:
https://onlinegdb.com/g-czg9Hfu

Click on 'Run' and you get a compiler error. But click on 'Fork this', click on the settings cog button (top right), add -O1 to 'Extra Compiler Flags' and 'Run' again - it builds/runs fine.

The fact it can be persuaded to compile ok is probably a gcc-specific idiosyncrasy; if it didn't compile when it should then it would be a gcc bug.
 
reply
    Bookmark Topic Watch Topic
  • New Topic