Avoiding Xcode
Xcode provides a front end for command-line tools like the GCC compiler and the GDB debugger. What this means is you can avoid Xcode and use the command-line tools if you want. There are several situations where you might want to bypass Xcode. If you’re learning C or C++, you’ll start out by writing small programs that contain one source code file. Invoking GCC directly would be easier for you than creating an Xcode project, especially if you’re familiar with the Unix command line. People writing cross-platform applications might want to run build scripts so their application builds the same way on multiple operating systems. Or you just might hate Xcode.
This post shows you how you can program on Mac OS X and avoid Xcode. By avoiding Xcode, I mean not having to use it. You still have to install Xcode. I’m sure it’s possible to install gcc without installing Xcode, but you’ll save yourself time and aggravation by just installing Xcode and not using it.
Writing and Compiling Your Code
Write your source code in the text editor of your choice. There are many text editors available for Mac OS X. TextWrangler and Smultron are free. TextMate, BBEdit, and ForgEdit aren’t free. If you search the Internet, I’m sure you’ll find more editors.
When you’ve finished writing your code, it’s time to compile it into a running program. Launch the Terminal application and navigate to the directory where your source code files are. Now you can compile your code. The simplest way to compile your code with GCC is to use the -o option. It takes the following form:
gcc -o AppName FilesToCompile (C programs)<br />g++ -o AppName FilesToCompile (C++ programs)<br />
If you wrote a Hello World program in C++ and saved your file as main.cpp, you would enter the following command to compile your program:
g++ -o HelloWorld main.cpp
GCC will compile your code and create an executable file named HelloWorld. You can run the program by double-clicking the executable file in the Finder.
Makefiles
Running GCC directly is fine when you’re writing small programs, but problems arise with larger programs. Imagine you’re writing a program that has 12 source files, links to four libraries, and uses six compiler flags. Having to type in the names of the source files, libraries, and compiler flags every time you want to compile your program would be tedious and error prone.
The solution is to use a makefile, which is a text file. Instead of running GCC directly, you put everything you need to compile your program in the makefile: source file names, compiler flags, paths to libraries, and paths to headers. Run the make command to compile your program using the makefile.
I’m not an expert on makefiles so I’m not going to show you how to create a makefile, but there are many tutorials on the Internet. Do a Google search for makefiles and you’ll find the tutorials.
If you don’t want to use makefiles, there are several cross-platform, open source build systems available. Three of the more popular systems are SCons, CMake, and autoconf. Those of you writing larger programs should look into these build systems if you don’t want to use Xcode.