Benjamin K.
Benjamin K.

Reputation: 1105

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.

Upvotes: 5

Views: 9161

Answers (3)

ahoka
ahoka

Reputation: 412

There is a simple way:

$ gcc -dumpversion
4.6

Upvotes: 18

Aaron Digulla
Aaron Digulla

Reputation: 328536

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.

Upvotes: 7

Employed Russian
Employed Russian

Reputation: 213375

I need to find out the available (installed in the system) GCC version (Major and minor)

What are you going to do with the information?

You can't get a meaningful answer to your question, because

  1. The user may not have any GCC installed in /usr/bin
  2. May have 5 different versions installed elsewhere on the system
  3. May have a version in /usr/bin which pretentds to be gcc-X.Y, but is actually gcc-Z.W, or Clang, or icc, etc.

But if you insist on getting a meaningless answer, popen("gcc --version") and parse the resulting output.

Upvotes: 5

Related Questions