1 <chapter id="portability-issues">
2 <title id="portability-issues.title">Portability issues</title>
5 <title id="anon.title">Anonymous unions/structs</title>
7 Anonymous structs and unions support depends heavily on the compiler.
8 The best support is provided by gcc/g++ 2.96 and later. But these
9 versions of gcc come from the development branch so you may want to
10 hold off before using them in production. g++ 2.95 supports anonymous
11 unions but not anonymous structs and gcc 2.95 supports neither. Older
12 versions of gcc/g++ have no support for either.
13 since it is anonymous unions that are the most frequent in the
14 windows headers, you should at least try to use gcc/g++ 2.95.
17 But you are stuck with a compiler that does not support anonymous
18 structs/unions all is not lost. The Wine headers should detect this
19 automatically and define <varname>NONAMELESSUNION</varname> /
20 <varname>NONAMELESSSTRUCT</varname>. Then any anonymous union will
22 <literal>u</literal> or <literal>u2</literal>, <literal>u3</literal>,
23 etc. to avoid name clashes. You will then have to modify your code to
24 include those names where appropriate.
27 The name that Wine adds to anonymous unions should match that used
28 by the Windows headers. So all you have to do to compile your
29 modified code in Windows is to explicitly define the
30 <varname>NONAMELESSUNION</varname> macro. Note that it would be wise
31 to also explicitly define this macro on in your Unix makefile
32 (<filename>Makefile.in</filename>) to make sure your code will
33 compile even if the compiler does support anonymous unions.
36 Things are not as nice when dealing with anonymous structs.
37 Unfortunately the Windows headers make no provisions for compilers
38 that do not support anonymous structs. So you will need to be more
39 subtle when modifying your code if you still want it to compile in
40 Windows. Here's a way to do it:
53 GetSystemInfo(&si);
54 printf("Processor architecture=%d\n",si ANONS .wProcessorArchitecture);
58 You may put the <literal>#define</literal> directive directly in the
59 source if only few files are impacted. Otherwise it's probably best
60 to put it in one of your project's widely used headers.
61 Fortunately usage of an anonymous struct is much rarer than usage of
62 an anonymous union so these modifications should not be too much work.
67 <title id="unicode.title">Unicode</title>
70 Because gcc and glibc use 4 byte unicode characters, the
71 compiler intrinsic <literal>L"foo"</literal> generates unicode
72 strings which cannot be used by Winelib (Win32 code expects 16
73 bit unicode characters). There are 3 workarounds for this:
79 Use the latest gcc version (2.9.7 or later), and pass the
80 <parameter>-fshort-wchar</parameter> option to every file
86 Use the <function>__TEXT("foo")</function> macro, define
87 <constant>WINE_UNICODE_REWRITE</constant> for each file
88 that is built, and add
89 <parameter>-fwritable-strings</parameter> to the compiler
90 command line. You should replace all occurrences of
91 <type>wchar_t</type> with <type>WCHAR</type> also, since
92 <type>wchar_t</type> is the native (32 bit) type. These
93 changes allow Wine to modify the native unicode strings
94 created by the compiler in place, so that they are 16 bit
95 by the time any functions get to use them. This scheme
96 works with older versions of gcc (2.95.x+).
101 Use the compiler default, but don't call any Win32 unicode
102 function without converting the strings first!
108 If you are using Unicode and you want to be able to use
109 standard library calls (e.g. <function>wcslen</function>,
110 <function>wsprintf</function>) as well as Win32 unicode calls
111 (API functions ending in W, or having
112 <constant>_UNICODE</constant> defined), then you should use
113 the msvcrt runtime library instead of glibc. The functions in
114 glibc will not work correctly with 16 bit strings.
117 If you need a Unicode string even when
118 _<constant>UNICODE</constant> isn't defined, use
119 <function>WINE_UNICODE_TEXT("foo")</function>. This will need
120 to be wrapped in <function>#ifdef WINELIB</function> to
121 prevent breaking your source for windows compiles.
124 To prevent warnings when declaring a single unicode character
125 in C, use <function>(WCHAR)L'x'</function>, rather than
126 <function>__TEXT('x')</function>. This works on Windows also.
130 <sect1 id="C-library">
131 <title id="C-library.title">C library</title>
133 <!-- *** Is all of this covered now? Make sure before deleting ***
135 Winelib currently only supports on C library: that of your
136 compiler. three solutions: native, mixed or msvcrt except we
137 only have native right now, using the native C library ->
138 different behavior: fopen, O_TEXT, unicode support,
144 There are 3 choices available to you regarding which C library
151 Use the glibc native C library.
156 Use the msvcrt C library.
161 Use a custom mixture of both.
167 Note that under Wine, the crtdll library is implemented using
168 msvcrt, so there is no benefit in trying to use it.
171 Using glibc in general has the lowest overhead, but this is
172 really only important for file I/O. Many of the functions in
173 msvcrt are simply resolved to glibc, so in reality options 2
174 and 3 are fairly similar choices.
177 To use glibc, you don't need to make changes to your
178 application; it should work straight away. There are a few
179 situations in which using glibc is not possible:
184 Your application uses Win32 and C library unicode
190 Your application uses MS specific calls like
191 <function>beginthread()</function>,
192 <function>loadlibrary()</function>, etc.
197 You rely on the precise semantics of the calls, for
198 example, returning <literal>-1</literal> rather than
199 non-zero. More likely, your application will rely on calls
200 like <function>fopen()</function> taking a Windows path
201 rather than a Unix one.
206 In these cases you should use msvcrt to provide your C runtime
207 calls. To do this, add a line:
210 <programlisting>import msvcrt.dll</programlisting>
213 to your applications <filename>.spec</filename> file. This
214 will cause <command>winebuild</command> to resolve your c
215 library calls to <filename>msvcrt.dll</filename>. Many simple
216 calls which behave the same have been specified as
217 non-importable from msvcrt; in these cases
218 <command>winebuild</command> will not resolve them and the
219 standard linker <command>ld</command> will link to the glibc
223 In order to avoid warnings in C (and potential errors in C++)
224 from not having prototypes, you may need to use a set of MS
225 compatible header files. These are scheduled for inclusion
226 into Wine but at the time of writing are not available. Until
227 they are, you can try prototyping the functions you need, or
228 just live with the warnings.
231 If you have a set of include files (or when they are available
232 in Wine), you need to use the <parameter>-isystem
233 "include_path"</parameter> flag to gcc to tell it to use your
234 headers in preference to the local system headers.
237 To use option 3, add the names of any symbols that you don't
238 want to use from msvcrt into your applications
239 <filename>.spec</filename> file. For example, if you wanted
240 the MS specific functions, but not file I/O, you could have a
244 <programlisting>@ignore = ( fopen fclose fwrite fread fputs fgets )</programlisting>
246 Obviously, the complete list would be much longer. Remember
247 too that some functions are implemented with an underscore in
248 their name and <function>#define</function>d to that name in
249 the MS headers. So you may need to find out the name by
250 examining <filename>dlls/msvcrt/msvcrt.spec</filename> to get
251 the correct name for your <function>@ignore</function> entry.
255 <sect1 id="porting-compiling">
256 <title id="porting-compiling.title">Compiling Problems</title>
258 If you get undefined references to Win32 API calls when
259 building your application: if you have a VC++
260 <filename>.dsp</filename> file, check it for all the
261 <filename>.lib</filename> files it imports, and add them to
262 your applications <filename>.spec</filename>
263 file. <command>winebuild</command> gives you a warning for
264 unused imports so you can delete the ones you don't need
265 later. Failing that, just import all the DLL's you can find in
266 the <filename>dlls/</filename> directory of the Wine source
270 If you are missing GUIDs at the link stage, add
271 <parameter>-lwine_uuid</parameter> to the link line.
274 gcc is more strict than VC++, especially when compiling
275 C++. This may require you to add casts to your C++ to prevent
276 overloading ambiguities between similar types (such as two
277 overloads that take int and char respectively).
280 If you come across a difference between the Windows headers
281 and Wine's that breaks compilation, try asking for help on
282 <email>wine-devel@winehq.com</email>.
286 <sect1 id="init-problems">
287 <title id="init-problems.title">Initialization problems</title>
289 Initialization problems occur when the application calls the Win32 API
290 before Winelib has been initialized. How can this happen?
293 Winelib is initialized by the application's <function>main</function>
294 before it calls the regular <function>WinMain</function>. But, in C++,
295 the constructors of static class variables are called before the
296 <function>main</function> (by the module's initializer). So if such
297 a constructor makes calls to the Win32 API, Winelib will not be
298 initialized at the time of the call and you may get a crash. This
299 problem is much more frequent in C++ because of these class
300 constructors but could also, at least in theory, happen in C if you
301 were to specify an initializer making calls to Winelib. But of
302 course, now that you are aware of this problem you won't do it :-).
305 Further compounding the problem is the fact that Linux's (GNU's?)
306 current dynamic library loader does not call the module
307 initializers in their dependency order. So even if Winelib were to
308 have its own initializer there would be no guarantee that it would be
309 called before the initializer of the library containing this static
310 variable. Finally even if the variable is in a library that your
311 application links with, that library's initializer may be called
312 before Winelib has been initialized. One such library is the MFC.
315 The current workaround is to move all the application's code in a
316 library and to use a small Winelib application to dynamically load
317 this library. Tus the initialization sequence becomes:
322 the wrapper application starts.
327 its empty initializer is run.
332 its <function>main</function> is run. Its first task is to
338 it then loads the application's main library, plus all its
344 which triggers the execution of all these libraries initializers
345 in some unknown order. But all is fine because Winelib has
346 already been initialized anyway.
351 finally the main function calls the <function>WinMain</function>
352 of the application's library.
357 This may sound complex but Winemaker makes it simple. Just specify
358 <option>--wrap</option> or <option>--mfc</option> on the command line
359 and it will adapt its makefiles to build the wrapper and the
364 <sect1 id="com-support">
365 <title id="com-support.title">VC's native COM support</title>
368 guide on how to replace it with normal C++ code (yes, how???):
369 extracting a .h and .lib from a COM DLL
370 Can '-fno-rtti' be of some use or even required?
375 <title id="SEH.title">SEH</title>
377 how to modify the syntax so that it works both with gcc's macros and Wine's macros,
383 <title id="others.title">Others</title>
385 -fpermissive and -fno-for-scope,
391 <!-- Keep this comment at the end of the file
394 sgml-parent-document:("winelib-user.sgml" "book" "chapter" "")