为什么即使定义了_GNU_SOURCE,我也无法访问uname struct的域名成员

时间:2019-12-09 10:04:58

标签: c linux macros

我正在尝试通过调用uname系统调用来获取Linux内核版本信息,但是却出现编译器错误,提示‘struct utsname’ has no member named ‘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结构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
};

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

1 个答案:

答案 0 :(得分:6)

来自man feature_test_macros

  

注意:为了有效,必须定义功能测试宏   在包含任何头文件之前

是:

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