]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/mainpages/devtips.h
merge "strategies" page with devtips.h; add mention of Bakefile and wx presets in...
[wxWidgets.git] / docs / doxygen / mainpages / devtips.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: devtips.h
3 // Purpose: Cross-platform development page of the Doxygen manual
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 /**
11
12 @page page_multiplatform Multi-platform development
13
14 This chapter describes some tips related to multi-platform development.
15
16 @li @ref page_multiplatform_includefiles
17 @li @ref page_multiplatform_libraries
18 @li @ref page_multiplatform_configuration
19 @li @ref page_multiplatform_makefiles
20 @li @ref page_multiplatform_winresources
21 @li @ref page_multiplatform_allocatingobjects
22 @li @ref page_multiplatform_architecturedependency
23 @li @ref page_multiplatform_conditionalcompilation
24 @li @ref page_multiplatform_cpp
25 @li @ref page_multiplatform_filehandling
26 @li @ref page_multiplatform_reducingerr
27 @li @ref page_multiplatform_gui
28 @li @ref page_multiplatform_debug
29
30 <hr>
31
32
33 @section page_multiplatform_includefiles Include files
34
35 The main include file is @c "wx/wx.h"; this includes the most commonly
36 used modules of wxWidgets.
37
38 To save on compilation time, include only those header files relevant to the
39 source file. If you are using @b precompiled headers, you should include
40 the following section before any other includes:
41
42 @verbatim
43 // For compilers that support precompilation, includes "wx.h".
44 #include <wx/wxprec.h>
45
46 #ifdef __BORLANDC__
47 #pragma hdrstop
48 #endif
49
50 #ifndef WX_PRECOMP
51 // Include your minimal set of headers here, or wx.h
52 #include <wx/wx.h>
53 #endif
54
55 ... now your other include files ...
56 @endverbatim
57
58 The file @c "wx/wxprec.h" includes @c "wx/wx.h". Although this incantation
59 may seem quirky, it is in fact the end result of a lot of experimentation,
60 and several Windows compilers to use precompilation which is largely automatic for
61 compilers with necessary support. Currently it is used for Visual C++ (including
62 embedded Visual C++), Borland C++, Open Watcom C++, Digital Mars C++
63 and newer versions of GCC.
64 Some compilers might need extra work from the application developer to set the
65 build environment up as necessary for the support.
66
67
68
69 @section page_multiplatform_libraries Libraries
70
71 All ports of wxWidgets can create either a @b static library or a @b shared library.
72
73 When a program is linked against a @e static library, the machine code from the object
74 files for any external functions used by the program is copied from the library into
75 the final executable.
76
77 @e Shared libraries are handled with a more advanced form of linking, which makes the
78 executable file smaller. They use the extension @c \91.so\92 (Shared Object) under Linux
79 and @c '.dll' (Dynamic Link Library) under Windows.
80
81 An executable file linked against a shared library contains only a small table of the
82 functions it requires, instead of the complete machine code from the object files for
83 the external functions.
84 Before the executable file starts running, the machine code for the external functions
85 is copied into memory from the shared library file on disk by the operating
86 system - a process referred to as @e dynamic linking.
87
88 Dynamic linking makes executable files smaller and saves disk space, because one copy
89 of a library can be shared between multiple programs.
90 Most operating systems also provide a virtual memory mechanism which allows one copy
91 of a shared library in physical memory to be used by all running programs, saving
92 memory as well as disk space.
93
94 Furthermore, shared libraries make it possible to update a library without recompiling
95 the programs which use it (provided the interface to the library does not change).
96
97 wxWidgets can also be built in @b multilib and @b monolithic variants.
98 See the @ref page_libs for more information on these.
99
100
101
102 @section page_multiplatform_configuration Configuration
103
104 When using project files and makefiles directly to build wxWidgets,
105 options are configurable in the file
106 @c "wx/XXX/setup.h" where XXX is the required platform (such as @c msw, @c motif,
107 @c gtk, @c mac).
108
109 Some settings are a matter of taste, some help with platform-specific problems, and
110 others can be set to minimize the size of the library. Please see the @c setup.h file
111 and @c install.txt files for details on configuration.
112
113 When using the @c 'configure' script to configure wxWidgets (on Unix and other platforms
114 where configure is available), the corresponding @c setup.h files are generated automatically
115 along with suitable makefiles.
116
117 When using the RPM packages (or DEB or other forms of @e binaries) for installing
118 wxWidgets on Linux, a correct @c setup.h is shipped in the package and
119 this must not be changed.
120
121
122
123 @section page_multiplatform_makefiles Makefiles
124
125 On Microsoft Windows, wxWidgets has a different set of makefiles for each
126 compiler, because each compiler's @c 'make' tool is slightly different.
127 Popular Windows compilers that we cater for, and the corresponding makefile
128 extensions, include: Microsoft Visual C++ (.vc), Borland C++ (.bcc),
129 OpenWatcom C++ (.wat) and MinGW/Cygwin (.gcc). Makefiles are provided
130 for the wxWidgets library itself, samples, demos, and utilities.
131
132 On Linux, Mac and OS/2, you use the @c 'configure' command to generate the
133 necessary makefiles. You should also use this method when building with
134 MinGW/Cygwin on Windows.
135
136 We also provide project files for some compilers, such as Microsoft VC++.
137 However, we recommend using makefiles to build the wxWidgets library itself,
138 because makefiles can be more powerful and less manual intervention is required.
139
140 On Windows using a compiler other than MinGW/Cygwin, you would build the wxWidgets
141 library from the @c build/msw directory which contains the relevant makefiles.
142
143 On Windows using MinGW/Cygwin, and on Unix, MacOS X and OS/2, you invoke
144 'configure' (found in the top-level of the wxWidgets source hierarchy),
145 from within a suitable empty directory for containing makefiles, object
146 files and libraries.
147
148 For details on using makefiles, configure, and project files,
149 please see @c docs/xxx/install.txt in your distribution, where
150 @c xxx is the platform of interest, such as @c msw, @c gtk, @c x11, @c mac.
151
152 All wxWidgets makefiles are generated using @link http://www.bakefile.org Bakefile @endlink.
153 wxWidgets also provides (in the @c build/bakefiles/wxpresets) the
154 <b>wxWidgets bakefile presets</b>: these files allow you to create bakefiles for
155 your own wxWidgets-based applications very easily.
156
157
158 @section page_multiplatform_winresources Windows Resource files
159
160 wxWidgets application compilation under MS Windows requires at least one
161 extra file: a resource file.
162
163 The least that must be defined in the Windows resource file (extension RC)
164 is the following statement:
165
166 @verbatim
167 #include "wx/msw/wx.rc"
168 @endverbatim
169
170 which includes essential internal wxWidgets definitions. The resource script
171 may also contain references to icons, cursors, etc., for example:
172
173 @verbatim
174 wxicon icon wx.ico
175 @endverbatim
176
177 The icon can then be referenced by name when creating a frame icon. See
178 the MS Windows SDK documentation.
179
180 @note include wx.rc @e after any ICON statements
181 so programs that search your executable for icons (such
182 as the Program Manager) find your application icon first.
183
184
185
186 @section page_multiplatform_allocatingobjects Allocating and deleting wxWidgets objects
187
188 In general, classes derived from wxWindow must dynamically allocated
189 with @e new and deleted with @e delete. If you delete a window,
190 all of its children and descendants will be automatically deleted,
191 so you don't need to delete these descendants explicitly.
192
193 When deleting a frame or dialog, use @b Destroy rather than @b delete so
194 that the wxWidgets delayed deletion can take effect. This waits until idle time
195 (when all messages have been processed) to actually delete the window, to avoid
196 problems associated with the GUI sending events to deleted windows.
197
198 Don't create a window on the stack, because this will interfere
199 with delayed deletion.
200
201 If you decide to allocate a C++ array of objects (such as wxBitmap) that may
202 be cleaned up by wxWidgets, make sure you delete the array explicitly
203 before wxWidgets has a chance to do so on exit, since calling @e delete on
204 array members will cause memory problems.
205
206 wxColour can be created statically: it is not automatically cleaned
207 up and is unlikely to be shared between other objects; it is lightweight
208 enough for copies to be made.
209
210 Beware of deleting objects such as a wxPen or wxBitmap if they are still in use.
211 Windows is particularly sensitive to this: so make sure you
212 make calls like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before deleting
213 a drawing object that may be in use. Code that doesn't do this will probably work
214 fine on some platforms, and then fail under Windows.
215
216
217
218 @section page_multiplatform_architecturedependency Architecture dependency
219
220 A problem which sometimes arises from writing multi-platform programs is that
221 the basic C types are not defined the same on all platforms. This holds true
222 for both the length in bits of the standard types (such as int and long) as
223 well as their byte order, which might be little endian (typically
224 on Intel computers) or big endian (typically on some Unix workstations). wxWidgets
225 defines types and macros that make it easy to write architecture independent
226 code. The types are:
227
228 wxInt32, wxInt16, wxInt8, wxUint32, wxUint16 = wxWord, wxUint8 = wxByte
229
230 where wxInt32 stands for a 32-bit signed integer type etc. You can also check
231 which architecture the program is compiled on using the wxBYTE_ORDER define
232 which is either wxBIG_ENDIAN or wxLITTLE_ENDIAN (in the future maybe wxPDP_ENDIAN
233 as well).
234
235 The macros handling bit-swapping with respect to the applications endianness
236 are described in the @ref group_funcmacro_byteorder section.
237
238
239
240 @section page_multiplatform_conditionalcompilation Conditional compilation
241
242 One of the purposes of wxWidgets is to reduce the need for conditional
243 compilation in source code, which can be messy and confusing to follow.
244 However, sometimes it is necessary to incorporate platform-specific
245 features (such as metafile use under MS Windows). The @ref page_wxusedef
246 symbols listed in the file @c setup.h may be used for this purpose,
247 along with any user-supplied ones.
248
249
250
251 @section page_multiplatform_cpp C++ issues
252
253 The following documents some miscellaneous C++ issues.
254
255 @subsection page_multiplatform_cpp_templates Templates
256
257 wxWidgets does not use templates (except for some advanced features that
258 are switched off by default) since it is a notoriously unportable feature.
259
260 @subsection page_multiplatform_cpp_rtti RTTI
261
262 wxWidgets does not use C++ run-time type information since wxWidgets provides
263 its own run-time type information system, implemented using macros.
264
265 @subsection page_multiplatform_cpp_null Type of NULL
266
267 Some compilers (e.g. the native IRIX cc) define NULL to be 0L so that
268 no conversion to pointers is allowed. Because of that, all these
269 occurrences of NULL in the GTK+ port use an explicit conversion such
270 as
271
272 @code
273 wxWindow *my_window = (wxWindow*) NULL;
274 @endcode
275
276 It is recommended to adhere to this in all code using wxWidgets as
277 this make the code (a bit) more portable.
278
279 @subsection page_multiplatform_cpp_precompiledheaders Precompiled headers
280
281 Some compilers, such as Borland C++ and Microsoft C++, support
282 precompiled headers. This can save a great deal of compiling time. The
283 recommended approach is to precompile @c "wx.h", using this
284 precompiled header for compiling both wxWidgets itself and any
285 wxWidgets applications. For Windows compilers, two dummy source files
286 are provided (one for normal applications and one for creating DLLs)
287 to allow initial creation of the precompiled header.
288
289 However, there are several downsides to using precompiled headers. One
290 is that to take advantage of the facility, you often need to include
291 more header files than would normally be the case. This means that
292 changing a header file will cause more recompilations (in the case of
293 wxWidgets, everything needs to be recompiled since everything includes @c "wx.h" !)
294
295 A related problem is that for compilers that don't have precompiled
296 headers, including a lot of header files slows down compilation
297 considerably. For this reason, you will find (in the common
298 X and Windows parts of the library) conditional
299 compilation that under Unix, includes a minimal set of headers;
300 and when using Visual C++, includes @c wx.h. This should help provide
301 the optimal compilation for each compiler, although it is
302 biased towards the precompiled headers facility available
303 in Microsoft C++.
304
305
306
307 @section page_multiplatform_filehandling File handling
308
309 When building an application which may be used under different
310 environments, one difficulty is coping with documents which may be
311 moved to different directories on other machines. Saving a file which
312 has pointers to full pathnames is going to be inherently unportable.
313
314 One approach is to store filenames on their own, with no directory
315 information. The application then searches into a list of standard
316 paths (platform-specific) through the use of wxStandardPaths.
317
318 Eventually you may want to use also the wxPathList class.
319
320 Nowadays the limitations of DOS 8+3 filenames doesn't apply anymore.
321 Most modern operating systems allow at least 255 characters in the filename;
322 the exact maximum length, as well as the characters allowed in the filenames,
323 are OS-specific so you should try to avoid extremely long (> 255 chars) filenames
324 and/or filenames with non-ANSI characters.
325
326 Another thing you need to keep in mind is that all Windows operating systems
327 are case-insensitive, while Unix operating systems (Linux, Mac, etc) are
328 case-sensitive.
329
330 Also, for text files, different OSes use different End Of Lines (EOL).
331 Windows uses CR+LF convention, Linux uses LF only, Mac CR only.
332
333 The wxTextFile, wxTextInputStream, wxTextOutputStream classes help to abstract
334 from these differences.
335 Of course, there are also 3rd party utilities such as @c dos2unix and @c unix2dos
336 which do the EOL conversions.
337
338 See also the @ref group_funcmacro_file section of the reference
339 manual for the description of miscellaneous file handling functions.
340
341
342 @section page_multiplatform_reducingerr Reducing programming errors
343
344 @subsection page_multiplatform_reducingerr_useassert Use ASSERT
345
346 It is good practice to use ASSERT statements liberally, that check for conditions
347 that should or should not hold, and print out appropriate error messages.
348
349 These can be compiled out of a non-debugging version of wxWidgets
350 and your application. Using ASSERT is an example of `defensive programming':
351 it can alert you to problems later on.
352
353 See ::wxASSERT for more info.
354
355 @subsection page_multiplatform_reducingerr_usewxstring Use wxString in preference to character arrays
356
357 Using wxString can be much safer and more convenient than using @c wxChar*.
358
359 You can reduce the possibility of memory leaks substantially, and it is much more
360 convenient to use the overloaded operators than functions such as @c strcmp.
361 wxString won't add a significant overhead to your program; the overhead is compensated
362 for by easier manipulation (which means less code).
363
364 The same goes for other data types: use classes wherever possible.
365
366
367
368 @section page_multiplatform_gui GUI design
369
370 @subsection page_multiplatform_gui_usesizers Use sizers
371
372 Don't use absolute panel item positioning if you can avoid it. Different GUIs have
373 very differently sized panel items. Consider using the @ref overview_sizer instead.
374
375 @subsection page_multiplatform_gui_useresources Use wxWidgets resource files
376
377 Use @c XRC (wxWidgets resource files) where possible, because they can be easily changed
378 independently of source code. See the @ref overview_xrc for more info.
379
380
381
382 @section page_multiplatform_debug Debugging
383
384 @subsection page_multiplatform_debug_positivethinking Positive thinking
385
386 It is common to blow up the problem in one's imagination, so that it seems to threaten
387 weeks, months or even years of work. The problem you face may seem insurmountable:
388 but almost never is. Once you have been programming for some time, you will be able
389 to remember similar incidents that threw you into the depths of despair. But
390 remember, you always solved the problem, somehow!
391
392 Perseverance is often the key, even though a seemingly trivial problem
393 can take an apparently inordinate amount of time to solve. In the end,
394 you will probably wonder why you worried so much. That's not to say it
395 isn't painful at the time. Try not to worry -- there are many more important
396 things in life.
397
398 @subsection page_multiplatform_debug_simplifyproblem Simplify the problem
399
400 Reduce the code exhibiting the problem to the smallest program possible
401 that exhibits the problem. If it is not possible to reduce a large and
402 complex program to a very small program, then try to ensure your code
403 doesn't hide the problem (you may have attempted to minimize the problem
404 in some way: but now you want to expose it).
405
406 With luck, you can add a small amount of code that causes the program
407 to go from functioning to non-functioning state. This should give a clue
408 to the problem. In some cases though, such as memory leaks or wrong
409 deallocation, this can still give totally spurious results!
410
411 @subsection page_multiplatform_debug_usedebugger Use a debugger
412
413 This sounds like facetious advice, but it is surprising how often people
414 don't use a debugger. Often it is an overhead to install or learn how to
415 use a debugger, but it really is essential for anything but the most
416 trivial programs.
417
418 @subsection page_multiplatform_debug_uselogging Use logging functions
419
420 There is a variety of logging functions that you can use in your program:
421 see @ref group_funcmacro_log.
422
423 Using tracing statements may be more convenient than using the debugger
424 in some circumstances (such as when your debugger doesn't support a lot
425 of debugging code, or you wish to print a bunch of variables).
426
427 @subsection page_multiplatform_debug_usedebuggingfacilities Use the wxWidgets debugging facilities
428
429 You can use wxDebugContext to check for
430 memory leaks and corrupt memory: in fact in debugging mode, wxWidgets will
431 automatically check for memory leaks at the end of the program if wxWidgets is suitably
432 configured. Depending on the operating system and compiler, more or less
433 specific information about the problem will be logged.
434
435 You should also use @ref group_funcmacro_debugging as part of a `defensive programming' strategy,
436 scattering wxASSERTs liberally to test for problems in your code as early as possible.
437 Forward thinking will save a surprising amount of time in the long run.
438
439 See the @ref overview_debugging for further information.
440
441
442 */