export.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Exporting symbols from Cygwin shared libraries.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* There are four ways to build shared libraries on Cygwin:
  15. - Export only functions, no variables.
  16. This has the drawback of severely affecting the programming style in use.
  17. It does not let the programmer use full ANSI C. It lets one platform
  18. dictate the code style on all platforms. This is unacceptable.
  19. - Use the GNU ld --enable-auto-import option. It is the default on Cygwin
  20. since July 2005. But it has three fatal drawbacks:
  21. - It produces executables and shared libraries with relocations in the
  22. .text segment, defeating the principles of virtual memory.
  23. - For some constructs such as
  24. extern int var;
  25. int * const b = &var;
  26. it creates an executable that will give an error at runtime, rather
  27. than either a compile-time or link-time error or a working executable.
  28. (This is with both gcc and g++.) Whereas this code, not relying on
  29. auto-import:
  30. extern __declspec (dllimport) int var;
  31. int * const b = &var;
  32. gives a compile-time error with gcc and works with g++.
  33. - It doesn't work in some cases (references to a member field of an
  34. exported struct variable, or to a particular element of an exported
  35. array variable), requiring code modifications. Again one platform
  36. dictates code modifications on all platforms.
  37. This is unacceptable. Therefore we disable this option, through the
  38. woe32-dll.m4 autoconf macro.
  39. - Define a macro that expands to __declspec(dllexport) when building
  40. the library and to __declspec(dllimport) when building code outside
  41. the library, and use it in all header files of the library.
  42. This is acceptable if
  43. 1. the header files are unique to this library (not shared with
  44. other packages), and
  45. 2. the library sources are contained in one directory, making it easy
  46. to define a -DBUILDING_LIBXYZ flag for the library.
  47. Example:
  48. #ifdef BUILDING_LIBASPRINTF
  49. #define LIBASPRINTF_DLL_EXPORTED __declspec(dllexport)
  50. #else
  51. #define LIBASPRINTF_DLL_EXPORTED __declspec(dllimport)
  52. #endif
  53. We use this technique for the libintl and the libasprintf libraries.
  54. - Define a macro that expands to __declspec(dllimport) always, and use
  55. it in all header files of the library. Use an explicit export list for
  56. the library.
  57. This is acceptable if
  58. 1. the programming language is not C++ (because the name mangling of
  59. static struct/class fields and of variables in namespaces makes it
  60. hard to maintain an export list).
  61. The benefit of this approach is that the partitioning of the source files
  62. into libraries (which source file goes into which library) does not
  63. affect the source code; only the Makefiles reflect it.
  64. The performance loss due to the unnecessary indirection for references
  65. to variables from within the library defining the variable is acceptable.
  66. We use this technique for libgettextlib (because it contains many gnulib
  67. modules) and for libgettextsrc (because this makes it easy to move source
  68. code from an msg* program to libgettextsrc). The macro is called
  69. DLL_VARIABLE.
  70. This file allows building an explicit export list. You can either
  71. - specify the variables to be exported, and use the GNU ld option
  72. --export-all-symbols to export all function names, or
  73. - specify the variables and functions to be exported explicitly.
  74. Note: --export-all-symbols is the default when no other symbol is explicitly
  75. exported. This means, the use of an explicit export on the variables has
  76. the effect of no longer exporting the functions! - until the option
  77. --export-all-symbols is used. */
  78. /* IMP(x) is a symbol that contains the address of x. */
  79. #define IMP(x) _imp__##x
  80. /* Ensure that the variable x is exported from the library, and that a
  81. pseudo-variable IMP(x) is available. */
  82. #define VARIABLE(x) \
  83. /* Export x without redefining x. This code was found by compiling a \
  84. snippet: \
  85. extern __declspec(dllexport) int x; int x = 42; */ \
  86. asm (".section .drectve\n"); \
  87. asm (".ascii \" -export:" #x ",data\"\n"); \
  88. asm (".data\n"); \
  89. /* Allocate a pseudo-variable IMP(x). */ \
  90. extern int x; \
  91. void * IMP(x) = &x;