Ошибка: FALLOC_FL_KEEP_SIZE необъявлен (первое использование в этой функции)

Согласно справочной странице, я определил _GNU_SOURCE перед включением <fcntl.h>:

#include <stdio.h>

#define _GNU_SOURCE    /* For Linux's fallocate(). */
#define HAVE_FALLOCATE 1

#include <fcntl.h>

int main(void)
{
    printf("%d\n", FALLOC_FL_KEEP_SIZE);
}

который я скомпилировал с:

gcc-13 -std=gnu2x c.c

Но это не удается:

error: `FALLOC_FL_KEEP_SIZE` undeclared (first use in this function)

Немного статистики об окружающей среде:

OS: Linux Mint 21.2 x86_64
Kernel: 5.15.0-112-generic
GLIBC Version: GNU C Library (Ubuntu GLIBC 2.35-0ubuntu3.8) stable release 2.35
Compiler: gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu1~22.04)

Запуск grep -r FALLOC_FL_KEEP_SIZE /usr/include вернулся:

/usr/include/linux/falloc.h:#define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */
/usr/include/linux/falloc.h: * with fallocate. Flag FALLOC_FL_KEEP_SIZE should cause the inode

🤔 А знаете ли вы, что...
C имеет строгую типизацию, что способствует обнаружению ошибок во время компиляции.


1
67
1

Ответ:

Решено

Проблема в том, что вы делали #include <stdio.h> раньше #define _GNU_SOURCE. Как есть, я получаю ту же ошибку, что и вы, но после ее исправления все компилируется нормально. Для man 7 Feature_test_macros:

       NOTE: In order to be effective, a feature test macro must be
       defined before including any header files.  This can be done
       either in the compilation command (cc -DMACRO=value) or by
       defining the macro within the source code before including any
       headers.  The requirement that the macro must be defined before
       including any header file exists because header files may freely
       include one another.  Thus, for example, in the following lines,
       defining the _GNU_SOURCE macro may have no effect because the
       header <abc.h> itself includes <xyz.h> (POSIX explicitly allows
       this):

           #include <abc.h>
           #define _GNU_SOURCE
           #include <xyz.h>