Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check GCC version in runtime

Tags:

c

linux

gcc

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.

like image 891
Benjamin K. Avatar asked May 09 '12 12:05

Benjamin K.


People also ask

How do I find my gcc version?

So if you ever need to check the version of the GCC C++ compiler that you have installed on your PC, you can do it through the command prompt by typing in the single line, g++ --version, and this will return the result.

How do you check if I have gcc compiler?

In the Command Prompt window type “gcc” and hit enter. If the output says something like “gcc: fatal error: no input files”, that is good, and you pass the test.

How do I know what version of gcc is binary?

comment section from the binary to find the version string. Use objdump and add --section to specify section name. For example, if your compiled a program named foo in the source dir, you can run the following commands to get GCC's version info: $ objdump -s --section .


2 Answers

There is a simple way:

$ gcc -dumpversion
4.6
like image 134
ahoka Avatar answered Oct 24 '22 05:10

ahoka


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.

like image 31
Aaron Digulla Avatar answered Oct 24 '22 07:10

Aaron Digulla