From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id C8A1B387086B; Mon, 27 Apr 2020 13:28:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8A1B387086B Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbsupport: include cstdlib in common-defs.h X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: 991a3e2e9944a4b3a27bd989ac03c18285bd545d X-Git-Newrev: ff8885c3be6f42ed90a7b0ec0028fad26861cd94 Message-Id: <20200427132815.C8A1B387086B@sourceware.org> Date: Mon, 27 Apr 2020 13:28:15 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Apr 2020 13:28:16 -0000 https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ff8885c3be6f42ed90a7b0ec0028fad26861cd94 commit ff8885c3be6f42ed90a7b0ec0028fad26861cd94 Author: Simon Marchi Date: Mon Apr 27 09:19:48 2020 -0400 gdbsupport: include cstdlib in common-defs.h In PR 25731 [1], the following build failure was reported: ../../binutils-gdb/gdb/gdbtypes.c:1254:10: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'? = ((std::abs (stride) * element_count) + 7) / 8; ^~~~~~~~ abs /usr/include/stdlib.h:129:6: note: 'abs' declared here int abs(int) __pure2; ^ The original report was using: $ gcc -v Apple LLVM version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin15.6.0 Note that I was _not_ able to reproduce using: $ g++ --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 11.0.0 (clang-1100.0.33.17) Target: x86_64-apple-darwin19.3.0 The proposed fix is to include in addition to . Here's an excerpt of [2] relevant to this problem: These headers [speaking of the .h form] are allowed to also declare the same names in the std namespace, and the corresponding cxxx headers are allowed to also declare the same names in the global namespace: including definitely provides std::malloc and may also provide ::malloc. Including definitely provides ::malloc and may also provide std::malloc Since we use std::abs, we should not assume that our include of stdlib.h declares an `abs` function in the `std` namespace. If we replace the include of stdlib.h with cstdlib, then we fall in the opposite situation. A standard C++ library may decide to only put the declarations in the std namespace, requiring us to prefix all standard functions with `std::`. I'm not against that, but for the moment I think the safest way forward is to just include both. Note that I don't know what effect this patch can have on any stdlib.h fix provided by gnulib. [1] https://sourceware.org/bugzilla/show_bug.cgi?id=25731 [2] https://en.cppreference.com/w/cpp/header#C_compatibility_headers gdbsupport/ChangeLog: * common-defs.h: Include cstdlib.h. Diff: --- gdbsupport/ChangeLog | 4 ++++ gdbsupport/common-defs.h | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/gdbsupport/ChangeLog b/gdbsupport/ChangeLog index 78fbbe65c70..b071049ae73 100644 --- a/gdbsupport/ChangeLog +++ b/gdbsupport/ChangeLog @@ -1,3 +1,7 @@ +2020-04-27 Simon Marchi + + * common-defs.h: Include cstdlib.h. + 2020-04-20 Tom Tromey * scoped_mmap.h (scoped_mmap): Mark move constructor as noexcept. diff --git a/gdbsupport/common-defs.h b/gdbsupport/common-defs.h index e42d2b80c04..d3f5eafa455 100644 --- a/gdbsupport/common-defs.h +++ b/gdbsupport/common-defs.h @@ -84,7 +84,12 @@ #include #include + +/* Include both cstdlib and stdlib.h to ensure we have standard functions + defined both in the std:: namespace and in the global namespace. */ +#include #include + #include #include #include