5

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.

8
  • 1
    did you mean -lpthread instead of -pthread? Commented Dec 13, 2014 at 13:28
  • 2
    @SouravGhosh They're exactly the same. Well, actually -pthread should be preferred. Commented Dec 13, 2014 at 13:33
  • 3
    Another thing to be aware of is the -std=gnu99 or e.g. -std=gnu11 flags that gives you extension to the respective C standard, which includes library features such as posix functions if you are using glibc on linux. Commented Dec 13, 2014 at 14:15
  • @nos Actually I'm not interested by GNU extensions and I prefer compile with strict ISO specification (I removed -pedantic* args for the examples). Commented Dec 13, 2014 at 14:24
  • 1
    @mafso Sure. But regardless of the name being 'gnu', facts are that -std=gnu99 enables the posix interfaces if you are using glibc on linux. You are absolutely right about the core language being different. Commented Dec 14, 2014 at 0:04

1 Answer 1

9

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that was the solution. However, how am I supposed to know this function is a Posix feature? pthread_create() is also Posix (1.2001) but I didn't ever specify it. According to your documentation, I didn't see "feature_test_macros" mention inside pthread_kill() man page.
@Flow - I think the point is, pthreads are not part of ISO C. "how am I supposed to know this function is a Posix feature?" - Pthreads is short for 'POSIX threads'. It is a bit strange that including the header doesn't expose the functionality, but pthreads still affect compiler options / linking in some cases.
@Flow: Most manpages have a section on feature test macros; if pthread_create is missing one in your system, I would say that is a documentation bug. However, you can derive the value from the CONFORMING TO line, using the chart in the posix standard whose link is in the answer. I just use 200809L all the time (I put -D_POSIX_C_SOURCE=200809L in my C flags in my Makefile) but it is probably "better" to be precise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.