devtips.h 18 KB

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