函数fseeko的隐式声明

时间:2016-11-23 17:20:53

标签: c large-files fseek

我正在尝试将fseeko函数与GCC编译器结合使用,以便处理C中大于4GiB的文件。现在,一切正常,我可以处理文件超过4GiB,但GCC一直抱怨隐含声明了fseeko函数。这是生成此消息的源代码的最小工作示例:

#define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE

#include "MemoryAllocator.h"

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/stat.h>

typedef struct BlockReader {
    char* fileName;
    // Total filesize of the file with name fileName in bytes.
    unsigned long long fileSize;
    // Size of one block in bytes.
    unsigned int blockSize;
    // Defines which block was last read.
    unsigned int block;
    // Defines the total amount of blocks
    unsigned int blocks;
} BlockReader;

unsigned char* blockreader_read_raw_block(BlockReader* reader, long startPos, unsigned int length, size_t* readLength) {
    FILE* file = fopen(reader->fileName, "rb");

    unsigned char* buffer = (unsigned char*) mem_alloc(sizeof(unsigned char) * (length + 1));

    FSEEK(file, startPos, 0);
    fclose(file);

    // Terminate buffer
    buffer[length] = '\0';
    return buffer;
}

我无法在任何地方找到我必须包含的标题来修复此警告。 GCC给出的确切警告是:

src/BlockReader.c: In function ‘blockreader_read_block’:
src/BlockReader.c:80:2: warning: implicit declaration of function ‘fseeko’ [-Wimplicit-function-declaration]
  FSEEK(file, reader->blockSize * reader->block, 0);

1 个答案:

答案 0 :(得分:2)

如果您正在使用-std-std=c99之类的-std=c11选项之一,则会要求完全符合标准的C环境,默认情况下不会公开POSIX接口(暴露)它们将是不符合的,因为它们位于为应用程序保留的命名空间中。您需要将_POSIX_C_SOURCE_XOPEN_SOURCE定义为适当的值才能获得它们。命令行上的-D_POSIX_C_SOURCE=200808L

#define _POSIX_C_SOURCE 200808L
在包含任何标题之前,在源文件中

就是这样做的方法。

此外,虽然这不是您的直接问题,但请注意__USE_LARGEFILE64_LARGEFILE_SOURCE_LARGEFILE64_SOURCE都不正确。在包含任何标头之前,在命令行上只需要off_t或者在源文件中-D_FILE_OFFSET_BITS=64,唯一需要做的就是#define _FILE_OFFSET_BITS 64