GCC:为POSIX函数获取警告隐式函数声明,但该函数已声明

时间:2019-06-10 14:02:47

标签: c gcc compiler-warnings

我遇到了一个非常奇怪的问题。我在其中一项功能中使用 readlink

#include <unistd.h>

#include "../logger/logger.h"
#include "../string/string.h"

bool path_retrieve_executable_filepath(char exe_filepath[PATH_MAX_SIZE]) {
    string_copy(exe_filepath, PATH_MAX_SIZE, "");
    ssize_t len = readlink("/proc/self/exe", exe_filepath, PATH_MAX_SIZE - 1);
    if (len == -1) {
        log_error("Unable to retrieve executable directory");
        return false;
    }
    exe_filepath[len] = '\0';
    return true;
}

我已经在我的Makefile中定义并将_POSIX1_SOURCE设置为2:

CC       = gcc
DEFINES  = -DVK_NO_PROTOTYPES -D_POSIX1_SOURCE=2 -DDEBUG
# compiling flags here
# CFLAGS = -std=c11 -flto -O3 -march=native
CFLAGS = -std=c11 -Wall -g3

LINKER   = gcc -o
# linking flags here
LFLAGS   = -flto -O3 -march=native -lm -lSDL2

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
    @mkdir -p $(dir $@)
    @$(CC) $(CFLAGS) $(DEFINES) $(INCLUDE_DIRS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

gcc给我隐式函数声明警告,尽管a)应该定义readlink函数b)一旦运行该函数即可正常工作。警告的详细信息:

src/file/path.c:10:19: warning: implicit declaration of function ‘readlink’; did you mean ‘realloc’? [-Wimplicit-function-declaration]
     ssize_t len = readlink("/proc/self/exe", exe_filepath, PATH_MAX_SIZE - 1);
                   ^~~~~~~~
                   realloc

关于如何摆脱警告的任何想法?我做错了什么吗?

0 个答案:

没有答案