Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC warning with std=c11 arg

Tags:

c

gcc

pthreads

c11

Here is a little C source code using pthread_kill() call:

#include <stdlib.h>
#include <pthread.h>
#include <signal.h>

int main(int argc, char *argv[])
{
        pthread_t th = NULL;

        pthread_kill(th, 0);

        return 0;
}

Gcc compilation produces various results depending on -std argument value (see below). I don't understand these different behaviors. I didn't get interesting informations into man pages except pthread_kill() is POSIX.1-2008 compliant.

Environment: Linux 3.2 64bits. GCC 4.7.2.

With -std=c11

gcc main.c -std=c11 -pthread

I get an implicit declaration:

main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]

With -std=c99

gcc main.c -std=c99 -pthread

Same result as -std=c11:

main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]

With -std=c90

gcc main.c -std=c90 -pthread

It simply works without any errors/warnings.

Thank you for your feedbacks.

like image 591
Flow Avatar asked Dec 13 '14 13:12

Flow


1 Answers

If you use a Posix feature, you need to define an appropriate feature test macro. See man feature_test_macros or the Posix standard.

If you don't define _POSIX_C_SOURCE to an appropriate value (depending on the minimum Posix level you require), then interfaces from that and subsequent Posix standards will not be defined by standard library headers.

If you need Posix.1-2008, for example, you need to do this:

#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>

int main(int argc, char *argv[])
{
        pthread_t th = NULL;

        pthread_kill(th, 0);

        return 0;
}
like image 70
rici Avatar answered Nov 16 '22 23:11

rici