Benjamin K. Benjamin K. - 1 year ago 123
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).


are only useful in compile time and I've found the
function from
, 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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download