在运行时检查GCC版本 [英] Check GCC version in runtime

查看:106
本文介绍了在运行时检查GCC版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在执行c程序(在运行时)内找出可用的(安装在系统中)GCC版本(主要和次要).意思是,以编程方式提取可用gcc的版本(就像我在shell中键入gcc --version,但在c程序中一样).

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).

__GNUC____GNUC_MINOR__仅在编译时有用,我从gnu/libc_version.h找到了gnu_get_libc_version()函数,但是它只有我自己的libc版本,我需要GCC版本.如果GCC有类似的功能,那就太好了...

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...

我真的很想避免调用shell命令来执行此操作.

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

推荐答案

使用参数--version调用gcc shell命令;这是正确的方法.请参见popen()来做到这一点.

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

或者您可以调用GCC来编译一个打印__GNUC____GNUC_MINOR__值的程序.但是,如果将相关GCC配置为交叉编译,那将是行不通的.

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.

用1.5个字表示:不要.

In 1.5 words: Don't.

这篇关于在运行时检查GCC版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