Bruno Haible wrote:
Lorenzo Bettini wrote:
I tried it and got no problem, but as far as I understand this is not a
proof, since
#if defined HAVE_DECL_STRDUP && !HAVE_DECL_STRDUP && !defined strdup
/* Duplicate S, returning an identical malloc'd string. */
extern char *strdup (const char *s);
#endif
so the extern definition is not parsed, since it finds the definition of
strdup in string.h (included in strdup.h).
Yes, the absence of a failure doesn't prove anything.
However, I tried this test program:
#include <iostream>
extern char *strdup (const char *s);
int main()
{
char *copy = strdup("foobar");
std::cout << copy << std::endl;
return 0;
}
and got no problem
This doesn't prove anything either: <iostream> may include <string.h>, which
may have an extern "C" declaration for strdup.
Can you tell us the list of .h files that you want to use from C++? It's
easier to make them C++ safe than to consider when exactly the extern "C"
will be needed or not.
You're right: iostream somehow includes string.h: if I remove such
include then I get the linkage error with this program (I forced in
strdup.c the use of the version not in string.h)
extern char *strdup (const char *s);
int main()
{
char *copy = strdup("foobar");
return (copy != 0 ? 0 : 1);
}
I thus included a possible patch.
thanks
Lorenzo
--
+-----------------------------------------------------+
| Lorenzo Bettini ICQ# lbetto, 16080134 |
| PhD in Computer Science, DSI, Univ. di Firenze |
| Florence - Italy (GNU/Linux User # 158233) |
| http://www.lorenzobettini.it |
| http://tronprog.blogspot.com BLOG |
| http://www.purplesucker.com Deep Purple Cover Band |
| http://www.gnu.org/software/src-highlite |
| http://www.gnu.org/software/gengetopt |
| http://www.gnu.org/software/gengen |
| http://doublecpp.sourceforge.net |
+-----------------------------------------------------+
Index: lib/strdup.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/strdup.h,v
retrieving revision 1.3
diff -U3 -r1.3 strdup.h
--- lib/strdup.h 14 May 2005 06:03:58 -0000 1.3
+++ lib/strdup.h 19 Dec 2006 14:49:15 -0000
@@ -22,8 +22,18 @@
#include <string.h>
#if defined HAVE_DECL_STRDUP && !HAVE_DECL_STRDUP && !defined strdup
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
/* Duplicate S, returning an identical malloc'd string. */
extern char *strdup (const char *s);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
#endif
#endif /* STRDUP_H_ */