Symbol visibility problems with -std=
Yaakov Selkowitz
yselkowitz@cygwin.com
Wed May 13 14:08:00 GMT 2020
On Wed, 2020-05-13 at 13:21 +0300, Pavel Fedin via Cygwin wrote:
> While compiling various software packages for Cygwin i notice that very often i have to add something like #define _GNU_SOURCE to
> them in order to compile correctly. Meanwhile on Linux they compile with no problems at all. I've narrowed it down to -std=???
> option using a simple test case:
[snip]
> $ g++ test.cpp -o test - compiles OK
> $ g++ test.cpp -o test -std=c++14 - error: 'strdup' was not declared in this scope; did you mean 'strcmp'?
>
> By printing out predefined macros (-dM -E) i found out that -std=something option adds " #define __STRICT_ANSI__ 1" to builtin
> macros, but removes all *_SOURCE definitions, so _DEFAULT_SOURCE is not triggered any more.
That is what -std=c* is supposed to mean, that you are declaring strict
ISO-standard language conformance.
> I've compared the behavior with Linux system. On Linux -std=c++14 also defines __STRICT_ANSI__, but various *_SOURCE macros are not
> omitted.
That's because _GNU_SOURCE is defined unconditionally by g++ on Linux,
which overrides the __STRICT_ANSI__ defined by -std=c*. IIUC this is
done only to handle the use of non-ISO functions in libstdc++,
particularly in the implementation of newer C++ standards.
The bottom line is, if you are using POSIX C extensions, then you
shouldn't be declaring -std=c* without the appropriate _*_SOURCE.
> Isn't this a Cygwin bug?
Not really, just that our g++ is somewhat more strict. If anything,
allowing use of functions outside the declared standards (the behaviour
on Linux) is the bug, and it's been on my to-do list for a long time to
fix. Fixing this isn't as simple as removing the unconditional define;
the C library functions used by newer C++ need to be appropriately
guarded, and then those specific guards used in libstdc++. It was a
major project to get this working on Cygwin.
> By the way, clang does not suffer from this problem.
Clang also defines _GNU_SOURCE unconditionally when compiling C++, even
on Cygwin. Perhaps *that* should be considered the bug.
--
Yaakov
More information about the Cygwin
mailing list