Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++: Is there a way to access compile flags inside the code that is being compiled?

Tags:

c++

gcc

Is there a way (e.g., defined constants) to access compile flags with which the compiler was run inside the code that is being compiled.

For example, I want a program that writes the flags with which it was compiled.

int main(){
    std::cout << COMPILE_FLAGS << std::endl;
}

Do such constants exist for gcc/g++? Or even better: Are there constants that are defined both in gcc and clang?

I am especially interested in examining the optimization level and the value of the -march flag. So, if there are no constants that show all flags, are there at least ones that display these values?

like image 345
gexicide Avatar asked Dec 12 '14 16:12

gexicide


1 Answers

The following command prints out all predefined macros:

g++ -dM -E - < /dev/null

This works with both gcc and g++. You can check yourself - unfortunately, there is no macro, that gives easy access to the full gcc/g++ command line.

Fortunately, most -m... flags result in adequate precompiler macros to be defined. For example, -m64 defines __x86_64 and -m32 defines __code_model_32__ . Or for -march: -march=core-avx2 results in #define __core_avx2__ 1 .

Just add the option, that you need to check, on the command line above, and check the result for new macro defines.

like image 55
Kai Petzke Avatar answered Oct 07 '22 15:10

Kai Petzke