Benjamin K. Benjamin K. - 16 days ago 5
Linux Question

Check GCC version in runtime

I need to find out the available (installed in the system) GCC version (Major and minor) inside the execution of a c program (in runtime). Meaning, programatically extract the version of the available gcc (same as if I was in a shell and typed gcc --version, but in a c program).

The

__GNUC__
and
__GNUC_MINOR__
are only useful in compile time and I've found the
gnu_get_libc_version()
function from
gnu/libc_version.h
, but it only gets me the libc version and I need the GCC version. If there is something similar for GCC it would be great...

I would really like to avoid calling a shell command to do this.

Answer

Invoke the gcc shell command with the parameter --version; it's the correct way to do this. See popen() to do that.

Or you can invoke GCC with to compile a program which prints the values for __GNUC__ and __GNUC_MINOR__. But that will not work if the GCC in question is configured for cross compilaton.

Alternatives would be to search the binary for version strings and hoping that you get the right one, that the format doesn't change and that the version string is distinct enough for you to recognize it with enough confidence.

In 1.5 words: Don't.

Comments