std18 getpagesize:函数的隐式声明+嵌套的extern声明

问题描述 投票:0回答:1

我不明白为什么函数getpagesize在使用c18版本的gcc时给出了隐式声明函数的警告。

gcc test.c -Wall -std=c18

隐式声明函数'getpagesize'[-Wimplicit-function-declaration]

'getpagesize'的嵌套extern声明[-Wnested-externs]

  int BLOCKSIZE = getpagesize();

这是我包含的文件:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdint.h>
#include <errno.h>
c unix gcc-warning
1个回答
3
投票

使用-std=cXX而不是-std=gnuXX禁用一堆正常定义的feature test macros,包括提供getpagesize()的那些。从它的man page(假设你使用的是linux):

glibc的功能测试宏要求(参见feature_test_macros(7)):

  getpagesize():
       Since glibc 2.19:
           _DEFAULT_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
       From glibc 2.12 to 2.19:
           _BSD_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
       Before glibc 2.12:
           _BSD_SOURCE || _XOPEN_SOURCE >= 500

因此,在包含任何头文件之前,必须将适当的值定义为适当的值。或者只是使用-std=gnu18

编辑:此外,由于getpagesize()已过时且不标准,请考虑使用POSIX标准sysconf(_SC_PAGESIZE)

© www.soinside.com 2019 - 2024. All rights reserved.