1

I encountered very strange issue. I am using readlink in one of my functions:

#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;
}

I have defined and set _POSIX1_SOURCE to 2 in my Makefile:

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!"

The gcc is giving me implicit-function-declaration warning although a) the readlink function should be defined b) the function is working correctly once run. The details of the warning:

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

Any ideas on how to get rid of the warning? Am I doing anything incorrectly?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jimmy
  • 276
  • 1
  • 8
  • 1
    Using `-std=c11` means POSIX functions are not declared unless you explicitly request them. Either use `-STD=gnu11` or set `_XOPEN_SOURCE` to `700`. – Jonathan Leffler Jun 10 '19 at 14:07
  • The correct feature test macros are listed in the [man page](http://man7.org/linux/man-pages/man2/readlink.2.html) (assuming you're using Linux with glibc). – Shawn Jun 10 '19 at 14:08
  • @JonathanLeffler I thought that only applied to POSIX functions which they smeared inside standard library headers? unistd.h isn't a standard lib so why would `-std=c11` block it? – Lundin Jun 10 '19 at 14:10
  • Anyway, RTFM suggest that either of the following will do `_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE >= 200112L`. – Lundin Jun 10 '19 at 14:11
  • `_POSIX1_SOURCE` *isn't* a glibc [feature test](http://man7.org/linux/man-pages/man7/feature_test_macros.7.html) though so maybe you're using some other OS? – Shawn Jun 10 '19 at 14:12
  • @JonathanLeffler I will try it out. Thanks! I thought when I define _POSIX1_SOURCE it would be OK. – Jimmy Jun 10 '19 at 14:12
  • @JonathanLeffler A simple test confirms your diagnosis: Adding `-std=c11` causes the missing declaration, while removing it does not. – Tom Karzes Jun 10 '19 at 14:12
  • @Lundin — I don’t know why GCC behaves as it does. I do know that it does behave as it does. – Jonathan Leffler Jun 10 '19 at 14:16
  • @JonathanLeffler Your first proposed solution solved my issue. If you post it as an answer I can mark it as solved. – Jimmy Jun 10 '19 at 14:16
  • Apologies for the mis-capitalized `STD`; cell phones have a mind of their own. I've closed it as a duplicate of another equivalent question. The basic problem is the same; the question was asked in 2010, so it references C99 rather than C11, and it also uses `_XOPEN_SOURCE 600` corresponding to POSIX 2004 rather than `_XOPEN_SOURCE 700` corresponding to POSIX 2008 (and later revisions). See [The Compilation Environment](http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02) for more information on symbols to be defined by programmers to get POSIX features declared. – Jonathan Leffler Jun 10 '19 at 14:26
  • Incidentally, I'm not aware of a feature macro `_POSIX1_SOURCE`. The link in my previous comment mentions `_POSIX_SOURCE` from POSIX 90, but that has been superseded by `_POSIX_C_SOURCE` and/or `_XOPEN_SOURCE`. You should usually use `_XOPEN_SOURCE`; it is rare to want POSIX but not XSI (X/Open) features. – Jonathan Leffler Jun 10 '19 at 14:50

0 Answers0