我正在尝试使用Xcode在Mac上运行C书中的代码,但编译时遇到问题
我有一个头文件,该文件具有用于文件的结构和一个缓冲区,但是当我尝试使用缓冲区变量时,在方法实现上出现错误。
我的标题看起来是这样
#ifndef custio_h
#define custio_h
#include <fcntl.h>
#include <unistd.h>
#define EOF (-1)
#define BUFSIZ 1024
#define OPEN_MAX 20 /* max #files open at once */
typedef struct _iobuf {
int cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of buffer */
int flag; /* mode of file access */
int fd; /* file descriptor */
} FILE;
extern FILE _iob[OPEN_MAX];
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
enum _flags {
_READ = 01, /* file open for reading */
_WRITE = 02, /* file open for writing */
_UNBUF = 04, /* file is unbuffered */
_EOF = 010, /* EOF has ocurred on this file */
_ERR = 020 /* error ocurred on this file */
};
int _fillbuf(FILE *);
int _flushbuf(int, FILE *);
FILE *cfopen(char *, char *);
#define feof(p) (((p)->flag & _EOF) != 0)
#define ferror(p) (((p)->flag & _ERR) != 0)
#define fileno(p) ((p)->fd)
#define getc(p) (--(p)->cnt >= 0 ? (unsigned char) *(p)->ptr++ : _fillbuf(p))
#define putc(x,p) (--(p)->cnt >= 0 ? *(p)->ptr++ = (x) : _flushbuf((x),p))
#define getchar() getc(stdin)
#define puchar(x) putc((x), stdout)
#endif /* custio_h */
我的实现是这样
#include "custio.h"
#define PERMS 0666
/* fopen: open file, return file pointer */
FILE *cfopen(char *name, char *mode)
{
int fd;
FILE *fp;
if (*mode != 'r' && *mode != 'w' && *mode != 'a')
return NULL;
for (fp = _iob; fp < (_iob + OPEN_MAX); fp++)
if ((fp->flag & (_READ | _WRITE)) == 0)
break; /* found free slot */
if (fp >= _iob + OPEN_MAX) /* no free slots */
return NULL;
if (*mode == 'w')
fd = creat(name, PERMS);
else if (*mode == 'a') {
if ((fd = open(name, O_WRONLY, 0)) == -1)
fd = creat(name, PERMS);
long c = 10000000;
lseek(fd, c, 2);
} else
fd = open(name, O_RDONLY, 0);
if (fd == 1) /* could not access name */
return NULL;
fp->fd = fd;
fp->cnt = 0;
fp->base = NULL;
fp->flag = (*mode == 'r') ? _READ : _WRITE;
return fp;
}
我使用_iob的行就是问题所在。
错误提示: 架构x86_64的未定义符号: “ __iob”,引用自: cucfio.o中的_cfopen ld:找不到架构x86_64的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)