OS X中的fp-> _IO_write_base和fp-> _IO_write_end

时间:2015-10-30 12:57:10

标签: c linux macos clang fopen

我需要在Mac上编译一个C文件,该文件是在假设只有Linux和GCC环境的情况下编写的。该文件包含类似

的行
if(fp->_IO_write_base == fp->_IO_write_end)

其中fp指向FILE的指针。由于OS X不提供glibc,因此无法使用clang编译此代码,并且出现以下错误。

error: no member named '_IO_write_base' in 'struct __sFILE'
   if ( fp->_IO_write_base == fp->_IO_write_end )
        ~   ^
error: no member named '_IO_write_end' in 'struct __sFILE'
   if ( fp->_IO_write_base == fp->_IO_write_end )
                              ~   ^
Linux机器上的

FILE(stdio.h中的typedef struct _IO_FILE FILE;)在/usr/include/libio.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
};

但在OS X中,FILE中的stdio.h定义如下。您可以看到_IO_write_base_IO_write_end是特定于glibc的,并且它们在OS X的C库中不存在。

typedef struct __sFILE {
        unsigned char *_p;      /* current position in (some) buffer */
        int     _r;             /* read space left for getc() */
        int     _w;             /* write space left for putc() */
        short   _flags;         /* flags, below; this FILE is free if 0 */
        short   _file;          /* fileno, if Unix descriptor, else -1 */
        struct  __sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
        int     _lbfsize;       /* 0 or -_bf._size, for inline putc */

        /* operations */
        void    *_cookie;       /* cookie passed to io functions */
        int     (*_close)(void *);
        int     (*_read) (void *, char *, int);
        fpos_t  (*_seek) (void *, fpos_t, int);
        int     (*_write)(void *, const char *, int);

        /* separate buffer for long sequences of ungetc() */
        struct  __sbuf _ub;     /* ungetc buffer */
        struct __sFILEX *_extra; /* additions to FILE to not break ABI */
        int     _ur;            /* saved _r when _r is counting ungetc data */

        /* tricks to meet minimum requirements even when malloc() fails */
        unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
        unsigned char _nbuf[1]; /* guarantee a getc() buffer */

        /* separate buffer for fgetln() when line crosses buffer boundary */
        struct  __sbuf _lb;     /* buffer for fgetln() */

        /* Unix stdio files get aligned to block boundaries on fseek() */
        int     _blksize;       /* stat.st_blksize (may be != _bf._size) */
        fpos_t  _offset;        /* current lseek offset (see WARNING) */
} FILE;

我想知道如何在Linux和OS X上编译代码。

1 个答案:

答案 0 :(得分:1)

这是糟糕的代码。 FILE *的内部结构是特定于实现的,因此FILE *需要被视为不透明 - 您无法使用其中的任何内容。因为在某些平台上,它实际上是不透明的,而您无法访问内部。

你必须弄清楚代码尝试做什么,并重写