Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Optimization flag removing undefined reference to extern variable

Considering the following piece of code :

extern int var;

void foo(int & param)
{
   (void) param;
}

int main(void)
{
   foo(*(&var));
   return 0;
}

Compiled this way :

$ g++ -Os -o test.o -c test.cpp
$ g++ test.o

But when I remove the -Os flag, there is an undefined reference to var.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What kind of optimization are enabled by -Os to skip this undefined reference ? (I tried to replace the flag by all the optimizations it enable according to the GCC documentation but I can’t reproduce without -Os.

Another question, when I compile the sample in one go :

$ g++ -c test.c

There is no error even though there is no optimization flag, why ?

>Solution :

After performing some binary search on the flags, the relevant one appears to be -fipa-pure-const, demo here. The description is "Discover which functions are pure or constant. Enabled by default at -O1 and higher.", which presumably includes noticing that foo doesn’t actually do anything with param.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading