4

The linux platform is Ubuntu 12.04

I have the following headers included in my source code:

#include <unistd.h>
#include <signal.h>
#include <ucontext.h>

...

When I compile it however, it complains /usr/include/x86_64-linux-gnu/sys/ucontext.h:139:5: error: unknown type name 'stack_t'

I googled and found that stack_t should be defined in signal.h, but here it doesn't seem to be defined?

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30

2 Answers2

10

This is meant to be a comment but I cannot make it readable there. Sorry.

Did you #define one of the following:

 _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • Thanks! defining `_BSD_SOURCE` fixed my problem. I guess my question now is why is this needed to enable `stack_t` on linux? – Jimmy Lu Dec 26 '13 at 03:33
  • 2
    @BeyondSora: `stack_t` is non-standard. Anything that is non-standard are enabled by using [feature test macros](http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html). – Jesse Good Dec 26 '13 at 03:51
  • 3
    You may be using `-std=c99` or `-std=c11`; you may be better off using `-std=gnu99` or `-std=gnu11`. Or you can add an appropriate POSIX define (`#define _XOPEN_SOURCE 700` for example), or (as you seem to have chosen) `#define _BSD_SOURCE`. Deciding when to make the extra facilities available and when not to do so is complex. – Jonathan Leffler Dec 26 '13 at 04:11
3

According to SUS v2 (1997), stack_t should be defined in <signal.h> - http://pubs.opengroup.org/onlinepubs/7908799/xsh/ucontext.h.html

The types sigset_t and stack_t are defined as in .

http://pubs.opengroup.org/onlinepubs/007908799/xsh/signal.h.html

The header defines the stack_t type as a structure that includes at least the following members:

void     *ss_sp       stack base or pointer
size_t    ss_size     stack size
int       ss_flags    flags

The type is also listed in glibc documentation: http://www.gnu.org/software/libc/manual/html_node/Signal-Stack.html

Data Type: stack_t

This type is used in sigaltstack function, described as:

sigaltstack is the newer interface, and comes from 4.4 BSD. ...

And the official Linux man page for sigaltstack (version 2015-07-23) says: http://man7.org/linux/man-pages/man2/sigaltstack.2.html

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   sigaltstack():
       _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
       _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
       || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L

CONFORMING TO POSIX.1-2001, POSIX.1-2009, SUSv2, SVr4.

So, when you use glibc newer than 2.12, you must define some of macro to be able to use sigaltstack and stack_t. Since glibc 2.10 you can just define #define _GNU_SOURCE to enable _BSD_SOURCE and _POSIX_C_SOURCE = 200809L

osgx
  • 90,338
  • 53
  • 357
  • 513