Don't export symbols from static MSVC .obj files.

Suggested by Dmitry Bely <dmitry.bely@gmail.com> in
<https://lists.gnu.org/archive/html/bug-gnu-libiconv/2023-08/msg00002.html>.

* include/export.h: Add a copyright notice. Set LIBICONV_DLL_EXPORTED to
empty on MSVC when DLL_EXPORT is not defined.
* Makefile.devel (include/iconv.h.build.in): Filter out the second
copyright notice.
* configure.ac (DLL_VARIABLE): Test DLL_EXPORT, not _DLL.
* lib/relocatable.h: From gnulib:
(RELOCATABLE_DLL_EXPORTED): Don't use __declspec(dllexport) when
creating static .obj files with MSVC.
* libcharset/include/export.h: Add a copyright notice. Set
LIBCHARSET_DLL_EXPORTED to empty on MSVC when DLL_EXPORT is not defined.
* libcharset/Makefile.devel (include/libcharset.h.build.in,
include/localcharset.h.build.in): Filter out the second copyright
notice.
diff --git a/ChangeLog b/ChangeLog
index 5d67a93..491506f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2023-09-06  Bruno Haible  <bruno@clisp.org>
+
+	Don't export symbols from static MSVC .obj files.
+	Suggested by Dmitry Bely <dmitry.bely@gmail.com> in
+	<https://lists.gnu.org/archive/html/bug-gnu-libiconv/2023-08/msg00002.html>.
+	* include/export.h: Add a copyright notice. Set LIBICONV_DLL_EXPORTED to
+	empty on MSVC when DLL_EXPORT is not defined.
+	* Makefile.devel (include/iconv.h.build.in): Filter out the second
+	copyright notice.
+	* configure.ac (DLL_VARIABLE): Test DLL_EXPORT, not _DLL.
+	* lib/relocatable.h: From gnulib:
+	(RELOCATABLE_DLL_EXPORTED): Don't use __declspec(dllexport) when
+	creating static .obj files with MSVC.
+
 2023-08-30  Bruno Haible  <bruno@clisp.org>
 
 	Recognize the *-*-windows* config triplets introduced on 2023-06-26.
diff --git a/Makefile.devel b/Makefile.devel
index f20755f..22eae3a 100644
--- a/Makefile.devel
+++ b/Makefile.devel
@@ -136,9 +136,10 @@
 
 
 include/iconv.h.build.in : include/iconv.h.in include/export.h
-	sed -e 's/extern \([^"]\)/extern LIBICONV_DLL_EXPORTED \1/' \
-	    -e '/_LIBICONV_VERSION/r include/export.h' \
-	  < $< > $@
+	cat $< \
+	| sed -e 's/extern \([^"]\)/extern LIBICONV_DLL_EXPORTED \1/' \
+	      -e '/_LIBICONV_VERSION/r include/export.h' \
+	| sed -e '/_LIBICONV_VERSION/,/[*][/]$$/{/_LIBICONV_VERSION/!d;}' > $@
 
 
 lib/aliases.h lib/canonical.h lib/canonical_local.h : lib/encodings.def lib/encodings_local.def lib/genaliases.c
diff --git a/configure.ac b/configure.ac
index 408cf51..1b6ce92 100644
--- a/configure.ac
+++ b/configure.ac
@@ -170,7 +170,9 @@
 dnl because
 dnl 1. when we install a shared library, we must arrange to export
 dnl    auxiliary pointer variables for every exported variable,
-dnl 2. when we install a shared library and a static library simultaneously,
+dnl 2. when we install a shared library and a static library simultaneously
+dnl    (e.g. when the user overrides the 'disable-static' default above by
+dnl    passing the configure options --enable-shared --enable-static),
 dnl    the include file specifies __declspec(dllimport) and therefore we
 dnl    must arrange to define the auxiliary pointer variables for the
 dnl    exported variables _also_ in the static library.
