Back to all guides

Introduction to UNIX software development

Typical development environment using ssh, screen, joe, gcc, gdb on UNIX (Linux, BSD) systems

Illustrated by example, with screen shots


Basics: editing and compiling code

1. Use an ssh (secure shell) client to log into your remote UNIX account. If you're using Windows, try the excellent free PuTTY program. In the PuTTY configuration specify 'SSH' mode rather than Telnet. Note that the PuTTY window can be resized, and allows selecting text (automatically copies to clipboard). Right-clicking in PuTTY will paste the clipboard contents, making code editing easier. If you are using Mac OS X or another UNIX like system, connect to your remote account using the command:
'ssh username@hostname'

secure shell

2. Use the 'screen' command to start the terminal multiplexer. This allows you to switch between several independent screens within your shell, and detach and resume your screens in case of a disconnect.

screen multiplexer

3. Start the joe editor. Specify a source code file name to edit from the command line - e.g. 'joe hello.c' After editing, save what you have so far using the key combination CTRL+K, S, ENTER. Help is available with CTRL+K, H

joe

4. Create a new screen, using CTRL+A, C. This will create a new, blank shell. Practice switching between the screens using CTRL+A, N (for next). You can create many windows and also switch between them using CTRL+A, [number] to go directly to screen 0, screen 1, etc.

5. Compile your program using 'gcc -o hello -Wall hello.c' where -Wall enables all warnings, highly recommended. The -o option specifies the output binary file name. Run the program using './hello'

compile hello.c

6. To make changes, switch back to your code editor with CTRL+A, N (next window). Remember to save changes to the file before you recompile, CTRL+K, S, ENTER.


Detaching and resuming screens

The screen multiplexer allows you the luxury of resuming all screens/shells underneath the main screen in case you deliberately or accidentally disconnect from your UNIX account. You can use this feature to suspend your development environment and resume work later, or to protect against a connection failure ruining your work. To detach an active screen session, use: CTRL+A, D (for detach). You can now exit the shell and go have a night out on the town, but your previous windows are still active! Your session should remain active so long as the UNIX server is not rebooted. (Note that if your connection accidentally got broken, it is the equivalent of doing this detach command -- try this by closing an active PuTTY window).

detached screen

To resume your screen session, log back into your UNIX account, and run 'screen -r' (for resume). You should get back to your previous environment, with all previous screens.


Basic debugging after a crash

1. Add a deliberate bug to your previous hello.c, as shown. Save the modified file with CTRL+K, S, ENTER

buggy hello.c

2. Use CTRL+A, N to switch to another window (or CTRL+A, C to create one if needed)

3. Enable core dumping (upon crash) underneath your shell with 'ulimit -c unlimited'

4. Compile your software with the -g option (this adds debugging info to the output binary), 'gcc -o hello -g -Wall hello.c' and run './hello' now, to get a core dump. The resulting core file will be in the same directory, named core or core.pid (process ID).

core dump

5. Start the debugger, gdb. Assuming the core was dumped to a file named core, run: 'gdb hello core' and wait for the prompt. Issue the 'bt' command in the debugger to get the stack trace. This shows important details at the time of the crash, including function call depth and even the line number in hello.c (line 7, null pointer reference).

gdb stacktrace

Exiting

1. Save all your work! In the editor, CTRL+K, Q (for quit)

2. Use 'exit' to exit each individual screen. Eventually screen itself exits.

3. Use 'exit' again to disconnect from the UNIX shell.


Advanced use of joe

Joe's own editor (joe) strikes an excellent balance between features and ease of use. Newer versions of joe even support syntax highlighting, recognizing the C/C++, Java, etc. code as you type it in. Covered below are some of the features of joe that are immediately useful to programmers.

Autoindent: maintains tab indents from the margin as you code, keeping code neat and organized without excessive keystrokes. To enable, use CTRL+T (to access options) and then use the arrow keys to select Autoindent, pressing space bar to toggle.

joe's autoindent

Find/replace: type CTRL+K, F and enter a string to search for. Upon a match, you can (I)gnore, (R)eplace, etc. Use (I)gnore and keep hitting CTRL+L to find the next match.

Copy/paste: Note that you can often just use PuTTY's built in copy and paste (right-click mouse to paste) to move small amounts of text around the window. However, if you want to copy or move blocks of code you should use joe's facilities to maintain formatting. Position the cursor above the starting point and type CTRL+K, B to begin the block. Then move the cursor past the end and define the block end with CTRL+K, K. The block of text is now selected. You can move the block to some destination using CTRL+K, M or alternatively CTRL+K, C for copy.

Multi-level undo: simple, but a life saver. The magic keystroke is CTRL+SHIFT+[hyphen] meaning CTRL+'_' (underscore)


Copyright (C) 2005 SysDesign