【问题标题】:Where to find struct _IO_FILE在哪里可以找到结构 _IO_FILE
【发布时间】:2013-05-01 17:16:28
【问题描述】:

我正在查看 /usr/include/stdio.h

偶然发现了下面这段代码。

/* Standard streams.  */

extern struct _IO_FILE *stdin;          /* Standard input stream.  */
extern struct _IO_FILE *stdout;         /* Standard output stream.  */
extern struct _IO_FILE *stderr;         /* Standard error output stream.  */
/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr

我的问题是,这个结构 struct _IO_FILE 在哪里声明,我想看看布局。并且代码也提到了

#define stdin stdin

这应该如何工作?

【问题讨论】:

  • 您使用的是什么操作系统/编译器?
  • 操作系统: 3.2.0-41-generic-pae #66-Ubuntu SMP Thu Apr 25 03:50:20 UTC 2013
  • 编译器: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
  • 了解结构的外观并不像您预期​​的那样有用。它会因一个操作系统而异,甚至会因标准库的一个版本而异。您编写的任何依赖于这些内部细节的代码都将是非常不可移植的。如果您正在尝试完成特定的事情,可能会有更好的方法来完成它。
  • 并没有试图让任何东西变得不可移植,这只是我在 Linux 中开发设备驱动程序时养成的习惯。您那里没有图书馆,所以您必须深入研究所有内容。

标签: c stdio


【解决方案1】:

/usr/include/libio.h(包含在stdio.h中),结构定义为:

struct _IO_FILE {
  int _flags;           /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

我通过查看预处理代码来搜索定义:

echo '#include<stdio.h>' | gcc -E -

【讨论】:

  • 这真的很有帮助@KingsIndian,我对 echo 命令不太了解,所以请你给我一些关于你使用的 echo 命令以及你传递的参数的说明。跨度>
  • 这就是您系统上的样子。在另一个系统上可能会完全不同。
  • 这与在 C 程序中包含 stdio.h 并查看预处理代码相同。 -E options 告诉 gcc 在所有#include 之后的预处理阶段之后停止。
【解决方案2】:

你可以这样搜索:

grep -rn "struct _IO_FILE {" --include="*.h" /usr/include

【讨论】:

    猜你喜欢
    • 2017-08-23
    • 2019-10-10
    • 2011-11-28
    • 2011-05-09
    • 2021-09-14
    • 1970-01-01
    • 2012-11-30
    • 2011-08-22
    • 2014-12-14
    相关资源
    最近更新 更多