Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Preprocessing Mixed-Language Code
Carbon and Cocoa use different languages for their APIs. Carbon uses C while Cocoa uses Objective-C. In addition, Carbon programmers may prefer to use C++. When you build an application that uses any combination of C, Objective-C, Objective-C++, and C++ files, you must make sure the compiler performs the appropriate preprocessing. The filename extension of the file you are compiling indicates the kind of compilation that is done, as shown in Table 1.
In an Xcode project, you can override the filename extension of a source file by changing its file type. The file type determines how Xcode preprocesses and compiles the file. For example, suppose you want to add Objective-C code to a C source file with a .c
filename extension. You can examine and change the file type of this source file as follows:
Select the C source file in the project window.
Open the File Info window and select the General pane.
Find the “File Type” setting.
Now Xcode will compile the Objective-C code in the file.
You can also direct Xcode to handle all the source files in a project as one language, regardless of their filename extensions and file types. For example, suppose you want to add Objective-C code to many of your C++ source files. To specify that the compiler treat all source files as Objective-C++ files:
Select the project in the Groups and Files pane of the project window.
Open the the Project Info window and select the Build pane.
Find the “Compile Sources As” setting.
Change its value from “According to File Type” to “Objective-C++“.
Now Xcode will compile the Objective-C code in all your C++ files. You can also change this setting on a per-target basis.
For complete Xcode documentation, see the Tools category of the ADC Reference Library. The Xcode user guide is also available from the Help menu in the Xcode application.
Copyright © 2002, 2007 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2007-10-31