gpt4 book ai didi

c - 为什么即使我定义了 _GNU_SOURCE 也无法访问 uname 结构体的域名成员

转载 作者:行者123 更新时间:2023-12-03 00:37:12 25 4
gpt4 key购买 nike

我尝试通过调用 uname 系统调用来获取 Linux 内核版本信息,但收到编译器错误,提示 ‘struct utsname’ 没有名为“domainname”的成员

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/utsname.h>

#define _GNU_SOURCE

int main(void) {

struct utsname buffer;

errno = 0;
if (uname(&buffer) != 0) {
perror("uname");
exit(EXIT_FAILURE);
}

printf("system name = %s\n", buffer.sysname);
printf("node name = %s\n", buffer.nodename);
printf("release = %s\n", buffer.release);
printf("version = %s\n", buffer.version);
printf("machine = %s\n", buffer.machine);

#ifdef _GNU_SOURCE
printf("domain name = %s\n", buffer.domainname);
#endif

return EXIT_SUCCESS;
}

根据https://linux.die.net/man/2/uname struct utsname 是

struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};

我不确定我在这里错过了什么

最佳答案

来自man feature_test_macros :

NOTE: In order to be effective, a feature test macro must be defined before including any header files

它是:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/utsname.h>

关于c - 为什么即使我定义了 _GNU_SOURCE 也无法访问 uname 结构体的域名成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com