Question

I've been trying to learn C the hard way - issuing basic calls, watching them fail, and trying to figure out what's going on. At the moment I'm working with fork() and the interoperation between parent and child processes. Everything has worked as expected, except tonight when I introduced -ansi into my CFLAGS. At that point, I got these messages from make:

waitpid.c: In function ‘wait_for_child’:
waitpid.c:63:3: warning: implicit declaration of function ‘strsignal’ [-Wimplicit-function-declaration]
   printf("child exited with signal %s\n", strsignal(WTERMSIG(child_status)));
   ^
waitpid.c:63:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
waitpid.c:64:3: warning: implicit declaration of function ‘WCOREDUMP’ [-Wimplicit-function-declaration]
   if ( WCOREDUMP(child_status) ) {
   ^

The output of gcc -C -E is radically different depending on whether I specify -ansi as well. Both show expansions of WIFEXITED, WEXITSTATUS, WIFSIGNALED, and WTERMSIG, but only the non-ANSI version expands WCOREDUMP. It's also the one that gets a declaration of strsignal from sys/wait.h.

I infer that strsignal and WCOREDUMP aren't supported in ANSI C, though I can't find anything explicit yet that says so. Is there some other way to perform their functions? Or am I missing something basic?

Full code is at http://pastebin.com/hQ2pR7fF. Thanks.

Was it helpful?

Solution

Neither strsignal nor WCOREDUMP are specified in C (checked C99). So you won't get them out of the box if you put GCC strictly in ANSI mode (which gets you C90).

strsignal is defined in POSIX, but WCOREDUMP isn't (see wait for POSIX, the Linux manpage states that it's not POSIX), so you can't get that even if you add POSIX extensions.

Easiest way to get both would be to define _GNU_SOURCE before including any headers (or directly on the compile command line), for Linux.
For Mac OS X, you probably (don't have that available) need _BSD_SOURCE and a _POSIX_C_SOURCE defined to something at or above 200809L.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top