Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC #pragma or command options

Tags:

c++

gcc

g++

pragma

If the compiler has some command-line flags and the code has some pragmas that are incompatible with those flags, which one will be used?

To be clearer: I am compiling with g++ -g -O2 -std=gnu++17 -static {files} – GCC version g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0.

If I write in my code #pragma GCC optimize("Ofast"), will the final code be compiled with -O2 or with -Ofast?

like image 304
Daniel Benedí Avatar asked Dec 31 '25 17:12

Daniel Benedí


2 Answers

That depends on if it's above or below the pragma.

void this_will_be_compiled_with_O2() { stuff(); }

#pragma GCC optimize("Ofast")

void this_will_be_compiled_with_Ofast() { stuff(); }
like image 118
KamilCuk Avatar answered Jan 02 '26 10:01

KamilCuk


Although not explicitly mentioned in the documentation, the description of the #pragma GCC reset_options directive implies that any #pragma GCC optimize directive will override the command line option(s):

#pragma GCC reset_options

    This pragma clears the current #pragma GCC target and #pragma GCC optimize to use the default switches as specified on the command line.

like image 31
Adrian Mole Avatar answered Jan 02 '26 10:01

Adrian Mole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!