Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "sigemptyset was not declared in this scope" when using C+11 and Newlib

We are catching compiler errors when using sigemptyset on Cygwin under Newlib. The error occurs with a C++ compiler, but only when -std=XXX is used. Without a standard option, the test program compiles and executes as expected.

The test program is below, and the Cygwin header of interest follows. I don't see anything suspicious in the Cygwin header.

I've tried tricks like #define _GNU_SOURCE and #define _XOPEN_SOURCE 700. I've also tried tricks like using the global and std namespaces. Related, see What does -D_XOPEN_SOURCE do/mean? and Namespace issues in c++11?.

What is causing the compile failure and how do I fix it?


$ cat ~/test.cxx 
#include <signal.h>

int main(int argc, char* argv[])
{
    struct sigaction new_handler;
    return sigemptyset(&new_handler.sa_mask);
}

Without a -std=XXX, it results in:

$ g++ -c test.cxx
$

With a -std=XXX, it results in:

$ g++ -std=c++03 -c test.cxx
test.cxx: In function int main(int, char**):
test.cxx:6:44: error: sigemptyset was not declared in this scope
  return sigemptyset(&new_handler.sa_mask);

And when trying to use sigemptyset in the global namespace:

$ g++ -std=c++03 -c test.cxx
test.cxx: In function ‘int main(int, char**)’:
test.cxx:6:12: error: ‘::sigemptyset’ has not been declared
     return ::sigemptyset(&new_handler.sa_mask);
            ^

Things get worse when using -std=gnu++03 and friends.

like image 688
jww Avatar asked Oct 04 '16 05:10

jww


2 Answers

The function is an extension over the ISO C standard.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigemptyset.html

as such is protected on /usr/include/sys/signal.h by
__XSI_VISIBLE >= 4

see /usr/include/sys/features.h for details.

As defaults the largest definition set is used, but -std=XXX reduces the definition scope

like image 101
matzeri Avatar answered Nov 01 '22 16:11

matzeri


The issue was worked through at Botan 2.1.0 does not compile under Cygwin 2.8.0 with g++ 5.4.0. Here are the two comments of interest.

First, from noloader:

Cygwin uses Newlib, not GNU's libstdc++. When there's no -std=c++XX, current GCC defaults to -std=gnu++11 (GCC 6 changes to gnu++14 by default). I believe GNU sources ensures expected functions, like sigaction, are available.

You might consider trying -D_XOPEN_SOURCE=600 or -D_XOPEN_SOURCE=700.

Also see C++ and feature guards Warning Question on the Newlib mailing list.

Second, from SideChannel:

Thanks to @noloader. Until now -std=c++11 was set in Makefile. The important info is in above mentioned thread on the Newlib mailing list. Yaakov Selkowitz wrote:

G++ defines _GNU_SOURCE on glibc targets, meaning that -std=c++NN is, contrary to the documentation, not strict ISO C++:

So, applying the patch #987 AND setting -std=gnu++11 works for me. I did not try the other -D options (I think the other fact is more fundamental). Summarizing, @randombit please apply the PR #987 and set -std=gnu++11 for gcc under Cygwin.

like image 1
jww Avatar answered Nov 01 '22 16:11

jww