大文件支持C编程不工作 [英] Large file support not working in C programming

查看:130
本文介绍了大文件支持C编程不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译一个共享对象(即最终在Python用于ctypes的)。用于建立对象的命令行是:

I'm trying to compile a shared object (that is eventually used in Python with ctypes). The command-line used to build the object is:

gcc -Wall -O3 -shared -Wl,-soname,borg_stream -lm -m128bit-long-double -fPIC \
    -D_FILE_OFFSET_BITS=64 -o borg_stream.so data_stream.c data_types.c \
    file_operations.c float_half.c channels.c statistics.c index_stream.c helpers.c

该库正确建立在一个32位操作系统和它做什么,它需要对小文件。然而,它失败大于4GB的文件的单元测试。此外,它设置errno为EOVERFLOW做一个fseek的/ FTELL时。但是,如果我的printf的sizeof(off_t),它返回8.如果我删除 -D_FILE_OFFSET_BITS = 64 ,然后它打印4。所以它看起来像 -D_FILE_OFFSET_BITS 正确做的工作。

The library builds properly on a 32-bit OS and it does what it needs to for small files. However, it fails the unit tests for files larger than 4GB. In addition, it sets errno to EOVERFLOW when doing an fseek/ftell. However, if I printf sizeof(off_t), it returns 8. If I remove -D_FILE_OFFSET_BITS=64, then it prints 4. So it seems like -D_FILE_OFFSET_BITS is properly doing its job.

为什么大文件支持还是不行?我在做什么错了?

Why does large file support still not work? What am I doing wrong?

推荐答案

添加选项 -D_LARGE_FILE_SOURCE = 1 来GCC编译。

Add the option -D_LARGE_FILE_SOURCE=1 to gcc compilation.

fseek64 是一个C函数。以使其可你必须包括系统头文件之前定义_FILE_OFFSET_BITS = 64。这将或多或少地定义 fseek的表现为实际 fseek64 。或者你可以做到这一点的编译参数例如GCC -D_FILE_OFFSET_BITS = 64,你已经做的。

fseek64 is a C function. To make it available you'll have to define _FILE_OFFSET_BITS=64 before including the system headers. That will more or less define fseek to behave as actually fseek64. Or you could do it in the compiler arguments e.g. gcc -D_FILE_OFFSET_BITS=64, that you are already doing.

http://www.suse.de/~aj/linux_lfs.html有在Linux上大文件支持一个良好的信息:

http://www.suse.de/~aj/linux_lfs.html has a good information for large file support on linux:

GCC -D_FILE_OFFSET_BITS = 64 编译你的程序。这将迫使所有的文件访问调用使用64位的变体。几种类型也随之变化,例如 off_t 变成 off64_t 。这是很重要的,始终使用正确的类型和不使用例如 INT 而不是 off_t 在C code。为了与其他平台的可移植性你应该使用在getconf LFS_CFLAGS 将返回 -D_FILE_OFFSET_BITS = 64 在Linux平台上,但可能返回的东西否则对如在Solaris上。对于链接,您应该使用通过在getconf LFS_LDFLAGS 报告的链接标志。在Linux系统中,你不需要特殊的链接标志。
定义 _LARGEFILE_SOURCE _LARGEFILE64_SOURCE 。有了这些定义,你可以直接使用LFS功能,如 open64
使用 O_LARGEFILE 标志与开放的大文件进行操作。

Compile your programs with gcc -D_FILE_OFFSET_BITS=64. This forces all file access calls to use the 64 bit variants. Several types change also, e.g. off_t becomes off64_t. It's therefore important to always use the correct types and to not use e.g. int instead of off_t in your C code. For portability with other platforms you should use getconf LFS_CFLAGS which will return -D_FILE_OFFSET_BITS=64 on Linux platforms but might return something else on for e.g. on Solaris. For linking, you should use the link flags that are reported via getconf LFS_LDFLAGS. On Linux systems, you do not need special link flags. Define _LARGEFILE_SOURCE and _LARGEFILE64_SOURCE. With these defines you can use the LFS functions like open64 directly. Use the O_LARGEFILE flag with open to operate on large files.

希望这有助于。

这篇关于大文件支持C编程不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