home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / os2 / programm / 3703 < prev    next >
Encoding:
Text File  |  1992-07-21  |  13.5 KB  |  297 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!brooke
  3. From: brooke@cco.caltech.edu (Brooke Paul Anderson)
  4. Subject: How to get/install/use GNU C/C++ and debugger (first draft)
  5. Message-ID: <1992Jul22.065053.1830@cco.caltech.edu>
  6. Sender: news@cco.caltech.edu
  7. Nntp-Posting-Host: kanga
  8. Organization: California Institute of Technology, Pasadena
  9. Date: Wed, 22 Jul 1992 06:50:53 GMT
  10. Lines: 285
  11.  
  12. Howdy, folks.  I've typed up a short document to help people get and
  13. use the GNU C and C++ compilers and debugger for OS/2.  It includes
  14. information on what you need, how to get it, how to install it, how
  15. to get it running, how to compile your first program, how to debug
  16. your first program, and what other information you might need.
  17.  
  18. This is a first draft, and I would appreciate comments (like "this
  19. is useful" or "this stinks -- don't bother").  I would also like
  20. suggestions for ANSI C reference manuals, C++ books, and OS/2 API
  21. books -- see the document for more details on what I'd like to
  22. include.
  23.  
  24. My goal in writing this is to produce a little manual so that
  25. even novices can get the GNU package up and running.  This is because
  26. I've found the package to be very nice so far -- I find it works fine
  27. for the type of programming I do, all for FREE!
  28.  
  29. At any rate, here it is (it's a long post):
  30.  
  31. --------------------- cut here ------------------------
  32.  
  33. THE GNU C AND C++ COMPILERS AND DEBUGGERS FOR OS/2
  34.  
  35. by Brooke P. Anderson, 7/21/92
  36. ("BROOKE" on GEnie, "brooke@hope.caltech.edu" on Internet)
  37.  
  38. TABLE OF CONTENTS
  39.  
  40. 1.  The purpose of this document
  41. 2.  What's GNU?
  42. 3.  The GNU compilers for OS/2 v2.0
  43. 4.  Installing and running the EMX port
  44. 5.  Books that help you use it
  45. 6.  Conclusions
  46. 7.  Addendum ("What's wrong with printf?")
  47.  
  48. THE PURPOSE OF THIS DOCUMENT
  49.  
  50. This document describes the free GNU C and C++ compilers and
  51. debuggers for OS/2 v2.0.  It concentrates on the EMX port (for
  52. reasons described below), giving information on what you need,
  53. how to get the package up and running, and how to get more
  54. information.  The EMX port includes a full 32-bit compiler (which
  55. handles both C and C++), a debugger, an assembler, and some other
  56. utilities.  The compiler does support many OS/2 API calls, so you
  57. software developers out there can use it for PM programming,
  58. manipulating semaphores, manipulating threads, using named pipes,
  59. etc.
  60.  
  61. WHAT'S GNU?
  62.  
  63. GNU produces a lot of free software for UNIX machines.  The
  64. software is mostly for software development and includes gcc (a C
  65. compiler), gdb (a symbolic debugger), g++ (a C++ compiler), make,
  66. bison (a yacc analogue), and emacs (a text editor).  These
  67. programs are standards in the UNIX community, often preferred
  68. over other products even though they are free.  The compilers,
  69. for example, produce well-optimized code; and emacs is one of the
  70. two most popular editors for UNIX.
  71.  
  72. Because the GNU software is free, it is often ported to other
  73. operating systems.  Luckily for us OS/2 users, much of the GNU
  74. software has been ported to OS/2 v2.0.  You can get it from the
  75. main anonymous-ftp site for OS/2, ftp-os2.nmsu.edu; and a fair
  76. amount of it is making its way to GEnie.  It probably also exists
  77. to a certain extent on CompuServe and many other on-line
  78. services.
  79.  
  80. THE GNU COMPILERS FOR OS/2 V2.0
  81.  
  82. For OS/2 v2.0, there are actually two ports of gcc and g++. 
  83. Eberhard Mattes and Kai Uwe Rommel provide one port -- I will
  84. call it the "EMX port."  (Kai Uwe Rommel ported g++, and Eberhard
  85. Mattes ported gcc and the rest.)  I'm not sure who provides the
  86. other port -- I will call it the "other port" [grin].  Both the
  87. EMX port and the other port contain version 2.1 of gcc.  The
  88. differences are that the EMX port provides gdb while the other
  89. port has no debugger, that the other port uses LINK386 instead of
  90. the GNU linker, and that the other port is much slower (by
  91. roughly a factor of two) for various floating point calculations
  92. compared to the EMX port.  Since the EMX port is faster in
  93. floating point and has a debugger, I will concentrate on it.
  94.  
  95. INSTALLING AND RUNNING THE EMX PORT
  96.  
  97. To get the EMX port up and running, you need the files
  98. "gnudev.zip" (which contains gcc, an old port of gdb, libraries,
  99. and some other programs), "emxdev.zip" (which contains necessary
  100. auxiliary programs), "emxgpp.zip" (which contains the files
  101. necessary to run C++, including some new libraries), and
  102. "emxgdb.zip" (which contains the latest port of gdb).  
  103.  
  104. Technical documentation for gcc and gdb is available in the form
  105. of info files, files specially formatted so that they can be used
  106. like hypertext.  You can read these files with a normal text
  107. editor, but you will notice occasional formatting commands
  108. embedded in the text.  gccinfox.zip contains the info files for
  109. gcc, and gdbinfox.zip contains the info files for gdb. 
  110. gccdoc.zip contains the same information as gccinfox.zip except
  111. all of the info files have been concatenated into one large file
  112. which is more suitable for people using only a normal text
  113. editor.  There is likewise a gdbdoc.zip file.
  114.  
  115. Source code for gdb, gcc, g++, and the libraries is available,
  116. too.  See the files "readme.doc" and "readme.gpp" described below
  117. for details.  The source code is not necessary for running the
  118. EMX port.
  119.  
  120. To summarize, you need gccdev.zip, emxdev.zip, emxgdb.zip,
  121. emxgpp.zip, gccdoc.zip (or gccinfox.zip), and gdbdoc.zip (or
  122. gdbinfox.zip).
  123.  
  124. Note that the above files might be available as ZOO archives
  125. instead of ZIP archives (for example, as "gccdoc.zoo" instead of
  126. "gccdoc.zip").  This is not a problem as both ZIP and ZOO are
  127. available free for OS/2.  You can get them from the same place
  128. you get the EMX archives.
  129.  
  130. Uncompress the files from the directory in which you want your
  131. EMX directory.  In other words, if you are in the directory
  132. "\programming", the unarchiving process will create the directory
  133. "\programming\emx" and a lot of subdirectories under that.  
  134.  
  135. IMPORTANT:  Uncompress gnudev.zip and emxdev.zip first, then
  136. uncompress the others.  emxgpp.zip and emxgdb.zip need to be
  137. uncompressed later as they contain some files that overwrite
  138. earlier versions in gnudev.zip and emxdev.zip.  Make sure that
  139. these newer versions are not skipped; make sure you do unarchive
  140. them and replace the older versions.  ZIP will ask you if you
  141. want to overwrite the older files -- you should answer "yes". 
  142. ZOO might automatically skip over the newer files rather than
  143. overwrite the older versions.  If this happens, you have to note
  144. which files ZOO skipped, delete the older versions, and then
  145. unarchive again.  This is a bug in ZOO for OS/2.
  146.  
  147. Now, you need to modify your config.sys file.  The examples below
  148. are from my config.sys file, and I have the emx directory
  149. installed under c:\apps.  Thus, my system has the directories
  150. "c:\apps\emx\dll", "c:\apps\emx\lib", "c:\apps\emx\include", and
  151. so on -- you will need to modify the following examples so that
  152. the directories are specified correctly.  First, you need to
  153. specify in your libpath the location of emx.dll.  In my
  154. config.sys, I have
  155.  
  156. LIBPATH=.;C:\OS2\DLL;C:\OS2\MDOS;C:\;C:\OS2\APPS\DLL;c:\apps\emx\dll;
  157.  
  158. Second, you need to set a few environmental variables so that gcc
  159. knows where to find various files:
  160.  
  161. set C_INCLUDE_PATH=c:/apps/emx/include
  162. set LIBRARY_PATH=c:/apps/emx/lib
  163. set CPLUS_INCLUDE_PATH=C:/apps/emx/include.cpp;C:/apps/emx/include
  164.  
  165. Note:  you need to use foward slashes ("/") and not backward
  166. slashes ("\") in the previous three lines.
  167.  
  168. Finally, go into the emx\lib\misc directory and type "make". 
  169. This updates some libraries.
  170.  
  171. After you do all of this, reboot your system (so that the changes
  172. to your config.sys take effect).  Now, you are ready to compile
  173. and debug programs.  To compile a C source file called
  174. "myprog.c", type "gcc -o myprog.exe myprog.c".  The -o switch
  175. tells gcc that it should call the resulting executable
  176. "myprog.exe".  To compile the C++ source file "myprog.cc", type
  177. "gcc -o myprog.exe myprog.cc -lgpp".  C++ source files should
  178. have the extension ".cc".  The -lgpp switch tells gcc to link the
  179. C++ libraries.  You can also tell gcc to optimize your code by
  180. using the -O switch.  There are two levels of optimization (-O
  181. and -O2, the highest being -O2).  Thus, for the fastest-executing
  182. code (at the expense of time to compile and of size of the
  183. executable), you would type "gcc -O2 -o myprog.exe myprog.c" for
  184. myprog.c and "gcc -O2 -o myprog.exe myprog.cc -lgpp" for
  185. myprog.cc.
  186.  
  187. To debug a program, you need to compile it with the -g switch: 
  188. "gcc -g -o myprog.exe myprog.c" for myprog.c and likewise for
  189. myprog.cc.  After compilation, you then type "gdb myprog.exe" to
  190. start the debugger.  Type "h" at the debugger prompt to get a
  191. list of help topics.  gdb supports all sorts of breakpoints
  192. (including conditional ones), and you can watch variables, set
  193. variables to different values, etc.  For example, to get help on
  194. running programs from within the gdb, you can type "help
  195. running".  That will give you a list of commands such as run,
  196. kill, step, etc.  You can get help on individual commands by
  197. typing, for example, "help step".
  198.  
  199. The following is a sample compile and debug session.  Go into the
  200. emx\test directory and type "gcc -g -o hello.exe hello.cc -lgpp"
  201. to compile hello.cc.  Type "hello" to run it, to see if your EMX
  202. port is functioning.  The program will print "Hello, world!" on
  203. the screen.  Now type "gdb hello.exe" to start gdb.  At the
  204. prompt "(gdb)", type "list main" to list the function "main". 
  205. Then type "break 5" to cause execution to stop at line 5 in the
  206. main function.  Type "run" to start execution -- it will stop at
  207. line 5.  Type "print argc" to see the value of the variable
  208. "argc".  Type "step" to run line 5 then halt execution again. 
  209. Type "quit" to quit.  To list a function (myfunc, say) that is
  210. longer than a screenful, type "list myfunc"; then type "list" to
  211. list the next screenful of myfunc.
  212.  
  213. The full EMX package contains utilities for creating programs
  214. that run under both OS/2 and DOS (it contains a free DOS
  215. extender).  Since I'm thoroughly uninterested in DOS, I won't go
  216. into any more detail on that.
  217.  
  218. The EMX port DOES support many of the OS/2 API functions,
  219. including ones for semaphores, PM programming, named pipes,
  220. threads, etc.  See develop.doc (in the emx\doc directory) for a
  221. list of the supported functions.  The documentation does not
  222. contain a manual on how to use these API calls -- you need an
  223. OS/2 programming book for that.
  224.  
  225. For more detailed information on the EMX port, read the files
  226. "readme.doc", "install.doc", "readme.gpp", "readme.gdb",
  227. "user.doc", and "develop.doc".  These files are in the emx and
  228. emx\doc directories.  For detailed descriptions of gcc (for all
  229. the various compiler switches) and gdb (for all the various
  230. debugging options), see the info files available in gccinfox.zip
  231. and gdbinfox.zip or in gccdoc.zip and gdbdoc.zip (as described
  232. above).
  233.  
  234. BOOKS THAT HELP YOU USE IT
  235.  
  236. The EMX package does not come with extensive documentation like
  237. that you would get with, for example, Borland C++.  gccinfox.zip
  238. (and gccdoc.zip) do not contain information on all the usual C
  239. library functions (like printf, calloc, fopen, sin, etc.). 
  240. develop.doc does contain a partial list of library functions,
  241. including a list of headers you need to include, what the
  242. functions do, and what parameters they accept -- but it is not
  243. complete.  Nevertheless, since gcc is ANSI C compliant (or at
  244. least close to it) and since g++ is very close to AT&T C++ 2.0,
  245. you can use just about any ANSI C and AT&T C++ 2.0 reference
  246. manuals.  I use the ones I got with an old version of Borland
  247. Turbo C++.  If you don't have such manuals, you should be able to
  248. find something suitable in a bookstore.  What you want (besides a
  249. description of the C and C++ languages) is a description of
  250. library functions, including a list of the headers you need to
  251. include to use each function.  For example, you want to be able
  252. to look in the book for a description of sin, find the
  253. appropriate page that tells you the prototype is "double
  254. sin(double)", that you need to include the header "math.h", etc.
  255.  
  256. Here are some suitable books:
  257.  
  258. Harbison and Steele, C:  A Reference Manual.  This book contains
  259. all the necessary information on library functions.
  260.  
  261. [Can people give me some more recommendations for books to put
  262. here?]
  263.  
  264. For those of you developing applications that use the PM or that
  265. use special OS/2 API functions (such as those for semaphores,
  266. threads, pipes, etc.), you will likewise need to get a book that
  267. discusses the various functions available.  
  268.  
  269. [Again, can people give me some recommendations here?  Is the
  270. Petzold book still good for OS/2 v2.0?]
  271.  
  272. CONCLUSIONS
  273.  
  274. I wrote this to help people get started with a free -- yet
  275. powerful -- 32-bit C and C++ compiler and debugger for OS/2.  For
  276. the price of a couple of books, you have a full C and C++
  277. programming system.  For the additional price of an OS/2
  278. programming book, you have a bargain-basement SDK.
  279.  
  280. If you find errors in this document, or if you have suggestions
  281. for its improvement, please let me know.  "BROOKE" is my GEnie
  282. address; "brooke@hope.caltech.edu" is my Internet address.
  283.  
  284. ADDENDUM
  285.  
  286. This is such a common question concerning gcc that I figured I
  287. should answer it from the start:  "What's wrong with printf?" 
  288. People find that printf doesn't immediately echo to the screen;
  289. the string is held in a buffer and dumped later, usually long
  290. after the printf statement is executed.  This is not really a bug
  291. -- ANSI C does not specify that printf should flush the I/O
  292. buffer (since it is probably more efficient on mainframe systems
  293. with terminals to flush the I/O buffer only by large chunks at a
  294. time).  So, to get a printf to work like you are probably
  295. accustomed to, put an fflush(stdout) after any printf you want
  296. immediately displayed. 
  297.