Check version of GCC at runtime

I need to find an available (installed on the system) version of GCC (Major and minor) inside the execution of program c (at runtime). The point is to programmatically extract the version of available gcc (just as if I were in the shell and typed gcc -version, but in a c-program).

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

I would really like to avoid invoking the shell command for this.

+4
source share
3 answers

Call the gcc shell command with the --version parameter; this is the right way to do it. See popen() for this.

Or you can call GCC to compile a program that prints values ​​for __GNUC__ and __GNUC_MINOR__ . But this will not work if the specified GCC is configured for cross-compilation.

Alternatives would be to look for binary version strings and ensure that you get the right decision that the format will not change and that the version string is different enough for you to recognize it with sufficient certainty.

In 1.5 words: Do not.

+7
source

There is an easy way:

 $ gcc -dumpversion 4.6 
+10
source

I need to know the available (installed on the system) version of GCC (primary and secondary)

What are you going to do with the information?

You cannot get a meaningful answer to your question, because

  • User may not have GCC installed in /usr/bin
  • 5 different versions can be installed elsewhere in the system.
  • There may be a version in /usr/bin that claims to be gcc-XY , but actually gcc-ZW , or Clang , or icc , etc.

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

+4
source

Source: https://habr.com/ru/post/1411513/


All Articles