linux 'memfd_create'的隐含宣告

t3psigkw  于 2022-11-02  发布在  Linux
关注(0)|答案(1)|浏览(133)

我试着在我的c代码中使用mman.h中的函数。代码用g++/clang++编译很好,但是当使用gcc/clang时,它说memfd_create没有被声明,但是代码仍然运行良好。
我试着用godbolt在线编译,结果和本地编译一样,所以我怀疑是我的设置有问题。有人知道为什么会这样吗?我用的是gcc 11.3和clang 14。


# include <stdint.h>

# include <stdio.h>

# define _GNU_SOURCE

# include <sys/mman.h>

int main()
{
    int32_t fd = memfd_create("", 0);

    if (fd == -1) 
    {
        printf("Error creating fd\n");
        return -1;  
    }   

    return 0;
}

编译警告:

main.c:9:15: warning: implicit declaration of function 'memfd_create' is invalid in C99 [-Wimplicit-function-declaration]
        int32_t fd = memfd_create("", 0);
nqwrtyyt

nqwrtyyt1#

_GNU_SOURCE必须在任何#include之前。请参阅man feature_test_macros


# define _GNU_SOURCE

# include <stdint.h>

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

# include <unistd.h>

# include <sys/mman.h>

相关问题