Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Command Line Compiling and Debugging

When you write programs for Mac OS X or iOS, you should write, compile, and debug your programs using Xcode, Apple’s integrated development environment (IDE). You’ll learn how to set up a simple Xcode project in Chapter 4, “Your First Objective-C Program.” However, for the simple C programs required in the exercises in this chapter and the next chapter, you may find it easier to write the programs in your favorite text editor and then compile and run them from a command line.

To compile from the command line, you will need:

  1. A terminal window. You can use the Terminal app (/Applications/Utilities/Terminal) that comes with Mac OS X. If you are coming from another Unix environment, and you are used to xterms, you may prefer to download and use iTerm, an OS X native terminal application that behaves similarly to an xterm (http://iterm.sourceforge.net/).
  2. A text editor. Mac OS X comes with both vi and emacs, or you can use a different editor if you have one.
  3. The Apple Developer tools. You can get the current version of Xcode and the Developer tools from the OS X App Store (they’re free).

The first step is to check whether the command line tools are installed on your system. At the command prompt type

which clang

If the response is

/usr/bin/clang

your command line tools are already installed. If the response is

clang: Command not found.

the command line tools are not installed on your system and you must use Xcode to install them. To install the command line tools:

  1. Open Xcode.
  2. Choose Xcode > Preferences... from the menu.
  3. When the Preferences panel opens, click the Downloads tab and then click on Components.
  4. Finally, click the Install button for Command Line Tools.
  5. When the installation is finished, you may close Xcode.

You are now ready to compile. If your source code file is named MyCProgram.c, you can compile it by typing the following at the command prompt:

clang -o MyCProgram MyCProgram.c

The -o flag allows you to give the compiler a name for your final executable. If the compiler complains that you have made a mistake or two, go back to fix them, and then try again. When your program compiles successfully, you can run it by typing the executable name at the command prompt:

MyCProgram

If you want to debug your program using gdb, the GNU debugger, or LLDB, you must use the -g flag when you compile:

clang -g -o MyCProgram MyCProgram.c

The -g flag causes clang to attach debugging information to the final executable.

To use gdb to debug a program, type gdb followed by the executable name:

gdb MyCProgram

Similarly, to use lldb you type lldb followed by the executable name:

lldb MyCProgram

Documentation for gdb is available at the GNU website, www.gnu.org/software/gdb/, or from Apple at http://developer.apple.com/mac/library/#documentation/DeveloperTools/gdb/gdb/gdb_toc.html. In addition, there are many websites with instructions for using gdb. Search for “gdb tutorial.”

You can learn more about LLDB by watching the Apple video at http://devimages.apple.com/llvm/videos/LLDB_Debugging_Infrastructure.mov or by going to the LLDB website, http://lldb.llvm.org/.

  • + Share This
  • 🔖 Save To Your Account