bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: bug-gnulib@gnu.org
Subject: Re: Missing extern "C" in count-one-bits.h?
Date: Fri, 21 Feb 2020 12:59:55 -0800	[thread overview]
Message-ID: <314f76bd-3892-b81d-9e7f-ea8865a5ac13@cs.ucla.edu> (raw)
In-Reply-To: <a546f1a8-d142-77ae-214a-ef72dd596cc0@polymtl.ca>

[-- Attachment #1: Type: text/plain, Size: 1182 bytes --]

On 2/20/20 12:15 PM, Simon Marchi wrote:
> The disagreement between the symbol names between count-one-bits.o and arm-get-next-pcs.o
> seems to show that there is a missing `extern "C"` in the count-one-bits.h header?

Thanks, I'll take your word for it. (I don't use C++.) I installed the 
attached patch into Gnulib. Apparently these modules have been used by C 
code only, until now.

> And apparently, gcc decided to compile the definition in the header and include
> it in arm-get-next-pcs.o, while clang did not.  Is that the compiler's choice,
> or is it because the macros and preprocessor conditionals in count-one-bits.h
> evaluated to something different for the two compilers?

It's a compiler's choice whether a function is inlined. When I call 
count_one_bits in C, gcc -O2 doesn't create a function for 
count_one_bits in the calling module; it simply issues the popcnt insn, 
or calls the appropriate GCC library function on platforms that don't 
have a popcnt insn. Perhaps you were compiling with some other level of 
optimization, or perhaps C++ does this differently; but regardless of 
how the compiler does it the resulting code is supposed to work of course.

[-- Attachment #2: 0001-Add-extern-C-to-count-one-bits.h-etc.patch --]
[-- Type: text/x-patch, Size: 3667 bytes --]

From 50da3fa2a9fde4ff42cf20d3994c1095060b4ff7 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 21 Feb 2020 12:41:33 -0800
Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=98extern=20"C"=E2=80=99=20to=20cou?=
 =?UTF-8?q?nt-one-bits.h=20etc.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This ports these .h files to C++.
Problem reported by Simon Marchi in:
https://lists.gnu.org/r/bug-gnulib/2020-02/msg00110.html
* lib/count-leading-zeros.h, lib/count-one-bits.h:
* lib/count-trailing-zeros.h: Add ‘extern "C"’.
---
 ChangeLog                  | 9 +++++++++
 lib/count-leading-zeros.h  | 8 ++++++++
 lib/count-one-bits.h       | 8 ++++++++
 lib/count-trailing-zeros.h | 8 ++++++++
 4 files changed, 33 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 8649b9110..89beae3d7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-02-21  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Add ‘extern "C"’ to count-one-bits.h etc.
+	This ports these .h files to C++.
+	Problem reported by Simon Marchi in:
+	https://lists.gnu.org/r/bug-gnulib/2020-02/msg00110.html
+	* lib/count-leading-zeros.h, lib/count-one-bits.h:
+	* lib/count-trailing-zeros.h: Add ‘extern "C"’.
+
 2020-02-19  Bruno Haible  <bruno@clisp.org>
 
 	uninorm/decompose-internal: Avoid "no previous prototype" warning.
diff --git a/lib/count-leading-zeros.h b/lib/count-leading-zeros.h
index b548754e1..7e88c8cb9 100644
--- a/lib/count-leading-zeros.h
+++ b/lib/count-leading-zeros.h
@@ -30,6 +30,10 @@ _GL_INLINE_HEADER_BEGIN
 # define COUNT_LEADING_ZEROS_INLINE _GL_INLINE
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
    expand to code that computes the number of leading zeros of the local
    variable 'x' of type TYPE (an unsigned integer type) and return it
@@ -108,6 +112,10 @@ count_leading_zeros_ll (unsigned long long int x)
                        unsigned long long int);
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 _GL_INLINE_HEADER_END
 
 #endif /* COUNT_LEADING_ZEROS_H */
diff --git a/lib/count-one-bits.h b/lib/count-one-bits.h
index 78770e424..eea56d859 100644
--- a/lib/count-one-bits.h
+++ b/lib/count-one-bits.h
@@ -30,6 +30,10 @@ _GL_INLINE_HEADER_BEGIN
 # define COUNT_ONE_BITS_INLINE _GL_INLINE
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* Expand to code that computes the number of 1-bits of the local
    variable 'x' of type TYPE (an unsigned integer type) and return it
    from the current function.  */
@@ -129,6 +133,10 @@ count_one_bits_ll (unsigned long long int x)
   COUNT_ONE_BITS (__builtin_popcountll, __popcnt64, unsigned long long int);
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 _GL_INLINE_HEADER_END
 
 #endif /* COUNT_ONE_BITS_H */
diff --git a/lib/count-trailing-zeros.h b/lib/count-trailing-zeros.h
index 2169f6262..1eb5fb919 100644
--- a/lib/count-trailing-zeros.h
+++ b/lib/count-trailing-zeros.h
@@ -30,6 +30,10 @@ _GL_INLINE_HEADER_BEGIN
 # define COUNT_TRAILING_ZEROS_INLINE _GL_INLINE
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
    expand to code that computes the number of trailing zeros of the local
    variable 'x' of type TYPE (an unsigned integer type) and return it
@@ -100,6 +104,10 @@ count_trailing_zeros_ll (unsigned long long int x)
                         unsigned long long int);
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 _GL_INLINE_HEADER_END
 
 #endif
-- 
2.24.1


  reply	other threads:[~2020-02-21 21:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20 20:15 Missing extern "C" in count-one-bits.h? Simon Marchi
2020-02-21 20:59 ` Paul Eggert [this message]
2020-02-21 22:47   ` Simon Marchi via Gnulib discussion list

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=314f76bd-3892-b81d-9e7f-ea8865a5ac13@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=bug-gnulib@gnu.org \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).