@@ -192,8 +194,13 @@
 AC_SUBST([DLL_VARIABLE])
 
 AH_BOTTOM([
-/* On Windows, variables that may be in a DLL must be marked specially.  */
-#if defined _MSC_VER && defined _DLL
+/* On Windows, variables that may be in a DLL must be marked specially.
+   The symbols marked with DLL_VARIABLE should be exported if and only if the
+   object file gets included in a DLL.  Libtool, on Windows platforms, defines
+   the C macro DLL_EXPORT (together with PIC) when compiling for a DLL
+   and does not define it when compiling an object file meant to be linked
+   statically into some executable.  */
+#if defined _MSC_VER && defined DLL_EXPORT
 # define DLL_VARIABLE __declspec (dllimport)
 #else
 # define DLL_VARIABLE
diff --git a/include/export.h b/include/export.h
index e9fb806..5354e7a 100644
--- a/include/export.h
+++ b/include/export.h
@@ -1,8 +1,36 @@
+/* Control of exported symbols from libiconv.
+   Copyright (C) 2005-2023 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #if @HAVE_VISIBILITY@ && BUILDING_LIBICONV
-#define LIBICONV_DLL_EXPORTED __attribute__((__visibility__("default")))
+# define LIBICONV_DLL_EXPORTED __attribute__((__visibility__("default")))
 #elif defined _MSC_VER && BUILDING_LIBICONV
-#define LIBICONV_DLL_EXPORTED __declspec(dllexport)
+/* When building with MSVC, exporting a symbol means that the object file
+   contains a "linker directive" of the form /EXPORT:symbol.  This can be
+   inspected through the "objdump -s --section=.drectve FILE" or
+   "dumpbin /directives FILE" commands.
+   The symbols from this file should be exported if and only if the object
+   file gets included in a DLL.  Libtool, on Windows platforms, defines
+   the C macro DLL_EXPORT (together with PIC) when compiling for a DLL
+   and does not define it when compiling an object file meant to be linked
+   statically into some executable.  */
+# if defined DLL_EXPORT
+#  define LIBICONV_DLL_EXPORTED __declspec(dllexport)
+# else
+#  define LIBICONV_DLL_EXPORTED
+# endif
 #else
-#define LIBICONV_DLL_EXPORTED
+# define LIBICONV_DLL_EXPORTED
 #endif
diff --git a/lib/relocatable.h b/lib/relocatable.h
index 57520b8..0edf0e9 100644
--- a/lib/relocatable.h
+++ b/lib/relocatable.h
@@ -1,5 +1,5 @@
 /* Provide relocatable packages.
-   Copyright (C) 2003, 2005, 2008-2017 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2005, 2008-2023 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify it
@@ -32,7 +32,20 @@
 #if HAVE_VISIBILITY && BUILDING_DLL
 # define RELOCATABLE_DLL_EXPORTED __attribute__((__visibility__("default")))
 #elif defined _MSC_VER && BUILDING_DLL
-# define RELOCATABLE_DLL_EXPORTED __declspec(dllexport)
+/* When building with MSVC, exporting a symbol means that the object file
+   contains a "linker directive" of the form /EXPORT:symbol.  This can be
+   inspected through the 'objdump -s --section=.drectve FILE' or
+   'dumpbin /directives FILE' commands.
+   The symbols from this file should be exported if and only if the object
+   file gets included in a DLL.  Libtool, on Windows platforms, defines
+   the C macro DLL_EXPORT (together with PIC) when compiling for a DLL
+   and does not define it when compiling an object file meant to be linked
+   statically into some executable.  */
+# if defined DLL_EXPORT
+#  define RELOCATABLE_DLL_EXPORTED __declspec(dllexport)
+# else
+#  define RELOCATABLE_DLL_EXPORTED
+# endif
 #else
 # define RELOCATABLE_DLL_EXPORTED
 #endif
diff --git a/libcharset/ChangeLog b/libcharset/ChangeLog
index 1c26d44..32c105f 100644
--- a/libcharset/ChangeLog
+++ b/libcharset/ChangeLog
@@ -1,3 +1,14 @@
+2023-09-06  Bruno Haible  <bruno@clisp.org>
+
+	Don't export symbols from static MSVC .obj files.
+	Suggested by Dmitry Bely <dmitry.bely@gmail.com> in
+	<https://lists.gnu.org/archive/html/bug-gnu-libiconv/2023-08/msg00002.html>.
+	* include/export.h: Add a copyright notice. Set LIBCHARSET_DLL_EXPORTED
+	to empty on MSVC when DLL_EXPORT is not defined.
+	* Makefile.devel (include/libcharset.h.build.in,
+	include/localcharset.h.build.in): Filter out the second copyright
+	notice.
+
 2023-05-21  Bruno Haible  <bruno@clisp.org>
 
 	Support creating shared libraries on Hurd/x86_64.
diff --git a/libcharset/Makefile.devel b/libcharset/Makefile.devel
index 7a991cd..f421a18 100644
--- a/libcharset/Makefile.devel
+++ b/libcharset/Makefile.devel
@@ -25,14 +25,18 @@
 	touch config.h.in
 
 include/libcharset.h.build.in : include/libcharset.h.in include/export.h
-	sed -e 's/extern \([^"]\)/extern LIBCHARSET_DLL_EXPORTED \1/' \
-	    -e '/#define _LIBCHARSET_H/r include/export.h' \
-	  < $< > $@
+	cat $< \
+	| sed -e 's/extern \([^"]\)/extern LIBCHARSET_DLL_EXPORTED \1/' \
+	      -e '/#define _LIBCHARSET_H/r include/export.h' \
+	| sed -e '/#define _LIBCHARSET_H/,/[*][/]$$/{/#define _LIBCHARSET_H/!d;}' \
+	  > $@
 
 include/localcharset.h.build.in : include/localcharset.h.in include/export.h
-	sed -e 's/extern \([^"]\)/extern LIBCHARSET_DLL_EXPORTED \1/' \
-	    -e '/#define _LOCALCHARSET_H/r include/export.h' \
-	  < $< > $@
+	cat $< \
+	| sed -e 's/extern \([^"]\)/extern LIBCHARSET_DLL_EXPORTED \1/' \
+	      -e '/#define _LOCALCHARSET_H/r include/export.h' \
+	| sed -e '/#define _LOCALCHARSET_H/,/[*][/]$$/{/#define _LOCALCHARSET_H/!d;}' \
+	  > $@
 
 totally-clean : force
 	rm -f autoconf/aclocal.m4 configure config.h.in include/libcharset.h.build.in include/localcharset.h.build.in
diff --git a/libcharset/include/export.h b/libcharset/include/export.h
index 413f3ae..280ee1b 100644
--- a/libcharset/include/export.h
+++ b/libcharset/include/export.h
@@ -1,8 +1,36 @@
+/* Control of exported symbols from libcharset.
+   Copyright (C) 2005-2023 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #if @HAVE_VISIBILITY@ && BUILDING_LIBCHARSET
-#define LIBCHARSET_DLL_EXPORTED __attribute__((__visibility__("default")))
+# define LIBCHARSET_DLL_EXPORTED __attribute__((__visibility__("default")))
 #elif defined _MSC_VER && BUILDING_LIBCHARSET
-#define LIBCHARSET_DLL_EXPORTED __declspec(dllexport)
+/* When building with MSVC, exporting a symbol means that the object file
+   contains a "linker directive" of the form /EXPORT:symbol.  This can be
+   inspected through the "objdump -s --section=.drectve FILE" or
+   "dumpbin /directives FILE" commands.
+   The symbols from this file should be exported if and only if the object
+   file gets included in a DLL.  Libtool, on Windows platforms, defines
+   the C macro DLL_EXPORT (together with PIC) when compiling for a DLL
+   and does not define it when compiling an object file meant to be linked
+   statically into some executable.  */
+# if defined DLL_EXPORT
+#  define LIBCHARSET_DLL_EXPORTED __declspec(dllexport)
+# else
+#  define LIBCHARSET_DLL_EXPORTED
+# endif
 #else
-#define LIBCHARSET_DLL_EXPORTED
+# define LIBCHARSET_DLL_EXPORTED
 #endif