简体   繁体   中英

GCC warning with std=c11 arg

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM