Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit declaration using -std=c99

Tags:

c

gcc

I'm getting this warning: (-std=c99 -pedantic)

warning: implicit declaration of function ‘strndup’ [-Wimplicit-function-declaration]

but I'm importing these libs:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

So what?! :(


// file.c:
    #include "file.h"
    strndup(...)
// file.h:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
like image 838
Fabricio Avatar asked Feb 06 '12 22:02

Fabricio


3 Answers

The issue is your usage of the -std=c99 option. Since strndup() isn't part of C99, and you're asking the compiler to go into standards compliant mode, it won't provide the prototype for it. It still links of course, because your C library has it.

While you may be able to coax gcc into providing it by specifying feature macros yourself, I'd say it doesn't make much sense to be in C99 compliance mode and ask for GNU extensions for example. gcc already provides a mode for this, which will solve your warning: -std=gnu99.

like image 101
FatalError Avatar answered Nov 05 '22 04:11

FatalError


My man strndup says

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       strdup():
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
       strndup():
           Since glibc 2.10:
               _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
           Before glibc 2.10:
               _GNU_SOURCE
       strdupa(), strndupa(): _GNU_SOURCE

So I'd need to, eg, #define _POSIX_C_SOURCE 200809L before the first #include in your file.
see man 7 feature_test_macros

like image 21
pmg Avatar answered Nov 05 '22 04:11

pmg


strndup is a GNU extension, so you need to compile with -D_GNU_SOURCE on the command line, or stick a #define _GNU_SOURCE 1 in your source files before the #include lines

like image 8
Chris Dodd Avatar answered Nov 05 '22 05:11

Chris Dodd