C++ 如何获取Linux发行版名称和版本?
在Windows中,我读取注册表项C++ 如何获取Linux发行版名称和版本?,c++,c,linux,api,C++,C,Linux,Api,在Windows中,我读取注册表项SOFTWARE\Microsoft\windowsnt\CurrentVersion\ProductName,以获取操作系统的全名和版本 但是在Linux中,代码 struct utsname ver; uname(&ver); retVal = ver.sysname; 返回字符串linux,而不是ubuntu9.04 如何获取Linux发行版名称和版本?通常: cat /etc/issue 我不确定我是否完全遵循了你的目标,但我认为你只是想要u
SOFTWARE\Microsoft\windowsnt\CurrentVersion\ProductName
,以获取操作系统的全名和版本
但是在Linux中,代码
struct utsname ver;
uname(&ver);
retVal = ver.sysname;
返回字符串linux
,而不是ubuntu9.04
如何获取Linux发行版名称和版本?通常:
cat /etc/issue
我不确定我是否完全遵循了你的目标,但我认为你只是想要uname上的“all”标志:
uname -a
尝试:
你也可以试试
lsb_release -a
或:
获取这些信息的目的是什么 如果您试图检测系统的某些功能或属性(例如,它是否支持某些系统调用或是否具有某些库),而不是依赖lsb_版本的输出,您应该:
- 尝试使用给定的功能并正常地失败(例如,dlopen用于库,syscall(2)用于syscalls等等)
- 如果适用,将其作为./configure检查的一部分(自动识别系统功能/属性的标准FOSS方式)
dl = dlopen(module_path, RTLD_LAZY);
if (!dl) {
fprintf(stderr, "Failed to open module: %s\n", module_path);
return;
}
funcptr = dlsym(dl, module_function);
if (!funcptr) {
fprintf(stderr, "Failed to find symbol: %s\n", module_function);
return;
}
funcptr();
dlclose(dl);
您甚至可以优雅地测试CPU操作码支持,例如:
在我的系统上,从bash(终端)提示符产生以下结果:
尝试这种方式是一种有趣的方式,并且比lsb版本限制更少
$ cat /etc/*-release
至少在CentOS 7和Ubuntu 16.04上都可用,这使得它比lsb_发行版
(不在CentOS上)或/etc/system发行版
(不在Ubuntu上)更具跨平台性
例如:
NAME=Fedora
VERSION="17 (Beefy Miracle)"
ID=fedora
VERSION_ID=17
PRETTY_NAME="Fedora 17 (Beefy Miracle)"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:fedoraproject:fedora:17"
HOME_URL="https://fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
$ cat /etc/*-release
$ lsb_release -a
$ hostnamectl
$ lsb_release -a
$ hostnamectl
$ uname -a
or
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
$ cat /proc/version
不幸的是,如果Linux localhost.localdomain 2.6.29.6-217.2.8.fc11.i586#1 SMP Sat Aug 15 00:44:39 EDT 2009 i686 i686 i386 GNU/Linux/etc/issue是登录前终端上显示的内容,则在Fedora 11上输出“uname-a”。读取文件本身通常会给您类似“这是\n。\O(\s\m\r)\t”的信息。不是很有用!这是一段自由格式的文本,可以包含任何内容,换句话说,+1这最终在发行版中被“标准化”。尽管回过头来看,该文件并不存在,但每个发行版都会放上自己的文件,如/etc/redhat-release。这只适用于符合LSB的Linux发行版,但不能保证适用于不符合LSB的发行版。OTOH,它还将在其他符合LSB的非Linux Unice上工作。例如,我很确定它在Adroid上不起作用。请注意,在例如Gentoo Linux上,默认情况下lsb_版本并不总是存在。我刚检查过,它由可选软件包sys apps/lsb release提供,目前未安装在我的系统上。
lsb release
是否适用于以下所有发行版?:Debian/Ubuntu | Red Hat Enterprise/Fedora Linux/Suse Linux/Cent OS?lsb release
在CentOS 7.4上不存在。您可能需要编辑标题,以明确问题在于如何从C源代码执行此操作,不是以脚本编写者或命令行用户的身份。@DarenW:这个问题有C/C++标记。另请参阅,我相信uname-mr返回Linux内核的版本,因此,假设不同版本的描述格式一致,那么“lsb_release-ds”应该是发布名称和版本所需的全部内容。谢谢,我想知道您应该如何使用short参数,我正在尝试它'lsb_release-s',并且想知道为什么它失败了。干杯
$ hostnamectl
$ uname -a
or
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
$ cat /proc/version