Why does -std=c++11 hide certain function calls

John Selbie jselbie@gmail.com
Wed Sep 5 05:56:00 GMT 2018

Updating Stuntman (Open source Stun Server) from C++ to modern C++.  I ran
into an issue.
This code:

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netdb.h>
    int some_networking_code()
       addrinfo* addr = NULL;
       int flags = AI_NUMERICHOST;
       return 0;

Compiles fine everywhere: with: g++ foo.cpp -c

With this: g++ foo.cpp -c -std=c++11
It compiles fine everywhere else, except CygWin.  Output on Cygwin:

    foo.cpp: In function ‘int some_networking_code()’:
    foo.cpp:8:4: error: ‘addrinfo’ was not declared in this scope
    foo.cpp:9:16: error: ‘AI_NUMERICHOST’ was not declared in this scope

Digging in further, I see that /usr/include/netdb.h has macros for blocking
declarataion of these items:

    #if __POSIX_VISIBLE >= 200112 && !defined(__INSIDE_CYGWIN_NET__)
    struct addrinfo {
      int             ai_flags;             /* input flags */
      … <deleted for brevity>

And when the macros are inspected with:  g++ foo.cpp -c -std=c++11 -dM -E |
We can see that:
    #define __POSIX_VISIBLE 0

Which explains the compiler output.
Searching the mailing list archives, I see I'm not the first to observe
something this:

Yes, switching to -std=gnu++11 or adding  -D_DEFAULT_SOURCE to the command
line line works.

But I don't understand why the need to enforce these extensions to get
access to some of the most common unix libraries? When the goal of Cygwin
is to allow Unix code to be rebuilt for Windows easily, it feels dirty that
my Makefile will need Cygwin specific adjustments.  Why not just take out
the __POSIX_VISIBLE>=200112 check altogether?  If I'm missing the point of
something obvious, I'd be open to be educated on this.


Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

More information about the Cygwin mailing list