diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:10 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:10 -0700 |
commit | 8e34b3a8024c028dd9fd21d70525fc6d215efde5 (patch) | |
tree | 896a32f54abdf42ae3c1bb3c5d5627668b481ce4 /contrib/zlib_dll_FAQ.txt | |
parent | 13a294f044ef0a89b2dcbfbb5d4d4c792673348e (diff) | |
download | zlib-1.2.0.2.tar.gz zlib-1.2.0.2.tar.bz2 zlib-1.2.0.2.zip |
zlib 1.2.0.2v1.2.0.2
Diffstat (limited to 'contrib/zlib_dll_FAQ.txt')
-rw-r--r-- | contrib/zlib_dll_FAQ.txt | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/contrib/zlib_dll_FAQ.txt b/contrib/zlib_dll_FAQ.txt new file mode 100644 index 0000000..bc1fd59 --- /dev/null +++ b/contrib/zlib_dll_FAQ.txt | |||
@@ -0,0 +1,257 @@ | |||
1 | |||
2 | Frequently Asked Questions about ZLIB.DLL | ||
3 | |||
4 | |||
5 | This FAQ is about the design, the rationale, and the use of | ||
6 | ZLIB.DLL. If you have general questions about zlib, you should | ||
7 | check the file "FAQ" found in the zlib distribution, or at the | ||
8 | location http://www.gzip.org/zlib/zlib_faq.html | ||
9 | |||
10 | |||
11 | 1. Why am I having problems using ZLIB.DLL? My application works | ||
12 | with the static build of zlib just fine, and I didn't make any | ||
13 | modification when recompiling it for ZLIB.DLL. | ||
14 | |||
15 | - Make sure you define ZLIB_DLL before including "zlib.h". | ||
16 | Applications that link to ZLIB.DLL will work properly if | ||
17 | the source files are compiled in this (or in a compatible) | ||
18 | way, and the executables are linked to MSVCRT.DLL. | ||
19 | |||
20 | |||
21 | 2. Why do I have to do this? When I use other libraries, I can | ||
22 | link my code to their static or dynamic versions, without | ||
23 | needing any source code modification or recompilation. | ||
24 | |||
25 | - In order to preserve the backwards compatibility with the | ||
26 | older versions of ZLIB.DLL, and give the ability to use zlib | ||
27 | to the non-C programmers at the same time, we had to do this | ||
28 | compromise. | ||
29 | |||
30 | |||
31 | 3. What exactly is this mess about, and why is it happening? | ||
32 | |||
33 | - It's about the calling convention used for the zlib functions. | ||
34 | If linked in statically, zlib uses the C (CDECL) convention. | ||
35 | If linked in dynamically (via ZLIB.DLL), it uses the STDCALL | ||
36 | convention. The ZLIB_DLL macro switches on the use of STDCALL. | ||
37 | |||
38 | It happens because we need to preserve compatibility with the | ||
39 | old releases of ZLIB.DLL that use STDCALL, and, at the same | ||
40 | time, we must provide support for programmers who use other | ||
41 | programming languages with bindings that require CDECL. | ||
42 | |||
43 | |||
44 | 4. Why not use the STDCALL convention all the time? | ||
45 | It's the standard convention in Win32, and I need it in my | ||
46 | Visual Basic project! | ||
47 | |||
48 | - Most of the Win32 API functions (without varargs) use indeed | ||
49 | the STDCALL (WINAPI) convention, but the standard C functions | ||
50 | use the default CDECL. If one calls Win32 functions such as | ||
51 | CreateFile(), sometimes it makes sense to decorate one's own | ||
52 | functions with STDCALL. But if one is aiming at ANSI C or | ||
53 | POSIX portability, and calls functions such as fopen(), it is | ||
54 | not a sound decision to include <windows.h> or to use non-ANSI | ||
55 | constructs only to make one's functions STDCALL-able. This is | ||
56 | not the biggest problem, however. | ||
57 | |||
58 | Technically, STDCALL is not bad; it is even a little faster | ||
59 | than CDECL. The problem of using STDCALL is actually a problem | ||
60 | of using any explicit calling convention. FASTCALL falls into | ||
61 | the same category. | ||
62 | |||
63 | Explicit specification of calling conventions, whether it's | ||
64 | direct or indirect via a macro, happens commonly in Windows, | ||
65 | but it is regarded as a noisy, non-standard C quirk on other | ||
66 | platforms. It isn't possible to write an ANSI C -conforming | ||
67 | program, for example, if it is necessary to specify calling | ||
68 | conventions. Libraries can hide the dirty stuff in header | ||
69 | files, under macros, but callbacks will still remain exposed. | ||
70 | This is why the zlib callbacks will not be decorated. | ||
71 | (The old Windows callbacks, such as WndProc, are decorated, | ||
72 | but the newer ones are not.) | ||
73 | |||
74 | There is one more problem with explicit, non-default calling | ||
75 | conventions: the ability to use zlib in other programming | ||
76 | languages. Some of them, like Ada (GNAT) and Fortran (GNU G77) | ||
77 | have C bindings implemented initially on Unix, hence relying | ||
78 | on the C calling convention. | ||
79 | |||
80 | So we are decorating the functions using STDCALL in ZLIB.DLL | ||
81 | to maintain compatibility with the old versions, but we are | ||
82 | using the default CDECL in the static library, to allow other | ||
83 | programming languages to use zlib in a portable fashion, via | ||
84 | C bindings. | ||
85 | |||
86 | |||
87 | 5. Why not use the default (CDECL) convention all the time? | ||
88 | It's the standard convention in C, and I need it in my Ada | ||
89 | project! | ||
90 | |||
91 | - Originally, ZLIB.DLL was intended to run under Visual Basic, | ||
92 | and VB6 and earlier need STDCALL. | ||
93 | |||
94 | We admit that cluttering the main zlib sources, for the sake | ||
95 | of interfacing with Visual Basic and at the expense of other | ||
96 | programming languages, is not fair. It would have been better | ||
97 | to maintain a "VB-only" project in the contrib/ directory, and | ||
98 | to build a custom ZLIBVB.DLL, for example -- as we did with | ||
99 | the Delphi projects. Another possible solution would have been | ||
100 | to build STDCALL wrappers around the CDECL-exported functions. | ||
101 | |||
102 | But this was the accident that we have to live with, in order | ||
103 | to maintain binary compatibility with the older versions of | ||
104 | ZLIB.DLL. | ||
105 | |||
106 | |||
107 | 6. If my application uses ZLIB.DLL, do I have to link it to | ||
108 | MSVCRT.DLL? Why? | ||
109 | |||
110 | - The executables (.EXE, .DLL, etc.) that are involved in the | ||
111 | same process and are using the C run-time library (i.e. they | ||
112 | are calling any standard C function), must link to the same | ||
113 | library. There are several libraries in the Win32 system: | ||
114 | CRTDLL.DLL, MSVCRT.DLL, the static C libraries, etc. | ||
115 | Since ZLIB.DLL is linked to MSVCRT.DLL, the executables that | ||
116 | depend on it must also link to MSVCRT.DLL. | ||
117 | |||
118 | |||
119 | 7. Why are you saying that ZLIB.DLL and my application must be | ||
120 | linked to the same C run-time library (CRT)? I linked my | ||
121 | application and my DLLs to different C libraries (e.g. my | ||
122 | application to a static library, and my DLLs to MSVCRT.DLL), | ||
123 | and everything works fine. | ||
124 | |||
125 | - If a library invokes only pure Win32 API (i.e. accessible | ||
126 | via <windows.h>), its DLL build will work in any context. | ||
127 | But if a library invokes standard C functions, things get | ||
128 | more complicated. | ||
129 | |||
130 | There is a single Win32 library in a Win32 system. Every | ||
131 | function in this library resides in a single DLL module, that | ||
132 | is safe to call from anywhere. On the other hand, there are | ||
133 | multiple versions of the C library that are all at the same | ||
134 | time in the system, and all of them have internal states, | ||
135 | therefore it is dangerous to intermix them with each other. | ||
136 | |||
137 | Intermixing multiple C libraries is possible, as long as their | ||
138 | internal states are kept intact. The Microsoft Knowledge Base | ||
139 | article Q140584 "HOWTO: Link with the Correct C Run-Time (CRT) | ||
140 | Library" enumerates some of the potential problems raised by | ||
141 | intermixing, but does not offer a complete description of how | ||
142 | to avoid them, except by advising not to mix the C libraries. | ||
143 | If you can send us more information about this issue, we will | ||
144 | highly appreciate it. (But please do NOT send us source code | ||
145 | from Microsoft, even if it comes with your legitimate copy of | ||
146 | Visual C++!) | ||
147 | |||
148 | If this kind of intermixing works for you, it's because your | ||
149 | application and DLLs are avoiding the corruption of the CRT's | ||
150 | internal states, due to a fortunate accident. It's not because | ||
151 | those libraries really work together. | ||
152 | |||
153 | Also note that linking ZLIB.DLL to non-Microsoft C libraries | ||
154 | (such as Borland's) raises similar problems. | ||
155 | |||
156 | |||
157 | 8. Why are you linking ZLIB.DLL to MSVCRT.DLL? | ||
158 | |||
159 | - MSVCRT.DLL exists on every Windows 95 with a new service pack | ||
160 | installed, or with Microsoft Internet Explorer 4 or later, and | ||
161 | on all other Windows 4.x or later (Windows 98, Windows NT 4, | ||
162 | or later). It is freely distributable; if not present in the | ||
163 | system, it can be downloaded from Microsoft or from other | ||
164 | software provider for free. | ||
165 | |||
166 | The fact that MSVCRT.DLL does not exist on a virgin Windows 95 | ||
167 | is not so problematic. The number of Windows 95 installations | ||
168 | is rapidly decreasing, Microsoft stopped supporting it a long | ||
169 | time ago, and many recent applications from various vendors | ||
170 | including Microsoft, do not even run on it. Even without these | ||
171 | arguments, no serious user should run Windows 95 without a | ||
172 | proper update installed. | ||
173 | |||
174 | There is also the fact that the mainstream C compilers for | ||
175 | Windows are Microsoft Visual C++ 6.0, and gcc/MinGW. Both | ||
176 | are producing executables that link to MSVCRT.DLL by default, | ||
177 | without offering other dynamic CRTs as alternatives easy to | ||
178 | select by users. | ||
179 | |||
180 | |||
181 | 9. Why are you not linking ZLIB.DLL to | ||
182 | <<my favorite C run-time library>> ? | ||
183 | |||
184 | - We considered and abandoned the following alternatives: | ||
185 | |||
186 | * Linking ZLIB.DLL to a static C library (LIBC.LIB, or | ||
187 | LIBCMT.LIB) is not a good option. People are using ZLIB.DLL | ||
188 | mainly to save disk space. If you are linking your program | ||
189 | to a static C library, you may as well consider linking zlib | ||
190 | in statically, too. | ||
191 | |||
192 | * Linking ZLIB.DLL to CRTDLL.DLL looks very appealing, | ||
193 | because CRTDLL.DLL is present on every Win32 installation. | ||
194 | Unfortunately, it has a series of problems: it raises | ||
195 | difficulties when linking to the Microsoft C++ libraries, | ||
196 | it is not thread-safe, and Microsoft has discontinued its | ||
197 | support a long time ago. | ||
198 | |||
199 | * Linking ZLIB.DLL to MSVCRT70.DLL, supplied with the | ||
200 | Microsoft .NET platform and Visual C++ 7.0, is not a good | ||
201 | option. Although it can be downloaded and distributed | ||
202 | freely, it is hardly present on today's Win32 installations. | ||
203 | If it will become more popular than MSVCRT.DLL, and will be | ||
204 | pre-installed on the future Win32 systems, we will probably | ||
205 | think again about it. | ||
206 | |||
207 | * Linking ZLIB.DLL to NTDLL.DLL is not possible. | ||
208 | NTDLL.DLL exports only a part of the C library, and only | ||
209 | on Windows NT systems. | ||
210 | |||
211 | |||
212 | 10. I understand your reasons. However, my project needs ZLIB.DLL | ||
213 | linked to something different than MSVCRT.DLL. What can I do? | ||
214 | |||
215 | Feel free to rebuild this DLL from the zlib sources, and link | ||
216 | it the way you want. It is required, however, to clearly | ||
217 | state that your build is unofficial. Another thing that is not | ||
218 | required, but highly recommended, is to name that custom DLL | ||
219 | differently, and/or to install it in a private directory that | ||
220 | can be accessed by your application, but is not visible to the | ||
221 | others (e.g. it's not the SYSTEM or the SYSTEM32 directory, | ||
222 | and it's not in the PATH). Otherwise, your build may clash | ||
223 | with applications that link to the official build. | ||
224 | |||
225 | For example, in Cygwin, zlib is linked to their runtime | ||
226 | CYGWIN1.DLL, and it is distributed under the name CYGZ.DLL. | ||
227 | |||
228 | |||
229 | 11. My I include additional pieces of code that I find useful, | ||
230 | link them in ZLIB.DLL, and export them? | ||
231 | |||
232 | No. A legitimate build of ZLIB.DLL must not include code that | ||
233 | does not originate from the official zlib sources. But you can | ||
234 | make your own private build, and give it a different name, as | ||
235 | suggested in the previous answer. | ||
236 | |||
237 | For example, in Borland Delphi and C++ Builder, zlib is part | ||
238 | of the standard VCL library. If an application links to VCL | ||
239 | dynamically, the name of the distributable binary (VCLxx.DLL) | ||
240 | does not posess any danger of clashing with a legitimate but | ||
241 | incompatible ZLIB.DLL. | ||
242 | |||
243 | |||
244 | 12. I see that I may have all kinds of problems if I use ZLIB.DLL. | ||
245 | Do you recommend to link zlib in statically? Do I get rid of | ||
246 | problems? | ||
247 | |||
248 | - Yes, definitely. In fact, unless you are distributing a large | ||
249 | number of executables, each of them linking to zlib, you will | ||
250 | save space by linking zlib in statically (assuming that you | ||
251 | would otherwise distribute ZLIB.DLL with your application). | ||
252 | zlib is not a big library, and the space saved by ZLIB.DLL is | ||
253 | little. Much of the actual size of the DLL is due to the 4KB | ||
254 | alignment in the binary. | ||
255 | |||
256 | But you may have reasons, other than size, to use the DLL. | ||
257 | That is entirely up to you. | ||