USING C++ (GNU C++) UNDER UNIX ------------------------------ Source files (C++ code files) should have one of the following suffixes (extensions): `.C', `.cc', `.cxx', `.cpp', or `.c++'; 1. Writing code ~~~~~~~~~~~~~~~ Type up the source file in one of the UNIX editors: emacs or vi. Emacs is the most popular one and *much* easier to use. Give it a name with one of the extensions above. Note: Any person who works in a UNIX environment knows and loves Emacs, even those hackers who claim they don't care. Once you get past the finicky abbreviations such as Ctrl+k "kill-to-end-of-line", Ctrl+y "yank-back-what-you-just-killed", Ctrl+a "goto-beginning-of-line", Ctrl+e "goto-end-of-line", Ctrl+v "page-down", ESC+v "page-up" etc., you can try ESC-/ "guess-at-expansion" and Emacs will try to complete the word you started to type based on what you already have in the file. Ever wondered why some Unix programmers use very long names? Also, Emacs has a number of modes, which it loads automatically based on the extension of the file. In C++ and C modes hitting Tab automatically indents your line of code according to the rules of good style. Try it. Don't forget to include all the libraries (collections of precompiled C or C++ procedures) and header files (collections of declarations and procedures in source, usually with .h suffix) you need through #include directive. If your program is getting large it's a good idea to put declarations and some procedures into header files and include them into the main code, instead of keeping all the code in one file. 2. Compiling and running a program. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To compile a source file, say tt.cc, and to obtain an executable, say `tt', run this command from the UNIX command prompt: >g++ -o tt tt.cc The program `tt' will be created and you can run it: >tt If you just say >g++ tt.cc your executable will be called a.out All the header files which you included from tt.cc will also be compiled. 3. Capturing program output into a file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Use `script' command. For example, to capture the output of tt into tt.out: >script tt.out >tt ... ... >exit // this will end the script, out put will be in tt.out 4. Making object files and linking. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you want just to compile source files and link it into an executable later, do (suppose you have files tt1.C, tt2.cpp, tt3.cc): >g++ -c tt1.C >g++ -c tt2.cpp >g++ -c tt3.cc The object files tt1.o, tt2.o, tt3.o will be created. To make an executable `tt' out of them: >g++ -o tt tt1.o tt2.o tt3.o This is a useful option, it allows you to break the program into independent modules and assemble them later. 5. Debugging ~~~~~~~~~~~~ There is a C/C++ debugger by GNU which you can use: gdb. To debug your program you have to compile it with -g option: >g++ -g -o tt tt.cc ^ -g could be put anywhere on the command line. To run: >gdb tt Try getting help first, type help, you'll get a menu of topics and can get help an any topic like this: gdb> help . This debugger allows you to set break points, step through code in different ways, examine current data etc. To run your program under gdb just type `run'. SEE ALSO: `man g++', `man gdb'