9

我有一个 C 应用程序,我正在尝试为 Mac OS X 10.6.4 编译:

$ uname -v
Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386

gcc的如下:

$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)

Makefile的如下:

CC=gcc
CFLAGS=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -O3 -Wformat -Wall -pedantic -std=gnu99

all: myApp
    rm -rf *~

myApp: myApp.o
    ${CC} ${CFLAGS} myApp.o -lbz2 -o myApp
    rm -rf *~

clean:
    rm -rf *.o myApp

问题是我的应用程序调用fseeko64and fopen64,并使用该off64_t类型进行偏移。当我编译我的应用程序时,我收到以下警告和错误:

$ make myApp
gcc -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -O3 -Wformat -Wall -pedantic -std=gnu99   -c -o myApp.o myApp.c
myApp.c: In function ‘extractData’:
myApp.c:119: warning: implicit declaration of function ‘fseeko64’
myApp.c:119: error: ‘off64_t’ undeclared (first use in this function)
myApp.c:119: error: (Each undeclared identifier is reported only once
myApp.c:119: error: for each function it appears in.)
myApp.c: In function ‘extractMetadata’:
myApp.c:305: warning: implicit declaration of function ‘fopen64’
myApp.c:305: warning: assignment makes pointer from integer without a cast

我的代码在 Linux 下构建没有错误。在 Darwin 下构建时,我可以对源代码进行哪些更改以添加大文件支持?

4

3 回答 3

10

Darwin 文件 I/O 默认为 64 位(至少 10.5),刚刚通过在 /usr/include 中的 grepping 找到了这个:

sys/_types.h:typedef __int64_t  __darwin_off_t;

unistd.h:typedef __darwin_off_t     off_t;

所以你需要做的就是像

#ifdef __APPLE__
#  define off64_t off_t
#  define fopen64 fopen
...
#endif
于 2010-10-23T10:26:36.790 回答
4

虽然这个问题有一个被接受的答案,但我认为这个解决方案有点误导。与其修复某些东西,不如先避免稍后再修复它

例如对于GNU C 库文档说的fopen64函数:

如果源代码是_FILE_OFFSET_BITS == 64在 32 位机器上编译的,则此功能在名称下可用fopen,因此可以透明地替换旧接口

您可以在fopen默认支持 64 位 I/O 的系统上使用相同的功能,并且您可以_FILE_OFFSET_BITS=64在 32 位上设置标志,而根本不需要写入重新定义。类似off64_tvs.的类型也是如此off_t

当您必须处理 3rd 方源并在自己的代码中使用标准函数时,请保存重新定义。

于 2015-09-05T08:53:46.023 回答
1

fseeko 和类似命令适用于大文件支持,因此不需要 fseeko64 等Apple 手册页

于 2010-10-23T10:26:13.560 回答