1

Is there any macro in c++ (using gcc) to get the compilation options used to build the executable ? I'm sure I saw something like that in some about dialogs.

any help will be appreciated

PS: while the question in Detect GCC compile-time flags of a binary interests in finding the options activated to compile a program, I'm interesting in finding the exact command line options used to compile my program from within this program source.

Community
  • 1
  • 1
sohaibafifi
  • 745
  • 7
  • 16
  • 2
    look here http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html – Andreas Grapentin Mar 04 '13 at 14:18
  • 2
    If it's ok for you to specify an option before the build, this will help: http://stackoverflow.com/questions/12112338/get-the-compiler-options-from-a-compiled-executable – jogojapan Mar 04 '13 at 14:18
  • @AndreasGrapentin Oh I see. I might have misunderstood the question. But if that is so, you could easily put together an answer based on the information from the link you provided. – jogojapan Mar 04 '13 at 14:23
  • Alternatively (and probably less compiler specific) you could also modify your build system to write the invoked gcc commands into a file and create an object file of it and link it to your executable – Andreas Fester Mar 04 '13 at 14:25
  • @jogojapan I'm looking for a method to get the compilation flags within the same code, not from extern – sohaibafifi Mar 04 '13 at 14:25
  • @sohaibafifi Is the link provided by Andreas Grapentin not the answer to your question? – jogojapan Mar 04 '13 at 14:26
  • What I want is simply something like : program compiled with "-m64 -pipe -Ofast -march=corei7 -mtune=corei7 -ffast-math -ftree-loop-linear -Wall -W -DDOUBLE" options – sohaibafifi Mar 04 '13 at 14:30
  • Make yourself comfortable with `make`. – sellibitze Mar 04 '13 at 14:31
  • @sohaibafifi I don't think it makes much sense to do this at compile time in your actual code. what build system do you use? maybe you can better solve it there. – Andreas Grapentin Mar 04 '13 at 14:32
  • @AndreasG I might be blind or silly or both, but I can't see a macro on that page that would do what the OP is asking for. – us2012 Mar 04 '13 at 14:32
  • @us2012 there are macros for a couple of flags, e.g. `-ansi`. to be honest, the OP was a bit vague about what he really wanted, that's why I posted this a comment instead of a full-fledged answer. :) – Andreas Grapentin Mar 04 '13 at 14:33
  • @AndreasGrapentin I'm using qmake, then make – sohaibafifi Mar 04 '13 at 14:40
  • 1
    @sohaibafifi this is tough. I doubt there is a method to print the string of command line arguments that were passed to the gcc. you'll have to go one level higher and look into the makefiles. – Andreas Grapentin Mar 04 '13 at 14:45
  • Sounds like another "XY question", where X is the problem you are trying to solve, Y your thought of a solution, so you ask how to do Y. What are you actually trying to achieve? – Mats Petersson Mar 04 '13 at 15:21
  • I was looking form something similar, mostly to know the optimization level, GNU has some information, like `__OPTIMIZE__` but unfortunately it is either 1 or 0, no information of the optimization level is given. I am talking about the macros defined here https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html (Thanks @AndreasGrapentin) Also I don't think this question is a duplicate. – alfC Jan 17 '15 at 00:39

2 Answers2

2

Apart from creating the compile string from the Common Predefined Macros , which seems hectic. I think there is an easy way to do it. The gcc -V on debian gives back flags used for configuration.

However, my shot would be to get full command in ./configure equivalent step and dump it to some file like config_line.h as a define.

Something like:

./configure:

#!/bin/sh
echo "#define conf_flags \"configured with: "$*"\"" >> config_line.h
#do some configuration steps here
#maybe even compilation itself

Then:

luk32:~/projects/tests$ ./test.sh --with=test
luk32:~/projects/tests$ cat ./config_line.h 
#define conf_flags "configured with: --with=test"

You get full config line defined in the external file under a define statement. I think its fairly straight forward and easy to use. And no need for much compiler magic.

It is also worth of noting you can most probably (if not always) create such file(s) right before the actual compilation so they are actually up-to-date and valid during compilation step. Answer in get-the-compiler-options-from-a-compiled-executable would imply the executable already exists, which might be a bummer in some cases.

Note: I gave bash example, but I'm pretty sure you can output similar header file under any half-descent build system, be it make, qmake, cmake, etc. the bash begin the simplest case.

I think most of them have access to the command line they are invoked with, as well as they provide easy way to get actual compile command. For example to provide two literals, one with commands used for make like -j 13 and another g++ ... used for actual compilation step performed by make.

Note2: I know this is not an answer the OP asked, but I guess it serves his purpose in the 1st place.

Community
  • 1
  • 1
luk32
  • 15,812
  • 38
  • 62
0

Because I'm using qmake build system I came across this solution :

I added this line to the end of my pro file :

QMAKE_CXXFLAGS += -DFLAGS=\"$$QMAKE_CXXFLAGS $$QMAKE_CXXFLAGS_RELEASE\"

then retrieved what I want from the FLAGS macro

sohaibafifi
  • 745
  • 7
  • 16