Building Carbon Applications

This chapter describes how to use the tools and libraries provided with the Mac OS X Developer Tools CD to build Carbon applications for both Mac OS 9 and Mac OS X. You can also install the Carbon system extension, CarbonLib, to run Carbon applications on Mac OS versions 8.1 and later.

Native Mac OS 9 Versus Mac OS X’s Classic Environment

If you plan to build, run, and debug Carbon applications for both Mac OS 9 and Mac OS X on a single system, the Mac OS X application Classic.app provides a convenient environment for running your development system. You can easily switch between the two environments, and launch applications in either.

If you prefer to develop on a native Mac OS 9 system (that is, on a computer running Mac OS 9 instead of Mac OS X), you’ll need to reboot to run Mac OS X and test your Carbon application in that environment.

If you have two computers, you might want to run Mac OS 9 on one computer and Mac OS X on the other. To transfer files between them, you can use one of the following methods:

Development Scenarios

There are a number of tools and processes you can use to build and debug Carbon applications. This section describes three scenarios that Apple recommends, and the advantages of each.

Using CodeWarrior to Build a CFM Carbon Application

This is the most likely scenario if you’re porting an existing Mac OS 9 application to Carbon, especially if you’re already using CodeWarrior. You’ll continue to use the Mac OS development tools and processes you’re familiar with, and you’ll create CFM applications that can run on both Mac OS 9 and Mac OS X. The only difference is that you’ll include the CarbonLib stub library in your CodeWarrior project.

Using CodeWarrior to Build a Mach-O Carbon Application

Metrowerks CodeWarrior Pro version 8.0 and later has support to build Mach-O applications on Mac OS 9, as well as build and debug applications on Mac OS X. If you have a second computer, you may also want to investigate whether Metrowerks’ two-machine debugger suits your needs, as it can debug CFM applications on both platforms. Contact Metrowerks for information about these products.

Using Project Builder to Build a Mach-O Carbon Application

Project Builder is Apple’s integrated development environment (IDE) for Mac OS X. It offers a comprehensive feature set that includes source-level debugging. Project Builder is a good choice if your application will run only on Mac OS X, and you want to take advantage of features available only on that platform. However, you can’t use Project Builder to build a CFM application, so if you want your program to run on both platforms you’ll either need to use CodeWarrior or other tools to create a CFM version for Mac OS 9.

See the Project Builder online help documentation for more information about creating Mach-O Carbon applications.

Building a CFM Carbon Application With CodeWarrior

If you plan to use Metrowerks CodeWarrior, CodeWarrior Pro version 8.0 or later is recommended. You can run CodeWarrior natively on Mac OS 9 or Mac OS X.

Preparing Your Development Environment

Before you start Carbon development with CodeWarrior, you’ll need to install the tools and libraries provided with the Mac OS X Developer Tools CD or the Carbon SDK.

  1. Copy the Carbon Support folder to the Metrowerks CodeWarrior folder on your hard disk. The Carbon Support folder should reside in the same folder as the CodeWarrior IDE application.

  2. Copy the appropriate Carbon system extension (CarbonLib or DebuggingCarbonLib) from the Carbon Support:CarbonLib folder to your Extensions folder. You should keep only one Carbon extension in your Extensions folder at any time.

    • CarbonLib is the standard implementation of Carbon for Mac OS 8.1 or later.

    • DebuggingCarbonLib is a debugging version of CarbonLib.

  3. CarbonLib is included in all versions of Mac OS 9 as well as the Classic environment on Mac OS X. However, to make sure you are using the latest version, you should replace the default CarbonLib with the latest one available (in this case version 1.6).

  4. To avoid the potential for data loss in the event that you need to reinstall Mac OS X, ensure that your CodeWarrior project files and source code reside on a separate hard disk.

Building Your Application

To build a Carbon version of your application, you’ll need to make the following changes to your CodeWarrior project.

  1. Add the following statement to one of your source files before including any of the Carbon headers:

    #define TARGET_API_MAC_CARBON 1

    This conditional specifies that the included header files should allow only Carbon-compatible APIs and data structures. You can include the conditional in a prefix file if you wish.

  2. Add the CarbonLibStub stub file to your project.

  3. Ensure that your project is not linking to any libraries that are not Carbon compatible. For example, the MPW ANSI C library is not Carbon compatible. Note that you should not directly link to InterfaceLib when you are linking with CarbonLib. On Classic Mac OS, CarbonLib will return an error to the Code Fragment Manager if your application attempts to link to both CarbonLib and InterfaceLib, causing the application launch to fail.

  4. Ensure that your CodeWarrior access paths and other target settings are correctly specified. See the sample code included with the Carbon SDK for examples of how to do this.

