Search code examples
c++cgccg++preprocessor

Can we access the compiler path from within a source file?


I am looking for a way of programatically accessing the path of the compiler that is compiling the current source file (I'd assume something like a macro here, think of __FILE__ and friends). Ideally something compiler-independent, but I also don't mind a compiler-specific extension (in that case preferably gcc), if only that is possible.

Specifically looking at gcc, I looked up the predefined macros but couldn't find anything. What I am looking for would be something like this:

printf("The compiler that compiled this file is located at %s\n", __COMPILERPATH__);
//  -> "The compiler that compiled this file is located at /usr/bin/gcc"

But there is no such thing as __COMPILERPATH__.


Solution

  • There is no standard way in C++.

    A simple approach is to pass the pass to the compiler path as a macro definition upon compilation. The syntax for that is compiler specific, but a build system generator can help with that.

    If std::embed proposal was accepted into a future standard, then following trick could work: std::embed("/proc/self/cmdline"). However, this is Linux specific.