问题
I have this minimal helloworld, extended with an include of ucontext.h
:
#include <ucontext.h>
#include <stdio.h>
int main(int argc, char** argv) {
printf ("hello world!\n");
return 0;
}
It compiles without warning with gcc-4.9 (gcc -c hw.c -Wall
).
But if I switch to the c11 standard (gcc -std=c11 -c hw.c -Wall
), I get the following error:
$ gcc -std=c11 -c hw.c -Wall
In file included from /usr/include/ucontext.h:26:0,
from hw.c:1:
/usr/include/x86_64-linux-gnu/sys/ucontext.h:137:5: error: unknown type name ‘stack_t’
stack_t uc_stack;
^
My first idea is that glibc doesn't support c11. Googling for that didn't reveal usable information. What is the case?
(I use glibc-2.19 with gcc-4.9. It is a debian jessie, amd64.)
回答1:
-std=c11
is C11 standard compliant mode. <ucontext.h>
isn't strictly part of C11 (see Stas's answer).
To use these headers either use extension mode -std=gnu11
or define appropriate macro depending on which platform do you intend to support (_POSIX_C_SOURCE
, _BSD_SOURCE
, _XOPEN_SOURCE
, _GNU_SOURCE
or maybe others).
See this page for more info about feature-enabling macros.
回答2:
It seems <ucontext.h>
functions are deprecated, because they use deprecated C feature. So they can't be used in standard compliant C code. See the rationale:
With the incorporation of the ISO/IEC 9899:1999 standard into this specification it was found that the ISO C standard (Subclause 6.11.6) specifies that the use of function declarators with empty parentheses is an obsolescent feature. Therefore, using the function prototype:
void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);
is making use of an obsolescent feature of the ISO C standard. Therefore, a strictly conforming POSIX application cannot use this form. Therefore, use of getcontext(), makecontext(), and swapcontext() is marked obsolescent.
So, it is not directly related to C11. For example, I can't compile your example with clang
on Mac OS X at all.
It was deprecated in C99 standard:
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
来源:https://stackoverflow.com/questions/32426835/glibc-error-in-ucontext-h-but-only-with-std-c11