Running Your Application on Mac OS 9

You can launch your application from the Finder on a Mac OS 9 system by double-clicking its icon. To run Carbon applications on Mac OS 8 (version 8.1 or later), you must install the CarbonLib or DebugCarbonLib extension in the Extensions folder.

Running Your Application on Mac OS X

As long as your application resides on an HFS Plus disk, you can launch it by double-clicking its icon.You cannot launch applications from a standard HFS format disk on Mac OS X.

You can also use the command-line tool LaunchCFMApp to launch CFM applications from a terminal window in Mac OS X. If the CFM application is in the current working directory, the command is:

/System/Library/Frameworks/Carbon.framework/Versions/A/Support/LaunchCFMAppfilename

If the application is in a different directory, you must specify the path.

Note that if your application does not contain a 'plst' resource, a bundled Info.plist file, or a 'carb' resource, Mac OS X opens the application in the Classic compatibility environment. To ensure that Mac OS X properly recognizes your application, it must include a resource of type 'plst' with ID 0, a bundled Info.plist file, or a resource of type 'carb' with ID 0.

Building a Mach-O Carbon Application With CodeWarrior

Before building a Mach-O version of your application with CodeWarrior, you should follow the instructions in the previous section for building a CFM Carbon application. After you’ve successfully built and tested a CFM version of your application on Mac OS 9, you can use CodeWarrior to build a Mach-O version for debugging on Mac OS X.

Preparing Your Development Environment

Metrowerks CodeWarrior Pro 8.0 and later has support for Mach-O applications. See the Metrowerks documentation for more information.

Building Your Application

Refer to your Metrowerks CodeWarrior documentation for instructions on building Mach-O versions of your application.

Running Your Application on Mac OS X

CodeWarrior creates an executable Mach-O binary that includes a resource fork. As long as this file resides on an HFS Plus disk, the resource fork remains intact and you can launch the application by double-clicking its icon.

Building a Mach-O Carbon Application With Project Builder

Project Builder is included on the Mac OS X Developer Tools CD. Instructions for building Mach-O Carbon applications are available in Project Builder’s online help documentation.

Building Applications Using MPW

An alternative to using CodeWarrior or ProjectBuilder for CFM applications is to use Apple’s MPW development environment, which is now available as a free download from the following website:

http://developer.apple.com/tools/mpw-tools/

You should place the CarbonLibStub stub file in the folder Interfaces and Libraries:Libraries:SharedLibraries, while the Carbon version of the Universal Headers should be placed in Interfaces and Libraries:Interfaces:CIncludes.

You compile your application and link against CarbonLibStub just as you would when using CodeWarrior. Note however that Carbon applications must contain a ‘SIZE’ resource, and the resource must have the acceptSuspendResumeEvents flag set. While CodeWarrior adds a ‘SIZE’ resource automatically, you must create your own when building with MPW.

In addition, you need to override your default entry point (by using the -m option in PPCLink) with one of the following:

The version of StdCRuntime.o containing __appstart, as well as a Carbon-friendly version of the CreateMake tool, will be available from the following web site:

http://developer.apple.com/tools/mpw-tools/updates.html

You should also examine the PackageTool sample application in the Sample Code folder of the CarbonLib SDK and the MPW release notes for more specifics about the build process.

Debugging Your Application

You can debug Carbon applications on Mac OS 9 or Mac OS X using the Metrowerks debugger. You can use also this debugger with two networked machines (for example, one running Mac OS 9 and the other running Mac OS X). Contact Metrowerks for more information.

You can also debug Carbon applications on Mac OS X using GDB, which you can run from a terminal window. Although GDB cannot directly debug a CFM application at this time, there is a workaround that lets you perform low-level debugging on a CFM application. You’ll use GDB to debug LaunchCFMApp, a Mach-O program that launches CFM applications.

This workaround has the following features and limitations:

To debug your CFM application:

  1. Launch the Terminal application: /Applications/Utilities/Terminal.app.

  2. Enter gdb /System/Library/Frameworks/Carbon.framework/Versions/A/Support/LaunchCFMApp.

    GDB loads the LaunchCFMApp program.

  3. If you want, set breakpoints at any Carbon function with the br command.

    For example, you may want to set a breakpoint at the DebugStr function, because DebugStr prints its argument without stopping the program’s execution. Enter br DebugStr at the GDB prompt.

  4. At the GDB prompt, enter r <app-pathname>, where <app-pathname> is the full pathname for your CFM application.

    To enter the application’s pathname, drag the application’s icon to the Terminal window.

    LaunchCFMApp launches your application.

To pause your application’s execution at any time, press Control-C in the Terminal application. To continue your application, enter cont. For more information on GDB, enter help.

Here are some additional hints that you might find useful: