TweetFollow Us on Twitter

A Taste of Project Builder

Volume Number: 18 (2002)
Issue Number: 12
Column Tag: Getting Started

A Taste of Project Builder

by Dave Mark

In last month's column, we checked out http://developer.apple.com and downloaded and installed Apple's development tools. Since last month, I've gotten a number of emails from folks asking about other development environments, such as CodeWarrior, REALbasic and AppleScript Studio. Is Project Builder the only game in town? Absolutely not. Will we dig into these other environments? Absolutely.

The mission here is to cross the chasm, to get from Mac OS 9 to Mac OS X. There are a number of ways of skinning this fish and I want to explore them all, look at the advantages and disadvantages of each environment.

    Know of a tool that I should be covering that has not been mentioned yet? Are you on the dev team at Apple, REAL, or Metrowerks and want to get the story out on your tools? If so, drop me an email at feedback@mactech.com and let's tawk.

Compiling in the Unix World

I know that last month I said I was going to dig back into Project Builder this month. But I was playing around in the Terminal app and I thought you might appreciate a detour into the Unix command-line way of doing things. No matter how sophisticated the development tools get, it is important to connect the dots back to the Unix command-line. After all, the compiler that Project Builder uses is the exact same compiler we'll be using from the command-line. I promise, next month we'll get right back into Project Builder and walk through the debugger.

Start by creating a new folder to hold your source code. Next, use your favorite text editor to create a new source code file. Personally, I use BBEdit, but in a pinch, TextEdit or Word will do the job. Just be sure you save the file as a plain text file. No styles, no rtf, just plain text.

How to do this? In Text Edit, go to the Format menu and select Make Plain Text. Notice that your empty window is renamed to Untitled.txt (as opposed to just plain Untitled, which really corresponds to Untitled.rtf). For simple source code editing, this'll do the trick.

In Word, you can accomplish the same thing by doing a Save As... and selecting Text Only from the Format drop-down menu. Bottom line, your development environment expects a stream of plain-text characters and will make complicated squealing sounds if you feed it anything more complex.

Create a new text file, save it as "hello.c" and type in this simple block of code:

int main()
{
   printf( "Hello, world!\n" );
}

    Note that this is a simple C program and we've used the ".c" extension to remind us that this is C and not Objective C. As we'll see in a bit, Objective C files traditionally end with ".m"

Now that your source code is entered and saved, let's drop into the Unix universe and compile and run the darn thing. Just so your source code will be easy to find from within Unix, create a new folder on your desktop named "hello" and drop your hello.c file into the hello folder. Now you are ready to launch Terminal.

You'll find Terminal inside the Applications folder, within the Utilities subfolder. When you run Terminal, a new Terminal window will open and you will be automatically logged in under whatever account you logged into when you started up Mac OS X. Typically, a Terminal session will start something like this:

Last login: Fri Nov  8 17:56:05 on ttyp1
Welcome to Darwin!
[David-Marks-Computer:~] davemark%

The first line should reflect the current date and time, and the last line is your prompt. In my case, the prompt consists of the current directory in square brackets, followed by my login name and the percent sign "%". Don't worry about the specifics for now. In a future column, we'll spend some time looking into a variety of Unix commands. For now, we just need enough to get the job done.

    The program that prompts you to enter your Unix commands is called the shell. There are a variety of shell programs out there, all of which do the same basic task, prompting you for input, then taking action based on that input. Though on the surface they might all look the same, there are some big differences between shells. The Bourne shell is the most basic (and typically uses a "$" as its prompt). The C shell (seashell, get it?) is a next generation shell that is actually about 20 years old but is still widely used. The bash shell is based on the Bourne shell (bash stands for Bourne-again-shell. "Bourne-again" - get it?) The tcsh shell is the default shell for Mac OS X users and is likely the one you are using.

    Wanna check to see what shell you are using? Go into the Applications folder, then into the Utilities subfolder and run the NetInfo Manager utility. NetInfo is a database manager. Much off the system configuration info is stored in a NetInfo database. To find your shell setting, click on users in the first column, then on your user name in the second column. In the bottom half of the window, scroll down about half-way. You'll find the word shell listed in the Property column. Look for the shell setting in the Value(s) column. Mine is set to /bin/tcsh. My guess is, yours is too.

At the prompt, type the command "pwd" followed by a carriage return:

[David-Marks-Computer:~] davemark% pwd
/Users/davemark

The command pwd stands for print working directory and asks the shell to list the path to the current directory. By default, this is your login directory, typically /Users/xxxx where xxxx is your login name. You can view this directory in the Finder by opening a Finder window and clicking the Home icon. Same place. In my case, pwd prints out "/Users/davemark".

Next, type the command "ls", which asks for a list of the contents of the current directory:

[David-Marks-Computer:~] davemark% ls
Desktop   Library   Music     Public    Sites
Documents Movies    Pictures  SME

Note that one of these files and folders is the Desktop directory. That's the one we're interested in. Type "cd Desktop" to change directory into the Desktop directory (to make the Desktop directory the current directory):

[David-Marks-Computer:~] davemark% cd Desktop

Now do another "ls" to see what is in the Desktop directory:

[David-Marks-Computer:~/Desktop] davemark% ls
October2002DevToolsPatch.dmg            hello

Being the neat and tidy person that I am, I only have two files on my desktop. One is the October 2002 Dev Tools patch I downloaded from developer.apple.com. If you don't have this patch already, go download it (unless of course there's already a newer patch on the site) and apply it to the tools on your hard drive.

The other item on my desktop is the hello directory in which I placed my source code. Now let's "cd hello" to drop into the hello directory, then do a "pwd" to verify that we are in the right place:

[David-Marks-Computer:~/Desktop] davemark% cd hello
[David-Marks-Computer:~/Desktop/hello] davemark% pwd
/Users/davemark/Desktop/hello

Looks good. Note that we also could have entered the command "cd /Users/davemark/Desktop/hello" and we would have winded up at the very same place. We just descended into this directory one level at a time so you could get a better sense of what we were doing.

Enter an "ls" command to verify that the source code file is inside the hello directory:

[David-Marks-Computer:~/Desktop/hello] davemark% ls
hello.c

Yup. It's there. Now we can compile the code. Here's how we do it:

[...Desktop/hello] davemark% gcc hello.c -o hellotest

The command is "gcc" and the arguments it takes are "hello.c", "-o" and "hellotest". Without getting into too much detail (we'll do plenty when we really start digging into Unix), we've asked the compiler (a program called "gcc") to compile the source code in the file "hello.c" and to save the output in a file named "hellotest".

    Actually, The real command is gcc3, but Unix makes aliases to this command (called a symbolic link) with the names "cc" and "gcc". In the olden days, there was only one C compiler for Unix and its name was "cc". Not so many years ago, the GNU folks (GNU stands for GNU is Not Unix - recursive, get it?) made the gcc compiler available and it made its way into many versions of Unix, especially systems based on the Linux kernel. More on GNU, Free Software, and Linux in a future article. If you have access to the Sundance Channel on DirecTV or the like, look for a movie called Revolution OS. A great little documentary.

    Want to prove that cc and gcc all link to gcc3? Use ls. Here's how I did it:

    davemark% ls -l /usr/bin/cc
    lrwxr-xr-x  1 root  wheel  4 Nov  8 22:49 /usr/bin/cc -> gcc3
    davemark% ls -l /usr/bin/gcc
    lrwxr-xr-x  1 root  wheel  4 Nov  8 22:49 /usr/bin/gcc -> gcc3

    To learn more about the gcc3 compiler, read the release notes. You'll find them on your hard drive in:

    /Developer/Documentation/ReleaseNotes/GCC3.html

Now type "hellotest", to see if this worked:

[David-Marks-Computer:~/Desktop/hello] davemark% hellotest
hellotest: Command not found.

This may have worked for you, but most likely you got the error "Command not found". Basically, your shell is telling you that it doesn't have a command named "hellotest". Every time you type a command at the shell prompt, the shell looks through its path list to see if it can find a program with a name matching the command you just typed. It has a set of directories that it always searches to find commands. Unless you specifically tell it to, it will not look in the current directory for commands. To force it to look in the current directory for hellotest, type "./hellotest", where the . stands for the current directory:

[David-Mar...puter:~/Desktop/hello] davemark% ./hellotest
Hello, world!

Hey! It worked! Cool! Check out Figure 1 to see this whole sequence in a single Terminal window.


Figure 1. Compiling and running hello.c in the Terminal app.

Objective-C is C with Objects

Once you know C, you have come a long way towards learning Objective-C. Objective-C is basically C with objects added into the mix. Let's rename "main.c" to be "main.m". Remember, Objective-C source files traditionally end with ".m". Use the "mv" command to rename the file:

[Davi...uter:~/Desktop/hello] davemark% mv hello.c hello.m
[Davi...uter:~/Desktop/hello] davemark% ls
hello.m   hellotest

Now we need a slightly different compile command, one that knows we are compiling with the Objective-C library:

[Da...:~/Desktop/hello] davemark% gcc hello.m -l objc -o hey
[Da...:~/Desktop/hello] davemark% ./hey
Hello, world!

    Every so often someone asks me what languages to learn first in order to become a Mac developer. With the release of Mac OS X, the choices have shifted. There are choices like REALbasic and AppleScript, as well as a variety of shell languages and web-focused languages (Perl, JavaScript, PHP). And, of course, the languages Objective-C, C++, and Java.

    If your goal is to become a professional Mac OS X developer, you should become familiar with all three of Objective-C, C++, and Java, so you can make the right choice of languages when the time comes. But (and here's where the controversy kicks in), I strongly believe you should first give yourself a grounding in C, the language on which these other three are based. I believe it is critical to understand where C leaves off and where these other languages begin. Given that Objective-C, C++, and Java all support a different object model, all they really have in common, syntactically, is C. There are about a million great C books out there and, if you don't have the bucks to spend, there are some on-line tutorials for C that are free.

    Learning C might seem like a waste of time, especially given that you can't produce an actual Mac OS X Cocoa app written in C, but it is not. Learning C first is both modular and efficient. C is pure procedural language. The others add the concept of object programming into the mix. Simplify the learning curve. Learning pure C is simpler, plus you'll add another language to your arsenal. And best of all, once you know C, learning these other languages will be much simpler. C is a great foundation language.

    So listen to your Uncle Dave and learn C first, then tackle Objective-C. Then you can play with C++ and Java while you are also learning Cocoa, REALbasic, PHP, etc.

The command "gcc hello.m -l objc -o hey" tells the compiler to compile the source file hello.m, linking with the objc library, and producing an output file named "hey". We then run "hey" and it produces the correct results. Cool! Our first Objective-C program. Not much Objective-C there, but we've learned a couple of things. A bit about Unix, the fact that when we installed the developer tools we are able to access them directly from within a Unix shell, and the important fact that Objective-C is, in effect, an extension of C.

Till Next Month...

It is definitely worth your while to become comfortable with the Unix command line. Commands like pwd, ls, and cd might seem arcane, especially when compared with the beauty of the Mac OS X gui, but it is important to be able to lift up the hood and tinker with the innards. There are a number of terrific books out there for learning Unix. If you go to the bookstore, head for the Unix section and check out the Unix "how to use the shell" books, as well as a general Unix overview book. There are a couple of Mac OS X-specific Unix books that are due for release around this time. Those might be good to add to the collection. On the other hand, there's a ton of good sites on the web. I'll try to gather some good URLs on spiderworks.com.

Next month, we'll dig into the debugger. Until then, why not spend a bit of time digging through some Unix documentation and, if you have the spare cycles, start plowing through some of the docs in /Developer/Documentation/. See you then...


Dave Mark is very old. He's been hanging around with Apple since before there was electricity and has written a number of books on Macintosh development, including Learn C on the Macintosh, Learn C++ on the Macintosh, and The Macintosh Programming Primer series. Check out Dave's web site at http://www.spiderworks.com

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.