CodeWarrior 使用教程
第一课 认识 CodeWarrior

Translated from CodeWarriorU.COM
CodeWarrior 能做些什么?
当你知道自己能写更好的程序时,你一定不会再使用别人开发的应用程序。但是常常会发生这种情况,就是当你写了无数行代码后,却不到使得整个程序出错的那一行代码,导致根本没法编译和链接整个程序。这可能更令人灰心丧气。
本文将告诉你如何使用 CodeWarrior 这一工具解决上述问题。
从现在开始,我们将集中精力学习如何在 CodeWarrior 中使用 C/C++ 进行编程。为了学习本课程,你必须已经能够比较熟练地使用上述两种语言之一。CodeWarrior 也可以支持 Java 开发,但那是另一门课程的内容。本课程仅限于在 Windows 平台上使用 CodeWarrior 进行的开发。一旦你精通了 CodeWarrior 编程后,你可以试试在其它平台上使用 CodeWarrior。本文中讨论过的大部分内容都可以应用到开发 Mac 应用程序中。
CodeWarrior 能够自动地检查代码中的明显错误,它通过一个集成的调试器和编辑器来扫描你的代码,以到并减少明显的错误,然后编译并链接程序以便计算机能够理解并执行你的程序。你所使用过的每个应用程序都经过了使用象 CodeWorrior 这样的开发工具进行编码、编译、编辑、链接和调试的过程。现在你在我们的指导下,自己也可以去做这些工作了。
你可以使用 CodeWarrior 来编写你能够想象得到的任何一种类型的程序。如果你是一个初学者,你可以选择编写一个应用程序(比如一个可执行程序),比如象微软公司的文本编辑器 WordPad 这样的应用程序。
应用程序可能是最容易编写的程序了,而那些庞大的商业软件,比如象 Adobe PhotoshopMicrosoft Word 以及 CodeWarrior 软件都是极其复杂的。其它类型的程序指的是控制面板(control panels),动态链接库(dynamic linked librariesDLLs) 和插件(plug-ins)。我们先来简单的讨论一下这些类型的程序。
Windows 中,控制面板程序是一些(通常比较小的)存放在控制面板目录下的程序,可以在开始菜单的控制面板项中看到它们。控制面板实际上是设置系统功能的一个简单界面,比如可以在控制面板中完成诸如设置音量和鼠标移动速度这样一些参数。
动态链接库 (DLLs) 提供了一些很有用的被系统和应用程序共享的函数。通常它们都是没有用户界面的。
插件 (Plug-ins) 有点类似于 DLLs,插件也是可供其它软件使用的小程序,但插件只能被特定的软件使用。比如说,Photoshop 的插件允许程序员方便地扩展 Photoshop 的功能,而不需要去修改它的源代码。同样的,许多 Web 浏览器也使用插件来增加功能,或者为了让你能够看到页面上的新的内容类型。实际上,有许多程序,包括 CodeWarrior,都使用这种插件结构。
一旦你掌握了 CodeWarior 和你所使用的操作系统的编程接口时,你可以编写许多其它类型的程序。实际上,本课程也是你深入学习其它编程方法的基础课程。你已经到了一个最优秀的地方开始你的编程之旅。好,现在请坐好,放松,去吸取这门课程的精华吧,你将踏上一条成为一个编程武士的道路!
CodeWarrior 入门
CodeWarrior 是一个复杂的应用程序,你必须花点时间来了解它的各种各样的组件和功能。第一课将讲述 CodeWarrior IDE 的安装。我们将学习菜单、窗口和其它的一些方面。建议你最好一边学习本课程,一边学习使用 CodeWarrior 的集成开发环境。我们从运行 CodeWarrior 开始,如果你按照默认方式安装的 CodeWarrior 软件,那么在 Windows 的桌面上将会有一个该软件的快捷方式图标。双击该图标启动 CodeWarrior 的集成开发环境。如果在 Windows 桌面上没有这个快捷图标(这种情况经常发生),你可以在 C:\Program Files\Metrowerks\CodeWarrior\Bin 中到 文件,然后运行它。
工具条
启动 CodeWarrior 后,你将会看到,在屏幕上方的菜单下面有一个工具条。这个工具条(见图1-1)包含了一些常用菜单项的快捷方式。在后面的章节中,你将学到如何设置这个工具条,甚至是整个 IDE 环境。现在,你应记住,这个工具条用于快捷使用 IDE 中的功能。
1-1: CodeWarrior IDE 的工具条
文件菜单
CodeWarrior IDE 的菜单是按照标准方式设置的。其中的文件菜单用于处理和文件相关的一些操作,比如创建、打开、保存和打印等等。
编辑菜单
CodeWarrior 的编辑菜单和其它的 Windows 应用程序也很相像。其中包括了剪切、复制和粘贴等操作,以及其它一些使得程序员能够更方便地管理源码版面布局的选项。在编辑菜单中还有一个 Preferences 项,我们在这一课的后面将提到它。
查看菜单
查看菜单(1-2)用于安排工具条和其它窗口在 CodeWarrior 环境中如何显示的选项。所谓的其它窗口包括许多特殊的窗口,比如观察点(Watchpoints)窗口,表达式(Expressions)窗口,过程(Processes)窗口和全局变量(Global Variables)窗口等。
1-2: 查看菜单中的工具条子菜单
查菜单
查菜单用于在单个文件或硬盘的目录中查指定的代码。你可以使用它来方便地替换文本块或在你的代码中搜索指定的项目。即使是在小工程中使用这个工具也非常方便。
工程菜单
工程菜单中的工具用于管理 CodeWarrior 工程。一个工程包括组成你正在编写的程序的所有的文件,包括头文件、源代码和库文件。工程窗口显示了所有这些文件的名称。我们将在第二课中深入讨论这部分内容。
调试菜单
这是在编制程序中最常用到的工具。我们将在第五课中详细介绍它的使用方法。
窗口菜单
用于在 CodeWarrior 环境管理窗口显示方式的菜单。
帮助菜单
通过帮助菜单可以到网上寻求关于 CodeWarrior 任何问题的在线解答。够酷的吧?
了解 CodeWarrior 集成开发环境的设置
CodeWarrior IDE 提供了许多设置以便让你定制你的工作环境。当你选择了编辑菜单中的 Preferences 项时,你将会看到一个设置对话框(如图1-3)。在该对话框中,有控制 CodeWarrior 编辑器、调试器和其它许多功能的界面和行为的选项。在这一节中,我们将学习这些设置的使用。
1-3:  设置对话框
你可以在自己的 CodeWarrior 中试试上述这些设置项。你可以先点击问号标志,然后点击你感兴趣的项目,就可以得到一个有关该项目的用途的简短介绍,也可以从帮助菜单中得到更详细的信息。
通用设置
编译设置(Build Settings): 选择是否在执行编译之前保存已打开的源文件,以及有多少内存被用于编译工作;
IDE 之外(IDE Extras): 几个独立的设置。比如指定 CodeWarrior 是否使用一个第三方的文本编辑器——因为集成的编辑器并不是很完美,这可以通过指定一个你惯用的编辑器来替代它;
插件设置(Plug-In Settings): 供插件开发商调试他们的插件;
隐藏文件夹(Shielded Folders): 在这里指定的文件夹在工程设计期间,或执行查和比较操作期间,将要被忽略掉。如果在你的工程级有一个巨大的数据文件目录,而你又不想让这些文件降低 CodeWarrior 的操作速度时,这个设置就很管用了;
资料树(Source Trees): 用于指定 CodeWarrior 在编译程序时用不着的目录。
编辑器设置
编辑器设置(Editor Settings): 几个用于定制编辑器显示、管理文本和窗口的设置项;
字体和制表符(Fonts and Tabs): 设置编辑器中的文本大小、字体、制表符和其它显示设置;
文本颜(Text Colors): 用于指定特定语言元素(比如程序的注释)在编辑窗口中的显示的颜。
调试器设置
显示设置(Display Settings): 几个用于定制调试器显示的设置项;
视窗化(Windowing): 设定调试器如何管理它的窗口(比如隐藏所有打开的编辑器窗口)
全局设置(Global Settings): 几个用于定制调试器在全局层次如何工作的设置。比如当一个包含了程序调试信息的文件被打开时,是否启动这个程序;
远程连接(Remote Connections): 允许通过 TCP/IP 进行远程调试。这样,你就可以在地球的另一边调试你的软件了;
Java 设置/Java 调试(Java Settings/Java Debugging): 允许你设置特定的 Java 编程语言选项。本课程不涉及这部分内容。
RAD 工具
Layout 编辑器: 几个用于在 CodeWarrior 中定制快速应用程序开发工具的使用的设置。
正如你所见到的,CodeWarrior 有许多设置项和选项。设置完后点击“Save”按钮保存你所作的修改,点击“Close”按钮将忽略你所作的所有修改。在进入下一节课之前,多练习几次。最后请确认默认的文本文件格式(Default Text File Format)”(在编辑器设置面板中)保持为 DOS 设置。
(待续)
附原文:
Lesson 1: Getting to Know CodeWarrior
What Will CodeWarrior Do for Me?
It can be frustrating to use other people's applications when you know you can make something better. But it can also be frustrating when you have a zillion lines of code and can't find the one typo that's making the whole application go kablooey -- not to mention getting it all compiled and linked and so on.
This is why you are here. And this is why CodeWarrior was created.
For now, we'll focus on writing programs in CodeWarrior using the C or C++ programming languages. This course assumes that you're pretty comfortable using at least one of these languages. CodeWarrior also supports Java development, but that will be covered in a separate class. This course concerns itself with Windows platform development. Once you get really good at programming with CodeWarrior, you can explore the other platforms for which tools have been designed. Much of what will be discussed here can also be applied to developing Mac applications.
CodeWarrior automatically checks your code for obvious errors, helps you scan it for less obvious ones (via an integrated debugger and editor), and then compiles and links the code so that your computer can understand and execute your program. Every application that you have used has been through the coding, compiling, editing, linking, and debugging process, using tools like CodeWarrior. Now you can do it too! But we still have to teach you how.
Using CodeWarrior, you can write just about any type of program you can imagine. If you are a beginner, you may choose to write an application (also known as an executable) as your first program. You might choose to create something as basic as WordPad, Microsoft's text editor, for example.
Applications are probably the simplest type of program to write, though large commercial ones like Adobe Photoshop, Microsoft Word and even CodeWarrior itself are quite complex. Other program types include control panels, dynamic linked libraries (DLLs), and plug-ins. Let's talk about these types for a minute.
Under Windows, control panels are those (usually small) programs that are stored in your Control Panels directory and visible in the Control Panel section on the Start menu. The control panel itself is simply an interface to a system-wide feature, like setting the sound volume or the mouse's motion speed, for example.
DLLs supply useful functions that are shared by the operating system and applications. They typically don't have a user interface
Plug-ins are similar to DLLs in that they provide small pieces of code that are used by other software. However, a single program typically uses this code. For example, Photoshop plug-ins allow programmers to easily extend the capabilities of the Photoshop application without having access to its source code. Likewise, many Web browsers use plug-ins that add extra features to the application or allow you to manipulate new content types on the Web. Many programs, including CodeWarrior, make use of this plug-in architecture.
There are plenty of other types of programs you can write once you master CodeWarrior and the programming interfaces for your operating system. In fact, once you complete this course, you can take other courses in which you will learn more in-depth methods of programming your computer. You've chosen an excellent place to begin your journey, however. So sit back, relax, and absorb the lessons in this course, and you'll be well on your way to becoming a CodeWarrior!
An Introduction to the CodeWarrior Application
CodeWarrior is a complex application. If you really want to benefit from all of its features, you should take this time to get to know its various components and the capabilities that will be available to you. This first lesson will give you a basic overview of how the CodeWarrior IDE is set up. (That acronym gets thrown around a lot, and it's short for Integrated Development Environment. ) We'll take a look at menus, windows, and other aspects of the program. Follow along with the CodeWarrior IDE while you read through these lessons. Start by launching the IDE application. If you followed the defaults for the CodeWarrior software installation, there will be an icon that represents a shortcut to the program. Double-click on it to start the IDE. If the shortcut icon is absent (these things happen), you can typically find the program at C:\Program Files\Metrowerks\CodeWarrior\
Toolbar
When you launch the CodeWarrior IDE, you'll notice a toolbar attached to the menu at the top of your screen. This toolbar (Figure 1-1) contains buttons that are shortcuts to frequently used menu items. You'll learn how to customize this toolbar -- and even the entire IDE -- in a later lesson. For now, notice that this toolbar can be used to quickly access features within the IDE.
Figure 1-1: The CodeWarrior IDE toolbar.
File Menu
The menus in the CodeWarrior IDE are set up in a relatively standard manner. The File menu contains choices that manage the care and feeding of files. It has standard items that let you create, open, save, and print files.
Edit Menu
The Edit menu will also seem familiar if you use other Windows applications. You'll find commands that cut, copy, and paste text, as well as a few other options to help programmers more easily manage source code layout. There is also a Preferences item in the Edit menu that we'll discuss later in this lesson.
View Menu
The View menu (Figure 1-2) gives you options to manage which toolbars and other windows are displayed in the CodeWarrior environment. Some of these windows include various specialty windows, such as the Watchpoints window, Expressions window, Processes window, and Global Variables window.
Figure 1-2: The Toolbar submenu of the View Menu.
Search Menu
The Search menu's commands allow you to locate source code within a single file, or within the directories of your hard disk. You can also easily replace large blocks of text or search your source code for references to other source code. These tools can be very handy, even for small projects.
Project Menu
Tools in the Project menu allow you to manage CodeWarrior projects. A project includes all of the files that make up a program you are writing, including its headers, source code, and libraries. The Project window serves as a container for these files. We'll discuss this in more depth in Lesson 2.
Debug Menu
When your source code doesn't do exactly what you want it to (or your computer crashes when you run your program), you'll develop a close relationship with the items on the Debug menu. We'll discuss debugging in Lesson 5.
Window Menu
The Window menu gives you the basic options to manage cascading and tiling windows in the CodeWarrior environment.
Help Menu
Lastly, use the Help menu to get online help with just about any item in CodeWarrior. Pretty cool setup, huh?
Getting to Know IDE Preferences
The CodeWarrior IDE has numerous preferences that you can use to customize your work environment. You may only want to change the text color, or you might want to get more personal, changing lots of settings. When you select Preferences from the Edit menu, you are presented with the IDE Preferences dialog box (Figure 1-3). There, you'll find options that control the appearance and behavior of the CodeWarrior editor, debugger, and lots more. In this section, we'll take a look at these preferences.
Figure 1-3: The Preferences dialog box.
As each item is described below, follow along in your own copy of CodeWarrior, and check out the various options available. You can get additional help by clicking on the question mark, then the item you're interested in. You'll see a terse description of the selected item's purpose. More detailed information is also available from the Help menu.
The General Preferences
Build Settings: Choose whether to save open-source code files before performing a build and how much memory to use while building.
IDE Extras: Various settings that don't belong anywhere else. Here's where you can have CodeWarrior interoperate with a third-party text editor. The integration of editors with the IDE is often less than perfect, but this limitation may be offset by the ability to use an editor that you know and like.
Plug-In Settings: Used by plug-in developers to assist in the debugging of plug-ins.
Shielded Folders: Directories listed here are skipped (ignored) during project and/or find and compare operations. This can be useful if you have a large directory of "data" files inside your project hierarchy yet you do not want those files to slow down certain operations of CodeWarrior.
Source Trees: This lets you specify directories other than the standard ones CodeWarrior searches when it builds a program.
Editor Preferences
Editor Settings: Various settings to customize the way the Editor displays and manages text and windows.
Fonts and Tabs: Text size, font, tab, and other display settings for the Editor windows.
Text Colors: Like the browser display, you choose the color in which certain language elements (such as comments) appear in the Editor window.
Debugger Preferences
Display Settings: Various settings to customize the display of the debugger.
Windowing: How the debugger manages its windows (e.g., hiding any open editor windows).
Global Settings: Various settings that customize how the debugger works on a global level, such as whether to launch a program when you open a file that contains the program's debugging information.
Remote Connections: Allows debugging over TCP/IP. You can debug software on a computer on the other side of the Earth!
Java Settings/Java Debugging: Allows you to set specific java programming language options, not covered in this course.
RAD Tools
Layout Editor: Various settings to customize the use of rapid application development tools within CodeWarrior.
As you can see, there are numerous settings and options that can be set. Clicking the Save button will save any changes you've made. Clicking the Close box for the Preferences window will cause CodeWarrior to ignore any changes you've made. Go ahead and play with it a little before you proceed to the next lesson. Make sure the Default Text File Format (in the Editor Settings panel) remains set for DOS
第二课  显示和定制工程和目标文件(1)
Translated from CodeWarriorU.COM
本课将讲述 CodeWarrior 操作文件的方式以及介绍它的工程(Project)窗口。
什么是工程文件
为了使用 CodeWarrior 来创建一个应用程序,你必须创建许多文件来构成一个工程(Project)。该工程的设置和所有这些文件的指针都被存放在一个工程文件中。这些设置包括编译和链接设置、源文件、库文件以及它们之间用于产生最终程序的相互关系。你可以将这个工程文件看作该工程的大脑:它保存了所有文件相互依存的关系并知道如何将它们组合成为一个可用的应用程序。工程窗口依次显示了这些关于你的程序和该程序所包含的文件的信息,并允许你只需轻松点击就可以修改这个工程。图2-1显示了工程窗口的外观。
2-1: 显示工程信息的工程窗口(图示为空的情况)
大多数组成你的程序的文件都是原始的文本文件。这些文件包含了你在 CodeWarrior 的编辑器中键入的源代码。在某些情况下,你还将使用一些预编译的源码和头文件。在进行数学运算和 I/O 操作时,你还要使用到一些库文件,比如 Metrowerks 标准库(Metrowerks Standard LibraryMSL)
通常,你都是使用 C C++ 来编写源代码,但通过给 CodewWarrior 的集成开发环境加装相应的插件(plug-in),你也可以在 CodeWarrior 中使用其它语言来进行编程。别忘了,CodeWarrior 可是一个可扩展的集成开发环境。通过给它安装一些插件,你就可以扩展它的功能。比如说,加入有人开发了一种新的叫做 Z++ 的编程语言,你就可以为 CodeWarrior 创建一个相应的插件使之能够支持 Z++ 语言,而且你还可以拿这个插件卖好多钱,让所有使用 CodeWarrior 编程的人都使上 Z++ 语言。那该多好!:
现在你知道了 CodeWarrior 是如何使用文件的了。下面我们来详细地学习有关工程窗口的知识。
工程窗口
当你启动 CodeWarrior 并打开或创建一个工程时,工程窗口就会显示出来。该窗口用于管理整个工程的所有文件以及这个工程将要产生的目标(targets)文件。一个工程包含了足够的信息来编译一个或多个目标文件。所谓的目标(target)文件就是当你编译程序时,由 CodeWariior 创建出来的(通常是)一个应用程序或库文件。一些程序可以编译产生多个目标文件。比如,你可以编写一个程序来创建一个 DLL 和一个调用该 DLL 的主应用程序。在 CodeWarrior 的工程中,你可以定义一个目标来产生 DLL,定义另一个目标来产生应用程序。通过这种方式,你的工程只需编译一次就可产生所有需要的目标文件或片断。图 2-2 显示了Hello World程序的工程窗口。
2-2: Hello World 程序的工程窗口(其中显示了所有包含于该程序中或用于编译该程序的文件)
你可以看到,在这个工程窗口中确实包含了一些项目。在窗口的顶端有三个标签(tab)页:文件标签(如图2-2显示)、链接顺序标签页和目标文件标签页(后两个标签页将在后续课程中讲述)。在这几个标签页的上方,有一个用于选择要编译的目标文件的下拉菜单。在本例中,我们选择的是 Hello World 工程的调试版本。在下拉菜单的右边有几个图标,它们的存在允许你不必使用菜单命令就可以方便地编译、链接和运行这个工程。
注意: 在某些版本的 CodeWarrior 中,链接标签被称为段(segments)标签。有些人可能在某些 X86 的处理器上编程时使用的是分段(segmented)代码。但使用 Windows 版的 CodeWarrior 工具时就不是这样,这是产生的 X86 代码使用的是"扁平的"(flat),或者叫做非分段的(unsegmented) 内存空间。
顾名思义,文件标签页列出了在工程中所有可能用到的文件。你可以通过创建一些组(用文件夹图标表示)来分门别类地管理这些文件,并可以帮助你方便地了解哪些文件将被使用。在本例中,我们将 C 程序(.c为后缀名的文件)全都放到一个叫做 Source 的组中,同时将库文件放到另一个组中,以便整洁明了地管理这些文件。
Sources 组中有一个 main.c 文件。因为我们还没有编译这个工程,所以在每个文件对应的 Code Data 栏中显示数字的都是零。一旦我们编译了这个工程,这些数字就将显示出来,表示源码转换为机器代码后实际的代码量和数据量。可是,库文件 ANSICX86.LIB MWCRTL.LIB 后面显示的是 n/a 。这表示索虽然这些文件被显示在这里,但是它们并不是 Hello World 目标文件的一部分。这两个文件是用于不可调试(non-debugging)开发的,而本例不是这种情况。如果我们从菜单中修改本例的目标文件为 Release(发布) 版本时,这两个文件就用得上了。这时这些文件后面的 Code Data 栏目就会显示相应的数值。
注意 : Data Code 栏右端的 Target 栏目中的小也是用来表示该文件是否被当前编译生成的目标文件使用到。
再往右边去,是 Debug (用一个绿的小虫子表示),它是用于告诉你对应的文件在编译时是否要产生调试信息。我们将在第五课中详细论述这部分内容。最后,我们来看看在每一行最右端的弹出菜单,它是用于打开文件、更新源码、打开包含的头文件等等操作的快捷方式,具体是什么才作,要看它所代表的项目的类型而定。
现在让我们来编译这个 Hello World 工程并看看编译后的工程窗口的情况。我们从 Project 菜单中选择 Make 命令来编译该工程。这将更新所有需要编译的文件并产生相应的输出文件——在本例中是 Hello World 这个应用程序。
2-3: 编译工程后的工程窗口的显示情况
从图2-3中我们可以看到,工程窗口发生了一些变化。Code Data 栏都显示了当前工程中对应项的相应的数字。如果你打算看看编译前后存放这些文件的目录的话,你会发现编译后在该目录下产生了一些新的文件,如图2-4所示。
2-4: 编译后将在工程目录中产生一些新的文件
在本例中,编译工程后在工程目录中产生了一个新的文件。这就是 X86 目标文件输出——生成一个叫做 Hello World x86 的应用程序。其它一些一直存在于这个目录中的文件是: .mcp 文件工程文件本省,以及 .c 源文件包含了应用程序源码的文件。Hello World Data 目录中还包含了由 CodwWarrior 生成的各种各样的支持文件。现在你还不必去关心这些文件。如果你正在编译目标文件的调试版本,你会看到更多的文件被产生出来,这些文件中可能包含着符号(symbolic)调试信息。集成开发环境的调试器使用这些符号信息来记录在高层次源码中的操作轨迹。我们将在第五课中详细讲述这方面内容。
现在你已经了解了这些文件是如何结合在一起被编译生成一个目标文件的。让我们来运行这个目标文件并看看它的运行结果是什么样。你双击 Hello World x86 应用程序,就会有如图 2-5所示的结果显示出来。
2-5: Hello World 应用程序的运行结果
你来试试看
总有一天,你自己也能编写出象 Hello World 这样充满激情和意义深远的程序来。这是完全有可能的。只要你有决心努力工作,学完本课你就可以编写这个程序了。下面我们给出该程序的源代码:
#include
void main(void) {
  int c;
  printf("Hello World on Windows!\n");
  c = getchar();
}
就这么多!你没想到会这么简单吧?你可以试试看将 "Hello World on Windows!" 这句话换成一句别的什么内容。要不了多久,你就能自己编写一些不仅能显示文本而且还能执行你指定的其它任务的应用程序了。但在此之前,你还需要学习第四课,有关链接的知识。
关于getchar()函数
那些有经验的 C 程序员可能会问,上述代码中使用的 字符输入函数 getchar() 用在这里是什么意思。通常地,当一个应用程序执行完毕后,它就会终止,然后操作系统就会立即清除该应用程序输出。这样,当上述应用程序显示完Hello World on Windows!后,为了避免系统立即将该程序地输出清除,我们在程序中添加了一个 getchar() 函数,使程序在此处暂停,直到有键盘输入时整个程序会终止。这样我们才有足够的时间来看清楚该程序的输出。
这是一个正常的操作,而不是一个 bug。实际上,CodeWarrior 也提供了一个适用于此处的库函数,这个库函数中的 C 控制台 I/O 函数步不仅可以在程序显示完毕后锁定应用程序,而且还可以让你将输出的文本信息存储到一个文件中去。
第一部分    第二部分
附原文:
Displaying and Customizing Projects and Targets
In this section of the lesson, I'll introduce you to the way CodeWarrior handles files and give you a tour of the Project window.
Understanding the Project File
To create programs in CodeWarrior, you will use a number of files that work together to form a project. A project's settings and pointers to other files are stored within a project file. These settings include compiler and linker options, source files, libraries, and information about how they interact with one another to build the final product -- your program. For now, just think of the project file as the brain of your project: it keeps track of everything that is going on and knows how to combine files and settings into a working application program. The Project window in turn displays this information about your program and its files and allows you to modify the project by just pointing and clicking. Figure 2-1 shows the Project window.
Figure 2-1: Each project you create appears in the Project window. This one is empty.
Most of the files that make up your program are ordinary text files. These files contain your source code. You'll create them by typing the source code into the CodeWarrior editor. In some cases, you will also use prebuilt source code and header files. There are also library files, such as the Metrowerks Standard Library (MSL) which provides valuable functions for math calculations and I/O operations.
Your source code will usually be written in C or C++, but can also be in any other language that is supported by a CodeWarrior IDE plug-in. Recall that CodeWarrior is an extensible IDE. In other words, by inserting (or writing) a plug-in, you can extend the capabilities of the CodeWarrior IDE. For example, if someone develops a new programming language, say Z++, you could create a plug-in to support that language and sell it for a zillion bucks, and then anyone using the CodeWarrior IDE could program in Z++. Neat, huh?
Now that you understand how CodeWarrior uses files, let's take a closer look at the Project window.
The Project Window
When you launch CodeWarrior and open or create a project, a Project window greets you. To recap, it allows you to manage the project, all of its files, and the targets that it builds. A project contains all of the information necessary to build one or more targets. A target is the file that CodeWarrior creates when you build your program -- usually an application or library. Some projects build multiple targets. For example, you may write a program that has a DLL, as well as the main application that calls it. In the CodeWarrior project, you can define one target to generate the DLL, and another target to generate the application. In this way, your project can build all the necessary targets, or pieces, in one fell swoop. Figure 2-2 shows the Project window for a program called Hello World.
igure 2-2: The Project window for a program called Hello World lists all of the files that are included in the program or that are needed to build it.
You can see that the Project window contains quite a few items. At the top are three tabs: the Files tab (displayed in Figure 2-2), the Link Order tab, and the Targets tab (both discussed later in this lesson). Above the tabs is a pull-down menu that allows you to choose which target to build. In this case, we have chosen the Debug version of the Hello World project. The icons to the right of this pull-down menu allow us to easily compile, link, and run the project without having to use menu commands.
Note: The Link Order tab is called the Segments tab in some versions of CodeWarrior. Some of you might have programmed certain x86 processors using segmented code. This is not the case with the Windows CodeWarrior tools. They generate x86 code that uses a "flat," or unsegmented, memory space.
As its name implies, the Files tab lists all of the files that could possibly be used in the project. You can create groups (represented by the folder icon) in the window to help manage the files hierarchically and make it easier to keep track of which files are being used. In our case, we place our C language source code files (indicated by the .c extension) in a group called Sources. We've organized libraries into other groups to keep things tidy.
The Sources group for our example holds the main.c file. Since we have yet to build this project, the numbers in the Code and Data columns are all set to zero. When we build the project for the first time, these numbers will reflect the actual amount of code and data the compiler creates when it translates the source code into machine language. However, the library files ANSICX86.LIB and MWCRTL.LIB have n/a next to them. This indicates that even though these files are listed, they are not used as part of the Hello World target. That's because these two libraries are used for non-debugging development, which is not applicable for this target. If we were to switch targets by picking the Release version of Hello World from the menu, these columns would change to reflect the use of these other libraries.
Note : The small bullet in the Target column to the right of the Data column also indicates whether or not the file is being used by the current target.
Other columns in the main window include the Debug column, to the right (see the little green bug?), which tells you whether this file is currently set to generate debug information when it is compiled. We will discuss this in more detail in Lesson 5. And lastly, the pop-up menu at the far right of each row acts as a shortcut that allows you to quickly open files and/or update sources for compilation, open included header files, etc., depending on the type of item it represents.
Let's build the Hello World project and see what the window looks like after it is built. To build this project, select Make from the Project menu. This will update all files that need to be built and will produce the output file -- in this case, the Hello World application.
Figure 2-3: When you build a project, the Code and Data items for each column are filled in.
Notice in Figure 2-3 that the window has changed a bit. The Code and Data columns now contain numbers for the items that are in the current project. If you were to look at a before and after screenshot of the folder on your hard disk where these items are stored, you would see that some new files have been generated. Let's take a quick look at those items in Figure 2-4.
Figure 2-4: Building a project generates new files within your project folder.
Notice in our example that there is a new file in the project directory. This is the x86 target's output -- the application called Hello World x86. Other files in this directory that exist all of the time are the .mcp file -- the project file itself -- and the .c source file, which contains the source code for the application. The Hello World Data directory contains various other support files generated by CodeWarrior. You need not be concerned with these items at this time. If you are building a debug version of the target, you'll see even more files, which can contain symbolic debugging information. The IDE's debugger uses the symbolic information to help it present tracking operations in high-level source code. These details are discussed later in Lesson 5.
Now that you understand how the files work together to build a target, let's run the target and see what it looks like. When you double-click the Hello World x86 application, the following (Figure 2-5) appears on the screen.
Figure 2-5: When you run the Hello World program, a window containing the words "Hello World" appears on screen.
Putting It into Play
Someday, you too may create something as lyrical and profound as Hello World. Possibly, with hard work and determination, you can do this by the end of the day. The source code to make this work is as follows:
#include
void main(void) { int c; printf("Hello World on Windows!\n"); c = getchar();
}
That's it! Who knew it could be so easy? Go on, you know you want to try it, but with some sort of profound statement instead of "Hello World on Windows!" Soon you'll be writing your own applications that not only can display text but can also perform other tasks for you. Before you know it, you'll be customizing AIBO dogs! That's Lesson 4. Well, okay, not really. Lesson 4 is Linking.
What's That Other Function Doing There?
Those already seasoned in the C language might be wondering what the character input  function, getchar(), is doing here. Typically, when an application completes its job, it terminates, and the OS cleans up after the program. This means the Hello World window disappears moments after the application finishes drawing the phrase "Hello World on Windows!" So that we can admire our handiwork, we put the brakes on the program by having it wait for a final keystroke before it terminates.
This is not a bug, but normal behavior. Honest. CodeWarrior does provide an alternate library that provides the C console I/O functions used here. This alternate library will not only halt the application after it finishes writing to a window but will also let you save the text output to a file.
Link Order
When you build a program using multiple source files, the link order is very important. After compiling your source code, you must link the items in the proper order. You will use the Link Order tab to determine this order. In the case of the Hello World x86 target we've been working with (Figure 2-6), note that the main.c source file is first on the list, with the libraries used by the program following. To reach the Link Order tab, open the project and click the tab. To change the order by which files are linked, you simply drag items up and down the list within this window. We will discuss Linking in more detail in Lesson 4.
Figure 2-6: Back in the Project window, clicking the Link Order tab displays a list of component files included in the current project.
Targets
Figure 2-7: The Hello World project's Target tab.
After you've chosen the files and defined their link order, you must configure the target. The target is essentially the output file -- in our case, the Hello World application. There are also a lot of settings that must be set behind the scenes to make it possible for you to hit the target. If you double-click the Hello World target in the Targets Tab (Figure 2-7), you should see the dialog box shown in Figure 2-8.
Figure 2-8: Set targeting preferences in the Settings window.
Configuring Target Settings
The preferences you learned about in Lesson 1 are global settings used throughout the CodeWarrior IDE. Now, let's take a look at options for configuring target options. I will briefly examine the most important of these options. Follow along in your copy of CodeWarrior by double-clicking an item under the Target tab to bring up the Settings panel.
If you want a little extra guidance, move your cursor over an item in the panel, right-click it, and read the pop-up help text that appears.
Note: Some of the items listed below are specific to the target we are currently viewing, in this case the Hello World target. If you're using AMD's K6 or Athlon processors, some of these panels may contain items that use features specific to that chip. Consult the documentation for the compiler/linker that you are using to ensure that you are making the best use of your development environment.
Target Preferences
Target preferences include the following items, each with its own panel:
Target Settings: Choose the target's name (the name that appears in the target pop-up menu), which linker(s) to use, and the output directory here.
Access Paths: Access paths tell CodeWarrior where to search for required files that are a part of the project but not necessarily project-specific (such as header files). CodeWarrior will not, by default, search your entire hard disk for files. It will only look where you explicitly tell it to look with access paths.
Build Extras: A few miscellaneous settings to improve the build speed.
Runtime Settings: In order to debug non-application code, such as a plug-in, you must have an application assist you. The application does this by calling the suspect plug-in code. This panel lets you specify the host application to use in this situation. This topic will be mentioned in Lesson 8.
File Mappings: Every file must be mapped so as to identify it to the compiler. That is, this panel tells CodeWarrior that files ending in .cpp are C++ source files and should therefore be compiled using the C++ compiler. See Figure 2-9 for more examples.
Figure 2-9: The File Mappings options allow you to specify the treatment of files, based on their extension.
Source Trees: Source trees are similar to access paths. Here you can enter file locations, such as file servers, that are specific to your project.
x86 Target: This panel allows you to set the type of project (application, library, or DLL), and various aspects of the project -- including how much memory the resulting program requires. It is also where you specify the name of the file output by the IDE. This name can -- and usually will -- be different from the target's name (the one entered into the Target Settings panel).
Setting Other Preferences
CodeWarrior includes a variety of additional preferences that can help you customize your environment before beginning to work on your own programs.
Language Settings
Language Settings include the following items: (discussed further in Lesson 3)
C/C++ Language: There are an extremely large number of settings for the C/C++ compiler. Suffice it to say that you can alter them here. See Figure 2-11.
Figure 2-10: C++ Language settings.
C/C+ Warnings: These settings (Figure 2-10) tell the C/C++ compiler whether to provide warnings as your code is being compiled. Sometimes source code can contain elements that aren't quite correct but are not necessarily errors. These are called warnings. You can instruct the compiler to treat all warnings as errors so that you can quickly examine the questionable code in the editor.
Windows RC: A Windows-specific resource compiler. Resources are graphic elements (such as windows, buttons, and menu items) that can be built independent of your program code. This panel lets you specify a header file that the resource compiler uses when generating these interface elements.
Code Generation
Code Generation preferences include the following items:
x86 Processor: There are a number of decisions your compiler must make as it generates the machine code from your C or C++ source code. The settings in this panel help the compiler to optimize your code. This item will be labeled differently on each CodeWarrior platform. Figure 2-11 shows the x86 version. This is discussed more in Lesson 3.
Figure 2-11: x86 Processor settings are specific to projects intended for a x86-based computer. CodeWarrior includes settings for each operating system it supports (directly, or through plug-ins).
Global Optimizations: This has got to be one of the most exciting and useful panels in all of CodeWarrior (see Figure 2-12). This panel allows you to set the global optimization level of the compiler. This means that you can control how efficient and fast the compiler tries to make your source code. However, speed comes at a price. The smaller and faster the source code, the more difficult it is to debug, and the longer it takes to compile. Fortunately, the panel spells it all out for you so you can make that decision every time you optimize! If you want to, I mean. You will have a great deal of fun with this panel as you adjust the settings to suit your needs and your projects.
Figure 2-12: Use Global Optimization settings to speed up compiling of the code (Optimizations Off), or make your code execute faster (Levels 1 through 4).
Linker Preferences
Linker preferences include the following items:
x86 Disassembler: The name and specific settings in this panel may differ a bit by platform, but the options are similar. They allow you to configure specific settings that the Linker must use when disassembling and debugging your target.
x86 Linker: The name and specific settings in this panel may differ a bit by platform, but the settings are similar (Figure 2-14). They allow you to configure specific settings that the Linker must use when building your target, such as specifying the entry point into your code. See Lesson 4.
Figure 2-13: Each supported operating has a Linker settings panel. Here is the x86 version.
Settings for the editor can be found on the Custom Keywords panel. This panel allows you to configure the text editor to display various custom keywords in different colors. For example, if you like to use a keyword in your source code to label certain areas of the code in a different color, you can list it here. It will appear in the color you choose.
The Debugger Settings panel allows you to choose how the debugger should log data, handle watch points, etc. This is discussed in more detail in Lesson 5.
Now you can begin to see how CodeWarrior's features help make programming easier. Now go check out your homework. Yes, you will have some homework assignments in this class. Then I'll see you back here for Lesson 3.
CodeWarrior 使用教程
第三课          
Translated from CodeWarriorU.COM
第一步:理解编译阶段
我们的前面的课程中已经学到,源程序输入完之后的工作就是要编译它。编译源程序就是计算机把你所写(通常是用C C++编写的)的源代码进行分解、分析,然后转化为机器语言。机器语言是一种计算机能够理解阿语言,而且它运行起来比 C C++ 也要快得多。编译的大致过程如下:
1. C C++ 编写代码:
#include
void main(void) {
  int c;
  printf("Hello World on Windows!\n");
  c = getchar();
}
2. 进行编译。 CodeWarrior 编译上述代码并把它翻译成机器语言,结果如下:
符号名:
1: _main 2: _@8 3: _printf 4: ___files 5: _fwide 6: ___get_char =============================================================== SECTION SIZE = 0x0000003C; NAME =?.text DEFINED SYMBOLS: name = _main offset = 0x00000000; type = 0x0020; class = 0x0002 00000000: 68 00 00 00 00 push offset _@8 00000005: E8 00 00 00 00 call _printf 0000000A: 59 pop ecx 0000000B: 6A FF push -1 0000000D: 68 00 00 00 00 push offset ___files 00000012: E8 00 00 00 00 call _fwide 00000017: 85 C0 test eax,eax 00000019: 59 pop ecx 0000001A: 59 pop ecx 0000001B: 7D 1E jge $+32 ; --> 0x003b 0000001D: 83 2D 2C 00 00 00 sub dword ptr ___files+44,1 00000023: 01 00000024: 72 0A jb $+12 ; --> 0x0030 00000026: FF 05 28 00 00 00 inc dword ptr ___files+40 0000002C: EB 0D jmp $+15 ; --> 0x003b 0000002E: 89 C0 mov eax,eax 00000030: 68 00 00 00 00 push offset ___files 00000035: E8 00 00 00 00 call ___get_char 0000003A: 59 pop ecx 0000003B: C3 ret near ============================================================= SECTION SIZE = 0x00000019; NAME = .data 00000000: 48 65 6C 6C 6F 20 57 6F 72 6C 64 20 6F 6E 20 57 Hello World on W 00000010: 69 6E 64 6F 77 73 21 0A 00 indows! DEFINED SYMBOLS: name = _@8 offset = 0x00000000; type = 0x0000; class = 0x0003 ==============================================================
上述机器代码难于阅读,不要去管它。机器代码相比 C C++ 而言,要难理解多了。但是,计算机只能理解机器语言。只有将你的程序编译或翻译成机器代码,然后再执行它,这样运行起来才能快一些,而不是每次运行时才去翻译它,那样运行速度就很慢了。
你只需选定一个源文件,然后从工程菜单中选择反汇编项,你就能看到该文件的机器语言清单。实际上上面我们看到的机器语言清单就是这样得到的。如果你仔细地对照阅读一下你的 C C++ 源代码和它编译后的机器代码,不难发现它们之间的关系。
CodeWarrior 中编译选项的详细设置
在正式开始编译源代码之前,CodeWarrior 还要对其做预处理。在这个阶段,是对 C C++ 代码进行编译前的一些准备工作。在编写程序的过程中,往往会有很多相同的P代码输入,于是程序员使用一些快捷方式,比如所谓的宏(macros)来代替这些相同的输入。例如,你可以使用 APPNAME 作为一个宏,来表示Metrowerks CodeWarrior,以此来减少输入的工作量。预处理就是要把这些宏转换为它们实际表示的代码,此外还要替换一些定义符号(比如 #define THREE 3)为实际的源代码。为了更好地理解预处理所做的工作,你可以查看一下预处理结果的清单。首先在工程窗口中选中一个源文件,然后从工程菜单中选择预处理项,你就可以看到源代码进行了预处理之后,编译之前的结果清单了。.
定制 CodeWarrior 的编译方式
在第二课中,我们已经了解了一些控制 CodeWarrior 编译代码的选项对话框。现在我们再来详细地看看一些标准 C C++ 编译器的设置。请你按照下面所学内容在你的 CodeWarrior 上进行实际的操作练习。
3-1: 控制 CodeWarrior 进行  C/C++ 编译的语言设置
从编辑菜单中选择 Hello World X86 setting 项来打开设置窗口。现在,点击图3-1中语言设置类(Language Settings category)下面的 C/C++ 语言标签(C/C++ Language label),你就可以看到C/C++ 语言设置对话框中的许多选项。我们逐个来学习这些选项,以便了解它们是如何影响编译的过程的。
激活 C++ 编译器(Activate C++ Compiler): 这个选项允许你把所有后缀名为 .c 的源文件当作 C++ 文件来进行编译。如果你想在 C 源码中使用一些 C++ 语言的特时,这个功能就很有用了;
ARM 一致性(ARM Conformance): 编译器要求你的代码遵循 ANSI C++ 的标准。但你可以通过选中这个选项来指定你的编译器遵循注释 C++ 参考手册(Annotated C++ Reference ManualARM) 中的标准;
允许 C++ 例外(Enable C++ Exceptions): 选中这个选项将允许你在 C++ 代码中使用 try/catch/throw 等块(blocks)。这些方法用于书写错误管理器(writing error handlers)
允许 RTTI(Enable RTTI): RTTI 表示 Run Time Type Information(运行类型信息)。这个选项运息编译器判定一个 C++ 对象在运行中的类型。这是 C++ 的高级特,在很多情况下都是很有用的。如果你想了解更多关于 RTTI 的信息,请查阅你的 C++ 手册;
内联深度/自动内联/延期内联(Inline Depth/Auto-Inline/Deferred Inlining): 这些是关于源代码中使用的内联函数的一些设置项。所谓内联函数,就是在编译时该函数的源代码将被直接插入到程序体中,而不是产生这个函数的调用。在某些情况下,使用内联函数可以提高代码的性能。这是编译器的一个高级设置项;
字符串池(Pool Strings): 通常,编译器将编译后代码中的所有字符串对象存储到它们自己的数据空间中。如果你想将所有的这些字符串存放到一个数据空间中,就应该选中此项。如果你的源代码中有非常多的字符串(比如上面我们提到的 APPNAME ),那么你就应该选中此项来节省内存空间。这个功能只能在使用 PowerPC Mac OS 平台上编程时使用;
不重用字符串(Don't Reuse Strings): 所谓重用字符串,就是指当你的程序中有几个完全一样的字符串时,编译器会将它们全都存放到同一个数据空间里。但是,有时你可能想修改某个字符串,这就会造成其它和要修改字符串共享数据空间的字符串也要被修改了。如果你想避免这种情况,就要选中这个选项。这样,即使程序中有完全一样的字符串,它们也将存放到不同的数据空间中;
要求有函数原型(Require Function Prototypes): 建议最好选中此项。使用函数原型可以帮助编译器在检查传递给函数的参数类型时发现代码中的错误。所谓函数原型就是在程序的前端对函数进行声明。也就是说,你应当在使用一个函数之前,定义或声明这个函数。既然这个选项这么好,那么什么时候应该关掉它呢?通常是,当你使用一些老版本的 C 进行编程时,并不需要对函数进行事先声明,这时你就应该关掉这个选项来检查整个程序代码。你一定愿意在程序前端书写函数原型,并选中此选项,因为它能帮助你解决这么多的编码错误;
允许支持布尔变量(Enable bool Support): 为了使用 C++ 的布尔变量—— true () false () 这两个关键字,必须选中此项;
允许支持wchar_t(Enable wchar_t Support): 为了使用 C++ 的内置类型 wchar_t 而不是 char 类型来表示字符类型,必须选中此项;
严格遵循ANSI/只能使用 ANSI 关键字(ANSI Strict/ANSI Keywords Only): 默认情况下,编译器允许你使用 Metrowerks 扩展和 C/C++ 语言的附加关键字。但是如果你想在这种情况时编译报错,那么就应该选中这两项。这样,编译出来的程序就是 100% ANSI 兼容代码;
扩展通配符(Expand Trigraphs): 默认情况下,通配符是不允许的。为了能够使用通配符,就要选中此项。所谓通配符,就是在你的源代码中代表字符常量的方式。例如, '????' 就是一个通配符;
多字节敏感(Multi-Byte Aware): 如果你是有能够的编程语言要求使用多字节字符(例如 Kanji Unicode),就要选中此项。这样编译器才能正确地处理源代码中的多字节字符;
指示到 SOM (Direct to SOM): 这是 Macintosh 平台上才有的功能,它允许你在 CodeWarrior 中直接创建 SOM 代码。SOM 是一种使用于苹果机上的开放文档环境的代码类型,但现在已没人用了;
使用 CR 为换行符(Map Newlines to CR): 这个选项允许你交换 '\n' '\r' (这是用于标识源码行结束的符号)。此选项只对 Mac OS 上的编程有用;
不严格的指针类型规则(Relaxed Pointer Type Rules): 选中此项将把 char *, unsigned char *, void * Ptr 当作是同一种类新。当你从另一个并没有正确管理指针类型的源代码中,或者是从一个使用还不能正确支持这些类型的老编译平台上开发的源代码中继承代码时,这个选项就很有用了;
枚举类型总是整型(Enums Always Ints): 通常情况下,编译器将一个枚举类型分配与之最接近的类型同样的空间。如果你想使枚举类型的空间总是和整型一样,那么就要选中此项。所谓枚举类型就像这样:enum {itemone, itemtwo = 7, itemthree}。其中,itemone 等于 0itemtwo 等于 7itemthree 等于 8
使用无符号字符类型(Use Unsigned Chars): 选中此项将把所有字符数据类型当作无符号字符类型来处理;
EC++ 兼容模式(EC++ Compatibility Mode): 使用 CodeWarrior 编译嵌入式 C++ (EC++) 代码时,要选中此项。请注意,此时 C++ 中的诸如模板(templates)、例外(exceptions)和其它一些 C++ 的高级功能就不可用了。具体情况请查阅 C++ 手册;
允许 Objective C(Enable Objective C): 为了使用 Objective C ( NeXT 计算机操作系统上很著名的编程语言),要选中此项。此选项只能在 Mac OS 下只用;
前缀文件(Prefix File): 如果需要在每个源文件中包括一个头或预编译头文件,就要将该文件名输入在此处。适用情况:当所有源文件都要访问一个特殊的定义,但你又不想在每个源文件中键入 #include 来包括该定义时,使用此选项很方便。
注意: 上述许多选项在 Mac OS Windows 平台上的 CodeWarrior 编译器版本中都是一样的。但根据我们在上面对这些编译器选项的描述可知,一些选项在两个平台上还是有一些不同的。然而,C C++ 时平台无关的编程语言,因此大多数概念都是可以应用于任何平台的。
附原文:
Step One: Understanding the Compile Phase
As I've discussed in previous lessons, compiling your source code is the next step after you type it in. When you compile your source code, your computer parses and analyzes the code that you've written (usually in C or C++) and converts it to machine language. Machine language is a programming language that your computer can understand and act upon much more quickly than C or C++. The life cycle for you and your code looks like this:
1. Write the code in C or C++:
#include
void main(void) { int c; printf("Hello World on Windows!\n"); c = getchar();
}
2. Choose Compile. CodeWarrior then compiles the code and translates it into machine language:
SYMBOL NAMES:
1: _main 2: _@8 3: _printf 4: ___files 5: _fwide 6: ___get_char
=============================================================== SECTION SIZE = 0x0000003C;
NAME =?.text DEFINED SYMBOLS: name = _main offset = 0x00000000; type = 0x0020; class =
0x0002 00000000: 68 00 00 00 00 push offset _@8 00000005: E8 00 00 00 00 call _printf
0000000A: 59 pop ecx 0000000B: 6A FF push -1 0000000D: 68 00 00 00 00 push offset ___files
00000012: E8 00 00 00 00 call _fwide 00000017: 85 C0 test eax,eax 00000019: 59 pop ecx
0000001A: 59 pop ecx 0000001B: 7D 1E jge $+32 ; --> 0x003b 0000001D: 83 2D 2C 00 00 00
sub dword ptr ___files+44,1 00000023: 01 00000024: 72 0A jb $+12 ; --> 0x0030 00000026:
FF 05 28 00 00 00 inc dword ptr ___files+40 0000002C: EB 0D jmp $+15 ; --> 0x003b
0000002E: 89 C0 mov eax,eax 00000030: 68 00 00 00 00 push offset ___files 00000035: E8 00
00 00 00 call ___get_char 0000003A: 59 pop ecx 0000003B: C3 ret near
============================================================= SECTION SIZE = 0x00000019;
NAME = .data 00000000: 48 65 6C 6C 6F 20 57 6F 72 6C 64 20 6F 6E 20 57 Hello World on W
00000010: 69 6E 64 6F 77 73 21 0A 00 indows! DEFINED SYMBOLS: name = _@8 offset =
0x00000000; type = 0x0000; class = 0x0003
==============================================================
That's hard to read, let alone type correctly. Machine code is a bit more difficult for a human to read than C or C++ code. However, it's just what the computer needs, and your application will operate much faster if you compile -- or translate -- it into machine language at the outset than if it has to make that translation every time you ran it.
You can view the disassembly (machine code listing), as we have done above, for any source file by simply clicking on it in the Project window and selecting Disassemble from the Project menu. In fact, the assembly code listing shown above was produced this way. By looking at your C or C++ source code and the disassembled code side by side, you will begin to see the relationships between the two.
A Detailed Look at Compiling Options in CodeWarrior
Before your code is actually compiled, CodeWarrior preprocesses it. This preprocessing phase prepares the C or C++ code to be compiled. Programmers have plenty of typing ahead of them, so they sometimes use shortcuts, known as macros. These macros allow you to type things like APPNAME when you really mean "Metrowerks CodeWarrior" and save keystrokes as you type your source code. Preprocessing converts the macro text you've typed into the code it represents. The preprocessor also substitutes defined symbols (such as #define THREE 3) in the source code. To get a better feel for what the preprocessor accomplishes, you can examine a listing of its output. To do this, pick a source file in the Project window and select Preprocess from the Project menu. The output file that appears shows the preprocessed source code as the compiler will actually see it.
Customizing the Way CodeWarrior Compiles
In Lesson 2, I showed you a few of the dialog boxes that control the way CodeWarrior compiles code. Let's take a closer look at some standard C and C++ compiler settings. Please follow along in your copy of CodeWarrior.
Figure 3-1: The C/C++ Language Settings control the way CodeWarrior deals with C and C++ code when compiling.
Open the Settings window by selecting Project Hello World x86 Settings from the Edit menu. Now, click the C/C++ Language label under the Language Settings category (Figure 3-1). You will see that there are numerous options in the C/C++ Language Settings Dialog Box. Let's take a look at some of these items one by one and see how they affect the compilation process.
Activate C++ Compiler: This option allows you to compile all .c source files as if they were .cpp files. This can be helpful if you want to use some of the features of the C++ programming language in your C source code.
ARM Conformance: The compiler expects your code to follow the ANSI C++ standard. You can direct the compiler to instead follow the standard specified in the Annotated C++ Reference Manual (ARM) by enabling this feature.
Enable C++ Exceptions: Enabling this feature allows you to use try/catch/throw blocks in your C++ code. These methods help with writing error handlers.
Enable RTTI: RTTI stands for Run Time Type Information. This allows the compiler to figure out the type of a C++ object at run-time. This is an advanced feature of C++ that can be helpful in various situations. See your C++ manual for more information on RTTI.
Inline Depth/Auto-Inline/Deferred Inlining: These options refer to the inlining of functions in your source code. That is, rather than generate the function call, the compiler inserts the function's code directly into the program body. In certain situations, this can improve code performance. This is an advanced compiler feature.
Pool Strings: Normally, the compiler will store all of your string objects in their own data space within your compiled code. If you would like all strings to be stored in just one data space, you can enable this feature. If you have a large number of strings in your source code (such as the APPNAME macro we discussed earlier), you should enable this feature to save memory. Be aware that although this checkbox is present in the Windows version of CodeWarrior, the feature only works for Mac OS PowerPC program code.
Don't Reuse Strings: If you have multiple strings that are the same throughout your program, the compiler will store them all in the same data space -- that is, it will reuse them. However, there are times when you may alter the string literal in place, in which case other strings that share its data space would also be altered. If you do not want this to happen, enable this feature, and all strings, even if they are identical, will be stored in separate data spaces.
Require Function Prototypes: It's a good idea to keep this feature enabled. Function prototypes allow you to more easily find errors in your source code by assisting the compiler in verifying data types that you pass to your functions. A function prototype is also known as a forward declaration. That is, you define (declare) the function before (forward) you use it. If this feature is so good, when might you turn it off? Typically, when you're working with old C programs that never made forward declarations of their function calls. You'd disable this option only to check the integrity of the program code. However, you'd want to write function prototypes for the program and reactivate this feature quickly, because it solves so many coding problems.
Enable bool Support: To use the C++ bool, true and false keywords, enable this feature.
Enable wchar_t Support: To use the C++ wchar_t built-in type for characters rather than char, enable this feature.
ANSI Strict/ANSI Keywords Only: By default, the compiler allows you to use Metrowerks extensions and additional keywords in the C and C++ languages. If you would like an error to be generated when you try to do this, enable the ANSI Strict and/or ANSI Keywords Only feature. This will ensure that you only compile 100 percent ANSI-compatible code.
Expand Trigraphs: By default, trigraphs are ignored. To expand them, you can enable this feature. Trigraphs have to do with the way character constants are represented in your source code. For example, '????' is a trigraph.
Multi-Byte Aware : If you are programming in a language that requires the use of multi-byte characters (like Kanji or Unicode), you will want to enable this feature. This enables the compiler to properly handle multi-byte characters in the source code.
Direct to SOM: This is a Macintosh-only feature that allows you to create SOM code directly in CodeWarrior. SOM is a type of code used in the now defunct OpenDoc environment from Apple Computer.
Map Newlines to CR: This feature allows you to swap '\n' and '\r' (the values for line feed and carriage return, which mark the end of a source code line). This is feature is only useful for Mac OS programs.
Relaxed Pointer Type Rules: Enabling this feature will treat char *, unsigned char *, void * and Ptr as the same type. This can be helpful when you inherit code from another source in which the programmer did not properly manage pointer types, and/or the developer used an old compiler that did not handle these types properly.
Enums Always Ints: Normally, the compiler will make an enumerated type the size of the closest type. If you would like types to always be the size of an int, you can enable this feature. An enumerated type is something like this: enum {itemone, itemtwo = 7, itemthree}. In this case, itemone would be equal to 0, itemtwo would be equal to 7, and itemthree would be equal to 8.
Use Unsigned Chars: Enabling this feature will cause all char data types to be treated as if they were unsigned char.
EC++ Compatibility Mode: Enable this feature to compile embedded C++ (EC++) code. Note that certain C++ goodies such as templates, exceptions, and other advanced C++ features aren't available in EC++. See your C++ manual for information.
Enable Objective C: To enable Objective C (made famous in the NeXT computer operating system) you can check this checkbox. This is another Mac OS-only language feature.
Prefix File: To include a header or precompiled header in every source file, type the name here. This can be useful if you have specific definitions that you want all source files to have access to, but do not want to type the #include line in the source files themselves.
Note: Many of these features are identical on both the Mac OS and Windows CodeWarrior compilers. As pointed out in the descriptions of the compiler settings above, there are some differences between the two. However, C and C++ are platform-independent languages, so most of these concepts apply on any platform.
CodeWarrior 使用教程
第四课     
Translated from CodeWarriorU.COM
第二步:连接程序各部件
当你的程序编译完毕以后,接着就要使用连接器来把这些组成你的程序的各个文件连接起来。
连接器是用来组合各个源文件被编译后得到编译代码的,同时还要将这些文件与它们所需要的库连接在一起。连接确保了每片代码都知道如何去到你程序中的其它元素。例如,连接器修改机器代码以便使用 MSL 库函数中的 printf() (该函数用来实现功能强大的控制台输入/输出)来替换你在源文件中使用的 printf() 函数调用。
当你编写一个程序时,常常将代码写成函数并存放在不同的文件中,然后将这些文件编译得到单个的输出文件。就像我们在前面使用的 Hello World 这个程序,它的输出文件是一个应用程序。但是在某些时候,同一个程序中使用的函数在输出时不一定是存放在同一个输出文件中的。例如,假设你编写一个应用程序时,其中包含了许多被程序的多个部分重复使用的代码,你就可以将这些被重复使用的代码编译成为一个可共享的动态链接库(DLL)。这样,该应用程序仍然可以访问到这些代码,但是它就需要弄明白应该到哪个文件以及如何到这些它需要使用的函数。这种情况,就需要做一些连接的工作了。
连接的类型
在程序的编制中,一个连接就是一个指针,指向一个象编译函数这样的对象,或者指向一个变量或一个数组这样的数据内容。连接有两种方式:硬连接(hard linking)和软连接(soft linking)
硬连接发生于连接过程中,说明代码的时候。换句话说,如果一个函数被使用了,在连接时连接器确切地知道这个函数在哪里并创建一个连接把这个函数连接起来。
软连接(也称为弱连接)发生于进行连接时,一些代码可以不需要进行说明,因为你知道这些代码位于一个 DLL 中,并在运行时可以从该 DLL 中进行调用。在这种情况下,连接器会告诉应用程序到哪里去它所需要的代码。
软连接也是计算机操作系统中许多元素使用的方法。接下来,我们一起来学习一些关于连接器的选项设置。
A 连接Detailed Look at Linking
为了将来能更好地领略 CodeWarrior for Windows 平台的卓越功能,你首先需要了解如何配置 CodeWarrior 的连接器来满足你的要求。
根据你的要求来配置连接器
本课中使用的示例是运行在 x86 平台上的  CodeWarrior 的连接器设置面板(如图4-1)。但是在这其中使用的很多术语也是适用于其它平台上的 CodeWarrior 连接器的。
4-1  x86 连接器设置窗口
通过选择编辑菜单中的工程名项来打开这个设置窗口。(“工程名是指当前的活动工程——在本例中就是 Hello World x86),然后点击设置窗口中的 x86 连接器(或类似的名字)标签。
下面我们来看看一些连接器设置项:
入口点用法(Entry Point Usage): Windows OS 体系中,每一个代码模块都有许多可能可以或者不可以选择的入口点,这取决于你所编译的代码类型。这些入口点的自然属性依赖于你正在编写的程序的输出文件(应用程序、库或 DLL )的类型和其它的一些因素。当一个应用程序需要调用一个 DLL 函数时,主机操作系统就使用这个入口点信息来正确地调用该 DLL 地初始化函数。大多数情况下,你应当设置此选项为默认方式(如图4-1所示)。这将让 CodeWarrior 根据你在目标文件设置面板中选择的目标文件类型,来自动地为输出文件设置正确的入口点;
子系统(Subsystem): 子系统有三种选择:Native Windows CUI Windows GUINative 选项是用于开发驱动程序和其它异种系统模块的; Windows CUI 选项是用于开发使用控制台风格用户界面( console-style user interface,所谓的CUI )的应用程序的;Windows GUI 选项用于开发使用 Windows 图形用户界面(Windows graphic user interface,所谓的 GUI )的应用程序的。因为我们是用的示例程序是简单的基于控制台的 Windows 应用程序,所以我们选择了 CUI 子系统。通常情况下,CodeWarrior 会根据你在开始创建工程时选择的工程模板来自动地设置子系统的选项;
子系统 ID/用户 ID(Subsystem ID/UserID): 和子系统项一样,这个选项也是由 CodeWarrior 自动设置的。具体系统对应的 ID 为:Windows 3.1Windows 95 Windows NT 3.5 ID 3.10Windows NT 4 ID 4.00
生成连接映射(Generate Link Map): 这个选项允许你生成一个文本文件,其中包含了程序中使用到的所有的类和函数的详细信息。当需要通过匹配变量或函数名的地址来调试代码时,这个选项就非常有用了;
生成 SYM 文件(Generate SYM File): 这个选项允许你在连接过程中生成一个符号文件。大多数的调试器在进行调试时需要一个符号文件来逐步调试你的源代码。我们将在第五课中详细讨论这个内容。此外,你还可以通过选中生成 CV 信息项(Generate CV Info item)来生成一个可用于 CodeView 的符号文件。CodeView 是一个流行的 Windows 调试工具;
生成 CV 信息(Generate CV Info): 生成一个可用于 CodeView 的符号文件。CodeView 是一个流行的 Windows 调试工具;
命令文件(Command File): 这个选项要你选择一个用于指示复杂的连接选项设置的文本文件。该文件称为连接器命令文件。它被使用来指定要导入和导出哪些符号。除非你正在编写一个特殊目的的代码,你才不需要一个连接器文件。
正如你所知的,连接器的设置过程是非常复杂的。但是,在多数情况下,你不需要修改连接器的默认设置。Metrowerks 公司的开发人员已经帮助你很好地解决了这个问题。
附原文:
Step Two: Link your Program's Components
After the compiler compiles your code, the linker links together the files that make up your program.
The linker is responsible for combining your compiled code, which may be located in various files after they are compiled, and connecting the files together, along with any libraries the program requires. Linking makes sure each piece of code knows how to find the other elements of your program. For example, the linker modifies the machine code so that your call to printf() gets correctly routed to the MSL library function printf(), which does the all of the magic necessary to implement console I/O.
When you write a program, you split your code into functions that are stored in different files and are compiled to a single output file. In the case of the Hello World program I've been using as an example, this output file is an application. In some cases, functions used in a program may not even be stored in the same output file. When would this happen? When you write an application, for example, that contains a lot of code that is reused in several parts of your program, you might compile all of that reusable code into a single shared DLL. When you do this, the application can still access the code, but it needs to know where and how to find it when it needs it, hence the need for linking.
Types of Linking
In programming, a link is a pointer, either to an object, such as a compiled function, or to data, such as a variable or an array. There are two ways to link: hard linking and soft linking.
Hard linking occurs when all of the code is accounted for at link time. That is, if a function is used, the linker knows exactly where it is at link time and can create a direct link to it.
Soft linking (also known as weak linking) is when some of the code may not be accounted for at link time, but you know that it is in a DLL that will be accessible at runtime. In this case, the linker can tell your application to search for the DLL when it is needed.
Soft linking is the method used by many elements of your computer's operating system. Next, we'll take a look at some of the options that the linker provides.
A Detailed Look at Linking
To further appreciate the functionality of the CodeWarrior linker for the Windows platform, you need to understand how you can configure the CodeWarrior linker to accomplish what you need.
Configure the Linker to Suit your Needs
The choices in this lesson appear in the x86 version of the CodeWarrior linker panel (Figure 4-1). But many of the terms used in the x86 linker are applicable to other linkers.
Figure. 4-1 The x86 linker settings window.
Open the Settings window by choosing "Project Name" Settings from the Edit menu. (Project_Name is the currently active project -- in this case, Hello World x86.) Click on the x86 Linker (or similar) tab in the Settings window.
Let's take a look at some of the linker options:
Entry Point Usage: Under the Windows OS architecture, each code module has a number of entry points that may or may not be optional, depending on the type of code you are building. The nature of these entry points depends upon the type of output file you're making (application, library, or DLL) and other factors. The host OS uses this entry point information to properly call a DLL's initialization functions when an application demands its services. Most often, you will leave this set to Default as shown in Figure 4-1. This lets CodeWarrior automatically set up the proper entry points for the output file, based upon your choice of target in the x86 Target panel.
Subsystem: There are several settings here. Of interest to you are the Native, Windows CUI, and Windows GUI selections. The Native setting is for building drivers and other exotic system modules. The Windows CUI setting is for those applications sporting a console-style user interface (hence the name CUI). The Windows GUI choice supports applications that rely on the Windows graphic user interface (GUI). You'll notice that our simple, console-based Windows application uses the CUI subsystem, as expected. Normally, CodeWarrior sets the subsystem for you automatically when you choose a CodeWarrior project template to construct the project file.
Subsystem ID/UserID: Like the subsystem item, this item is typically set for you automatically. For the curious: Windows 3.1, Windows 95, and Windows NT 3.5 have an ID of 3.10, while Windows NT 4 has an ID of 4.00.
Generate Link Map: This option allows you to generate a text file that contains detailed information on the classes and functions used throughout your program. This can be extremely helpful when debugging code by matching addresses to variable or function names.
Generate SYM File: This option allows you to generate a symbol file during the link process. Most debuggers require a symbol file in order to step through your source code when debugging. This is discussed in more detail in Lesson 5. You can also generate a symbol file for CodeView, a popular Windows debugging tool, by checking the Generate CV Info item.
Generate CV Info: Generates a symbol file for CodeView, a popular Windows debugging tool.
Command File: This item lets you pick the name of an optional text file that directs sophisticated linking options. Called the linker command file, you use it to specify which symbols to import and export, and other advanced linking operations. Unless you're writing special-purpose code, you won't normally need a linker command file.
As you can see, the linker can be a relatively complex piece of software machinery. Luckily, in many cases, the default settings will suffice. Very rarely will you have to tweak the linker settings to get your programs to work. The folks at Metrowerks have done an excellent job of managing this thankless job for you. Yay, CodeWarrior!
CodeWarrior 使用教程
第五课           
Translated from CodeWarriorU.COM
关于调试
Purging pesky pests proves productive for programmers!
调试就是在你的程序代码中到一个问题并着手去解决它的过程。不管是谁,在编程时都难免出现一些输入或程序逻辑上的错误,一旦出现这种问题,很可能你想破了头也不到错误在哪。这个问题可能只是一个输入上的小错误或者只是一个变量用错了,因此你很难发现它。但是因为这个错误的存在使得你的程序无法实现预期的功能。有时候会更糟糕,以致你的机器都崩溃了。这时,你就必须借助调试来解决问题。
CodeWarrior 带有一个集成的调试器,可以让你在 CodeWarrior 主窗口中打开一个窗口来进行调试工作。使用调试器的方法是,从工程菜单中选择 Enable Debugger 项,然后重新编译你的目标文件。完成上述步骤后,你的程序就会在调试窗口中运行,此时你就可以隔离并改正你到的任何一个问题了。
通常进行调试时总要生成源程序的一个调试目标文件。该目标文件的源文件保持不变,但是需要预先设置置一些选项才能产生该程序的调试版本。通过点击工程窗口的弹出菜单中的调试目标文件,你就可以进入调试状态。
5-1: 调试器界面
5-1显示了 Hello World 这个程序在 CodeWarrior 调试器中进行调试时的情况。为了能够更方便地演示调试器的工作过程,我们在程序中添加了一小段循环代码(其中使用了 ix y 等三个变量)。调试器允许对代码执行单步逐行调试。当你在代码中移动时,你还可以查看程序中所有变量的值。在这种慢速移动(slow motion)”的方式下调试程序,你可以非常清晰地查看到代码的运行情况并很容易地解决问题。
但实际情况是,不是所有的问题都很容易解决的。有些问题就有可能要花费你长达数周的时间去解决。不管是经验多么丰富的编程老手,同样不可避免地要遇到这样的问题。作为一个初学者,你更容易遇到这样的问题,但是,你千万别被这种情况吓倒。请记住,从错误中学习,你能学到更多的东西。
请注意在调试器窗口左上角的那些按钮,它们是用于帮助你进行调试工作的。这些按钮从左到右分别是:运行程序(Run Program)、停止(Stop)、退出(Kill (Quit))、跳过(Step Over)、转入(Step Into)和转出(Step Out)。最后两个命令按钮是让你决定在单步调试程序时,如果遇到一个函数,是否要进入函数内部进行代码调试。或者你也可以使用跳过(Step Over)命令让该函数一次执行完毕,而不进入函数体中单步调试函数的代码,这样可以节省一些时间。如果你不能肯定函数是否正确无误,那么你可以转入(step into)该函数体内部对其进行单步调试。对于初学者,你应该单步调试每一个函数,至少可以练练手嘛!
5-1 所示的调试窗口包含三个主要部分:
栈窗格(The Stack pane)位于左上方的窗格是显示调用栈(也叫做调用栈的记录)的区域。当你在单步调试源码的过程中调用了多个函数时,在这个区域就显示出调试到达当前状态之前调用过的函数名清单。这个清单会随着你的调试过程,根据你是否转入函数内部的情况随时增减;
变量窗格(The Variables pane):  位于右上方的窗格是显示变量名和其值的区域。其中显示的是各个变量的名字和它们的值,而且还可以依据变量的类型让你查看(或修改)这些数据。在图5-1中显示的 Hello World x86 程序这个例子中,有四个变量:cix y。当你在代码中执行单步调试时,这些变量的值就会根据程序执行的过程实时地更新。例如,在调试过程中,你可以注意到后三个变量是如何取得它们的值的(这些值是在循环代码中赋给这些变量的),而变量 c 显示的是一个无意义的值(因为你还没有给 getchar() 输入任何值)。你可以使用这个窗格来发现不正常的变量值,从而发现程序设计上的错误这对于程序的调试成功,非常关键;
源代码窗格(The Source pane)位于下方的窗格显示了 Hello World x86 这个程序的 C 语言源代码,它也可以用来显示该代码的汇编语言版本。随着调试的深入进行,仅仅是程序的 C 语言代码可能不能提供足够的调试信息,这是你就需要查看该程序的汇编语言代码。你可以通过该窗口下端的弹出菜单来选择查看程序的源代码、汇编代码或者两者的混合显示。
你可能已经注意到在源代码窗格左边的红停止标记,该标记表示这是一个断点,它用于告诉调试器在此处停止执行程序。此功能可以帮助你很方便地快速到达程序中地某一位置,然后从此位置处开始调试程序。当你设置了一个断点,调试器运行到断点所在的行就停止下来,等待你的控制命令。在某行程序左边显示的蓝小箭头是当前程序的计数器,它表示指向的程序行为即将要执行到的语句。在图5-1中,我们已经设置了一个断点以便程序执行到完源代码中的循环块后在第二个输出语句前停下来。设置断点的操作也很简单,只需在你希望调试器停止的程序行的左端点一下鼠标即可。
下面我们来看看关于 CodeWarrior 调试器的一些参数设置。
全局调试器参数设置
通过选择编辑菜单中的“Preferences”项,可以看见 CodeWarrior 调试器有许多全局的参数设置。
设置内容
5-2: 调试器全局参数设置显示
设置显示面板(见图5-2)包含了一些决定调试器窗口上如何显示信息的参数设置。当你刚开始使用调试器时,你可以在这里把调试器窗口上的某些文字设置得大一些或小一些,或者用一些特殊的颜来显示它们。
颜设置允许你选择在源代码中用于突显观察点(watchpoints)和变量名的文字颜。这种设置非常有用,可以帮助你在调试代码时很容易地观察到观察点或变量值的变化。观察点可用来帮助你在整个程序中跟踪某个值并将其显示在一个窗口中。
其它设置的用途都解释得很明白了,你也不需要去更它们,默认设计就可以了。当你对调试器了解得更多了一些时,你才可能会的想去修改这些设置。
关于视窗的设置
5-3: 关于视窗的设置
在调试程序的过程中,你会发现桌面上很会就挤满了许多打开的窗口。关于视窗的设置(见图5-3)允许你设定在调试中隐藏或显示某些特定的窗口来管理这些窗口。默认的设置是隐藏非调试用的窗口,这在大多数情况下就可以了,不必修改它。
全局设置
全局设置窗格(如图5-4所示)包含了一些用于控制调试器操作方式的选项。
5-4: 全局设置
调试过程中缓存修改后的文件这一设置是用于决定修改后的文件需要缓存多长时间用的。缓存文件能够提高调试器的速度,但是要牺牲一些磁盘空间。
其余的设置解释得很清楚了。如果你只是一个初学者,那么默认设置就可以了。
远程连接
5-5: 远程连接
最后是远程连接设置(见图5-5)。该设置面板用于设置通过一个 TCP/IP 网络进行远程调试。这是一些高级设置,作为初学者,使用默认设置就可以了。
至于 Java 设置和 Java 调试面板用于使用 Java 语言进行编程的情况的,本课程不涉及这个内容。你可以学习一些相应的课程。
目标文件和工程特定的调试器参数设置
以下是关于被调试的目标文件和工程的设置项。当在 CodeWarrior 的工程窗口的目标文件标签中有一个目标文件被选中时,你再选择编辑菜单中的工程名设置(Project Name Settings)项即可看到这个设置窗口。
vs编程软件
5-6: 目标文件和工程调试器设置
调试器设置
重定位后的库...的位置(Location of Relocated Libraries, etc): 如果你正在调试的文件被移到其它位置了,或者你正在通过一个 TCP/IP 网络来进行远程调试,你就必须将这些文件所在的目录输入到这个文本框中;
在应用程序启动时在临时断点处停止(Stop at temp breakpoint on application launch): 默认情况下,这个选项在程序的起始位置中断调试器的运行。但你也可以设置为可在任何断点处中断;
自动查目标库(Auto-target Libraries): 选中这个选项可以自动地访问库。此选项用于正在调试的代码是一个库的一部分时,当你调试这些代码时,调试器将做应做的事”(例如装载库并调用任何初始化函数)
记录系统消息(Log System Messages): 这个选项用于强制将所有的系统消息写入一个记录文件中。当你遇到代码导致系统崩溃,并因系统崩溃又看不到屏幕显示时,就很有用了。当你重启机器后,你可以从记录文件中查看导致系统崩溃的消息。特别是今后你调试一些大型程序时,记录文件就很有用了,所以一定要学会读懂记录文件!
数据更新时间间隔(Data Update Interval): 选中这个选项并给它设置一个值,这样你就可以决定数据查看(data view)多长时间更新一次。数据查看更新得越频繁,调试器显示的数据值就越新;
缓存运行中的符号文件(Cache symbolics between runs): 在调试器运行时,符号文件(在编译过程中产生的一种文件)将被缓存起来。这可以提高调试的进程;
在观察点处停止(Stop at Watchpoints): 选中此项,当观察点值为真时就中断调试。观察点和断点一样是很有用的。不同的是,断点每次都是在一个特定的代码行处停止下来,而观察点是监视一个变量或内存的一块区域,当此被观察的对象发生改变时,它就中断调试过程。这和 VC 中的条件断点相似,都是在设定的条件被满足时中断调试过程的。这是一个很有用的调试工具。
x86 异常(Exceptions)
5-7:  x86 出错信息面板
当你调试有问题的代码时, CodeWarrior 调试器会截取到由程序的 bug 引起的某些奇怪的操作(即所谓的 异常——Exceptions) IDE 会将这些异常传递给一个特殊的代码——一个被称之为处理器的东西——由它来处理这些异常问题。
但是,对于大多数的异常,调试器只是简单地中断它的工作而已。为什么会这样呢?
如果你正在做一个高级开发,例如开发一个系统库或一个设备驱动程序,你可能想测试一下这些代码是如何处理这些异常的。只有调试器不截取这些异常时,你才能这么做。如果为了一些特定的原因,你希望调试器抓住某些特定异常发生时的控制("seize control of the moment" for specific exceptions) ,你就应该在这个设置面板上做一些设置。在该面板上显示的异常是 CodeWarrior 的集成开发环境可以处置的情况。点击那些你希望由调试器来处理的异常情况,然后点击保存按钮保存设置。就像前面提到的,这是很高级的编程设置,因此我们在这将不作详细讨论。总之,CodeWarrior 的调试器是一个非常先进的,而且很易于使用的附件。如果你学好了调试器的使用,它将会成为你开发软件的最重要的一个工具。
另外还有其它的可执行程序远程调试设置面板,它们是针对高级用户设置的,本课程不打算再作介绍。如果你需要同时调试多个可执行程序或通过 TCP/IP 网络来调试程序的话,你可以自行参看这几个设置面板。
附原文:
An Introduction to Debugging
Purging pesky pests proves productive for programmers!
Debugging is the process of tracking down a problem in your code and fixing it. The sad truth is there is always the possibility that your code contains typing or logic errors. Maybe you thought through a problem thoroughly but still missed a key step in solving it, or maybe you made a tiny typo in your code or used the wrong variable. In many cases, you'll know when you have a flaw in your logic or a programming error because your program will not function as expected. Or, in a worst-case scenario, your computer will crash. When this happens, it's time to begin debugging.
CodeWarrior includes an integrated debugger, which just means that you can view it while you're looking at other windows of CodeWarrior. Begin using the debugger by selecting Enable Debugger from the Project menu; then rebuild your target. Once that's done, your program will run within the debugger window, allowing you to isolate and fix any problems you find.
Quite often you will generate a debug target of the program. The target's source files remain the same, but many of the options required to produce a debuggable version of the program (such as generating a symbol file) are preset. This lets you flip into debug mode by just picking the debug target from the Project window's pop-up menu.
Figure 5-1: The debugger does its thing.
Figure 5-1 shows the Hello World debug program running within the CodeWarrior debugger. Note that we've added a few lines of code to the program in the form of a loop using the variables i, x, and y. We added this code in order to more easily demonstrate how the debugger works. The debugger allows you to step through your code line by line. As you move through the code, you can also note the values of all of your variables. By forcing your program to run in this "slow motion" mode, you can see exactly how your code is working and very easily track down bugs.
I lied. Not all bugs are easy to track down. I've personally worked on bugs that have taken weeks to figure out and fix. Anyone who has programmed for any length of time probably has similar horror stories. Don't let them get you down. On top of everything else, you are a beginner, so you will inevitably be writing buggy code. Remember: we learn more from our mistakes than our successes. OK, I'll stop now.
Notice the buttons at the top left corner of the debugger window. These buttons assist you in stepping through your code. The buttons are, from left to right: Run Program, Stop, Kill (Quit), Step Over, Step Into, and Step Out. The last two step commands allow you to decide whether you want to step into a particular function in your code, or simply step over it and let the entire function execute in one fell swoop. If you know that a certain function does not contain the bug, you can easily step over it and save time. If you're not sure, you can step into it and walk through its code line by line. As a beginner, you should assume that nothing is bug-free -- and you can use the practice.
The Debugging window (Figure 5-1) contains three main sections:
The Stack pane (top left) contains a display of the call stack, also known as call history. As you step through source code and call function after function, this area displays the history of functions you have called to get to the current location. This list grows and shrinks as you step through different sections of a program and according to whether you step into functions or not.
The Variables pane (top right) shows variable names and their values. Depending on the type of variable, this display will vary to allow you to view (and edit) the data accordingly. In the case of the Hello World x86 program, shown in Figure 5-1, there are four variables: c, i, x, and y. As you step through your code, the values of the variables will update in this window in real time. For example, notice how the latter three variables have realistic values (since these values were set by the code loop), while c has a nonsense value (you have yet to enter a character into the getchar() function). Using this pane to spot odd or unexpected values is one of the keys to your debugging success.
The Source pane (bottom) displays the C source code for Hello World x86 program. It can also display the assembly language version of the code. Depending on the type of bug you are tracking down, the C source display may not provide enough detail. In that case, you may need to view the program at a finer level of detail -- in assembly language. The pop-up menu at the bottom of this window lets you choose whether you view the program as source, assembly language, or a mix of the two.
Note the red stop sign on the left edge of the Source pane. The stop sign indicates a breakpoint. You set breakpoints in the debugger to tell it where to stop executing your code. This feature can come in very handy when you want to quickly move to a certain point in your program and begin working from there. When you set a breakpoint, the debugger will automatically stop at that line of code and will let you take control. The small blue arrow is the current program counter; it points to the line of code that is about to be executed. In the Figure 5-1, we have set the breakpoint so that the program executes the code loop before halting on the second printf() statement. You can set and clear breakpoints by simply clicking to the left of the line of code you're interested in.
Let's look at some of the preferences for the CodeWarrior debugger.
Global Debugger Preferences
There are many global preferences in the CodeWarrior debugger. Let's take a look. These settings can be reached by selecting Preferences from the Edit menu.
Display Settings
Figure 5-2: Display settings.
The Display Settings panel (Figure 5-2) contains preferences that determine how the debugger displays information in its windows. As you begin to use the debugger, you will find that you want certain text to appear larger or smaller, or to be displayed in a specific color. You can select these options here.
Color settings allow you to choose the text color used by watchpoints or variable names when the code alters them. This allows you to see if a watchpoint or variable is being changed without your knowledge as the code executes. Watchpoints are useful if you want to track a variable at intervals throughout the program and want it to be conveniently displayed in a window.
The other settings are self-explanatory. In most cases, the default settings will work. As you learn more about the debugger, you may want to alter these settings.
Windowing Settings
Figure 5-3: Windowing Settings.
When debugging, you will quickly find that your desktop is cluttered with lots of open windows. The Windowing settings (Figure 5-3) allow you to manage windows by choosing to hide or show certain types of windows when you start debugging. The default setting, Hide Non-Debugging Windows, will probably suffice in most cases.
Global Settings
The Global Settings pane (Figure 5-4) contains options that control the way the debugger operates.
Figure 5-4: Global settings.
Cache Edited Files Between Debug Sessions setting allows you to choose how long to cache edited files between debug sessions. Caching files can increase the speed of the debugger but will also take up precious disk space.
The remaining settings are self-explanatory. The default settings will serve you well as you begin to use the debugger.
Remote Connections
Figure 5-5: Remote Connections.
Lastly, the Remote Connections Settings (Figure 5-5) allow you to remotely debug code across a TCP/IP network. These settings are advanced, and the default values will work fine as you learn to debug in CodeWarrior.
Note that the Java Settings and Java Debugging panels are for the Java Programming Language and are not covered in this course. However, other courses are available for this language!
Target and Project-Specific Debugger Preferences
The following settings are specific to each target and project that is debugged. You can reach them by selecting Project Name Settings from the Edit menu, while a target is selected in the Target tab in the CodeWarrior Project window.
Figure 5-6: Target and Project Debugger Settings.
Debugger Settings
Location of Relocated Libraries, etc: If the file you are debugging has been moved, or if you're debugging remotely over a TCP/IP network, you must type the file's directory path into this text box.
Stop at temp breakpoint on application launch: By default, this option breaks the debugger (stops execution of the code) at the beginning of your program. However, you can choose to break at any point.
Auto-target Libraries: Check this setting to automatically access libraries. This can assist you when debugging code that is part of a library. The debugger will "do the right thing" (such as loading the library and calling any initialization functions) as you step into its code.
Log System Messages: This setting forces all system messages to be written to a log file. This can be useful if your code is crashing your computer, and you can't see the screen during the crash. The messages will be written to a log file that you can view after you restart your computer. Log files can save you many headaches later. Learn to read them!
Data Update Interval: By setting a value here and checking this box, you can choose how often data views are updated. The more often you update data views, the more up-to-date the displayed values will be in the debugger.
Cache symbolics between runs: Symbolic files (files generated during the compile process) will be cached and referenced between debugger runs. This can speed up the debugging process.
Stop at Watchpoints: To stop at watchpoints when they are equal to true, check this option. Watchpoints can be very useful, similar to breakpoints. Whereas a breakpoint will break into the debugger on a specific line of code every time, a watchpoint monitors a variable or area of memory and breaks into the debugger only when the contents change. There is also a feature called a conditional breakpoint that will halt execution when the condition is true. Depending on your needs and the way you like to work, these advanced features can be very helpful to you as a programmer.
x86 Exceptions
Figure 5-7: The x86 Exceptions panel.
As you debug questionable code, the CodeWarrior debugger intercepts certain oddball operations (termed exceptions) brought about by a program bug. The IDE passes these exceptions along to special code -- appropriately known as a handler -- that attempts to deal with the problem gracefully.
However, for many exceptions, the debugger simply stays out of the way. Why is this?
If you're doing advanced programming such as writing a system library or a device driver, you may want to test how its code handles some of these exceptions. By not intercepting many exceptions, the debugger lets you do this. If for some reason you want the debugger to "seize control of the moment" for specific exceptions, you can direct it to do so with this panel. The exceptions shown here are ones that the CodeWarrior IDE is prepared to handle. Click on the exception types you need the debugger to field for you, and then click Save. As mentioned previously, this is heavy-duty programming material, so we won't go into any details here. The debugger is a very advanced, yet very easy-to-use addition to the CodeWarrior environment. If you learn to use it well, it will become one of the most important tools in your software development arsenal.
Note that the Other Executables and Remote Debugging panels are for advanced users and are not covered in this course. If you ever have a need to debug multiple-executables at a time and/or debug over a TCP/IP network, you will know it!
CodeWarrior 使用教程
第六课       
Translated from CodeWarriorU.COM
 
这将是最简单的一课,但它却能够帮助帮助你更好地组织你的思想和开展工作。
正如我们在第一课中学到的,可以有许多中方式来为手头某个特定的任务定制 CodeWarrior。你可以定制 CodeWarrior 的外观和许多设置项,以便使得编译、连接和调试你的程序变得更快、更简单。
下面我们来看看可用于帮助你更有效地使用 CodeWarrior 的定制选项。因为定制 CodeWarrior 会改变 IDE 的所有行为,所以我们可以想到在编辑菜单下到这些设置项。在那儿你将到选择、命令和键绑定(Key Bindings)等菜单。点击其中的命令项就会显示定制 IDE 命令设置的窗口。定制 IDE 命令的面板包含两个主标签页。命令标签页允许你很容易的定制出现在每个 CodeWarrior 菜单中的命令。通过该标签页你也可以修改在内置的文本编辑器中使用的命令,例如选择文本和移动光标等命令。在这个设置窗口中,你还可以创建任何用于触发某个菜单命令项的组合键(也称之为键绑定——key binding),以此来启动一个应用程序,或者执行一个脚本。当然,你也可以设定一个某个命令项是否出现在一个菜单中。
定制 IDE 命令的面板中的另一个标签页是工具条项目标签页,在这个标签页中你可以看到在 CodeWarrior 的工具条中显示的项目,例如显示在每个文本编辑器窗口上方(或下方,取决于你的参数设置)
在工具条项目标签页中,你只需点击一个工具条图标然后把它拖到主菜单下面的工具条或者文本编辑器窗口的工具条中,就可以往相应的工具条中增加一个命令。还可以把工具条中的一个图标拉到工具条的最后。从 CodeWarrior 的窗口菜单中的工具条子菜单中选择重设窗口的工具条(Reset Window Toolbar) 或重设浮动工具条菜单项,还可以重新设置该工具条。
在大多数情况下,CodeWarrior 的默认设置就可以满足你的要求了。偶尔你也可能发现一个通过改变一个菜单或工具条的方式来自动完成一些任务。关于 Codewarrior 的定制,你可以参考它的在线文档获得更多的信息。

CodeWarrior 定制示例

    通过定制 CodeWarrior,你可以增强你的工作环境和提高工作效率。     除了对菜单和工具条的简单修改之外,你可能还想只需点击一个图标或键入一个组合键就可以执行一个脚本或启动一个应用程序。有时当你正在参与一个非常庞大的开发项目的时候,需要做一些定制工作来解决自动设置带来的混乱问题。例如,你可以创建一个脚本来完成以下这些工作:
自动删除编译过程中产生的多余的文件;
将输出文件复制到一个本地或网络上的目录中以做备份;
为你的工程创建一个安装应用程序。
对于 Windows 用户而言,可以通过 VBScriptPerl或其它使用通用对象模型(Common Object ModelCOM)接口的脚本语言来驱动 CodeWarrior Pro 5 IDE 完成一系列复杂的操作。如果使用 VBscript,你还需要一个可以执行 VBScript 的的应用程序( Internet Explorer 5),或者是一个 Scripting Host 工具——可以从以下网址下载 msdn.microsoft/scripting/windowshost/
下面的 VBScript 脚本用于指导 CodeWarrior IDE 删除当前工程中的所有目标(object)文件,以便执行一个干净的构建(build),然后再执行构建工作(编译和连接操作)。该脚本还打开一个编辑器窗口来显示操作结果(成功或失败的)的总结。这个脚本是有点长,但设计得很好,因为其中包含了许多用于错误检查的代码。
' 文件名:Build.vbs
' 作者: Jim Trudeau, Metrowerks
' 以及版权声明信息
option explicit
'所有的变量都必须进行声明
dim CW
          dim project                      '默认的工程
      dim textDocument          '用于保存报告的文本文档
      dim textEngine                '用于处理文本的对象
      dim eol                            '行尾字符格式
      dim result                        '返回的值
eol = chr(13)                    '设置行尾字符
'创建 CodeWarrior 的一个实例
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
'创建一个文本文档并获得其引擎(Engine)
set textDocument = CW.OpenUntitledTextDocument()
set textEngine = textDocument.TextEngine
'得到默认的工程
set project = CW.DefaultProject
'错误控制
if TypeName(project) = "Nothing" then
textEngine.InsertText("Script operates on default project." &eol)    textEngine.InsertText("There must be at least one open project." &eol)
else
dim target  '当前目标
dim buildmessages '错误和警告
'*** 获得当前目标
set target = project.GetCurrentTarget
textEngine.InsertText("Build Information" &eol)
'显示名字
result = target.name
textEngine.InsertText("Building target " &result &eol)
'*** 删除所有的对象代码目标
RemoveObjectCode true
'*** 获得构建代码后的消息
set buildMessages = target.BuildAndWaitToComplete
ProcessMessages (buildMessages)
end if
'========================================================= ' ProcessMessages - get errors and warnings, process them ' receives build messages '=========================================================
sub ProcessMessages (messages)
dim result          '返回值
dim messageList '消息收集
'*** 获得错误的数量
result = messages.ErrorCount
if result = 0 then textEngine.InsertText(eol &"Build Succeeded." &eol)
else textEngine.InsertText(eol &"!!!BUILD FAILED!!!" &eol)
'*** 显示错误的数量
textEngine.InsertText("Number of errors: " &result &eol)
'*** 获得错误清单
set messageList = messages.Errors
'*** 处理错误
ProcessMessageList (messageList)
end if
'*** 检测是否有警告信息
result = messages.WarningCount
'*** 显示数量
textEngine.InsertText("Number of warnings: " &result &eol)
'*** 取得警告信息并处理之
if result then
'*** 获得警告信息清单
set messageList = messages.Warnings
'*** 处理警告信息
ProcessMessageList (messageList)
end if

end sub
'========================================================= ' ProcessMessagelist - loop through messages, report info ' receives message collection, could be errors or warnings '=========================================================
sub ProcessMessageList (messageList)
dim result '返回值
dim index '循环计数器
dim message '个人信息
'*** 遍历消息清单
for index = 0 to messageList.Count-1
'*** 获得个人信息
set message = messageList.Item(index)

'*** 获得消息文本
result = message.MessageText

'*** 显示消息文本
textEngine.InsertText(result &eol)

****在错误中忽略一行(skip a line between errors )
textEngine.InsertText(eol)
next
end sub
因为使用了微软公司的 OLE/COM 查看器工具,这个 IDE 支持许多 COM 对象。一个脚本语言可以使用这些对象来与 CodeWarrior IDE 进行通信。关于 CodeWarrior 定制与脚本机制,还有很多优秀的功能,但在这里就不详述了,因为 CodeWarrior 把这些功能实现得非常好。打开上面所描述到得窗口自己看看吧!当你熟练使用了 CodeWarrior 的一些功能之后,你会发现它是非常的易用!
附原文:
An Introduction to Customization
This will be the simplest lesson, but it will provide you with the flavoring that will help you organize your thoughts and work much better. As I mentioned in Lesson 1, there are many ways to customize CodeWarrior for the specific development task at hand. You can customize the look and feel of CodeWarrior as well as a multitude of options for compiling, linking, and debugging your code faster and more easily. Let's look at some of the customization options available to help you use CodeWarrior more efficiently. Since customizing CodeWarrior affects the IDE's overall behavior, we can expect to find such settings under the Edit menu. There you'll find the menu selection, Commands, and Key Bindings. Pick this item and the Customize IDE Commands settings window appears. The Customize IDE Commands panel contains two main tabs. The Commands tab allows you to easily customize the commands that appear in each CodeWarrior menu. With it you can also modify commands used within the built-in text editor, such as selecting text and moving the cursor. Using this window, you can create any key combination (key binding) that triggers any menu command, launches an application, or executes a script. You can also choose whether an item should appear on a menu. The Toolbar Items tab allows you to see which items appear in various CodeWarrior toolbars, such as the one that appears at the top (or bottom, depending upon your preference) of each text editor window. In the Toolbar Items tab, you can add a command to the toolbar (either the toolbar under the main menu or the text editor's window toolbar) by clicking on a toolbar item icon and dragging it to the toolbar to which you want to add it. Dropping the icon on the toolbar adds it to the end of that toolbar. You can reset the toolbar by selecting Reset Window Toolbar or Reset Floating Toolbar from the Toolbar submenu in the Window menu. In most cases, CodeWarrior's default settings will work for your needs. Every once in a while, you may find a way to automate some task by changing a menu or toolbar. Consult the CodeWarrior online documentation for more information about CodeWarrior customization.

CodeWarrior Customization Examples

By customizing CodeWarrior, you can enhance your work environment and become much more productive. In addition to simple changes to menus and toolbars, you may want to execute a script or launch an application by clicking an icon or typing a key combination. Sometimes, when you work on very large development projects, there is a need to automate more intricately and customize your development environment. For example, you could create a script to do any of the following:
Automatically delete extra files created by the build process.
Copy your output file or files to a local or network directory for backup.
Build an installer application for your project.
For Windows, you can drive the CodeWarrior Pro 5 IDE through a complex sequence of operations using VBScript, Perl, or another scripting language that uses the Common Object Model (COM) interface. For VBScript, you'll need an application that executes VBScript (such as Internet Explorer 5), or the Scripting Host utility, available at msdn.microsoft/scripting/windowshost/. The following VBScript directs the CodeWarrior IDE to perform a "clean build" by removing all of the object files in the current project, and then executing the build (compile and link operations). The script also opens an editor window and displays a summary of the operation (success or failure). It's a trifle long, but that's fine because the script includes a lot of error-checking code.
' file: Build.vbs
' author: Jim Trudeau, Metrowerks
' and warning messages.
option explicit 'all variables must be declared
dim CW dim project 'default project dim textDocument 'text document object to hold report dim textEngine 'the object for dealing with text dim eol 'end-of-line character for formatting dim result 'returned values
eol = chr(13) 'set end of line character
'create an instance of CodeWarrior set CW = CreateObject("CodeWarrior.CodeWarriorApp")
'create text document and get engine set textDocument = CW.OpenUntitledTextDocument() set textEngine = textDocument.TextEngine
'get the default project set project = CW.DefaultProject
'do some error control here if TypeName(project) = "Nothing" then textEngine.InsertText("Script operates on default project." &eol) textEngine.InsertText("There must be at least one open project." &eol) else dim target 'current target dim buildmessages 'errors and warnings
'*** get the current target set target = project.GetCurrentTarget
textEngine.InsertText("Build Information" &eol)
'display name result = target.name
textEngine.InsertText("Building target " &result &eol)
'*** remove all object code target.RemoveObjectCode true
'*** get messages after building code set buildMessages = target.BuildAndWaitToComplete
ProcessMessages (buildMessages) end if
'========================================================= ' ProcessMessages - get errors and warnings, process them ' receives build messages '=========================================================
sub ProcessMessages (messages)
dim result 'returned values dim messageList 'message collection
'*** get the number of errors result = messages.ErrorCount
if result = 0 then textEngine.InsertText(eol &"Build Succeeded." &eol) else textEngine.InsertText(eol &"!!!BUILD FAILED!!!" &eol)
'*** display number of errors textEngine.InsertText("Number of errors: " &result &eol)
'*** get the list of errors set messageList = messages.Errors
'*** process the errors ProcessMessageList (messageList) end if
'*** determine whether there are warnings result = messages.WarningCount
'*** display number textEngine.InsertText("Number of warnings: " &result &eol)
'*** get warnings and process them if result then '*** get the list of warnings set messageList = messages.Warnings
'*** process the warnings ProcessMessageList (messageList) end if
end sub
'========================================================= ' ProcessMessagelist - loop through messages, report info ' receives message collection, could be errors or warnings '=========================================================
sub ProcessMessageList (messageList)
dim result 'returned values dim index 'loop counter dim message 'individual message
'*** walk through the list of messages for index = 0 to messageList.Count-1 '*** get the individual message set message = messageList.Item(index)
'*** get the text of the message result = message.MessageText
'*** display the message text textEngine.InsertText(result &eol)
'skip a line between errors textEngine.InsertText(eol) next
end sub
Using Microsoft's OLE/COM Viewer utility, the IDE supports a large number of COM objects. A scripting language can use these objects to communicate with the CodeWarrior IDE. There is a lot here regarding CodeWarrior scriptability and customization, but there isn't much to say about it, since CodeWarrior implements it so elegantly. Open the windows described above and explore! You will find that CodeWarrior is even easier to use when you have precise control over some of its functions!
CodeWarrior 使用教程
第七课  Microsoft Foundation Classes (MFC)
Translated from CodeWarriorU.COM
库是什么?这是一个你必须知道如何回答的问题!一个库就是一个包含了你程序运行时需要调用的函数的文件。在库中提供的典型函数有:文件和屏幕的 I/O 函数,内存管理服务,3-D 图形显示以及科学计算函数。使用库可以缩短你的开发时间并简化你的应用程序。在开发程序当中,你会经常地使用到库。以下是几种在编程中使用库的方法:
将你需要用到的函数写在一个源文件中,然后把它编译成为一个库或 DLL
使用别人提供的库;
使用一个其他人提供的共享库( DLL)
编写你自己的库或 DLL
第一种使用库的方法很简单。你只需在一个 C C++ 源文件中写入源代码,然后编译、连接并执行它。这是我们在本课程中早已学到的知识。就这样你就可以创建你自己的一个库,就这么简单!
使用别人的库
使用别人提供的库稍稍有些不同。由第三方开发商提供的库通常包括以下几个文件:库文件(通常是以 .lib 为后缀名的文件),一个头文件(后缀名为 .h) 以及(我们希望有但不一定有)一些文档(后缀名为.doc)。头文件是用于让编译器将你的程序中使用的函数与库中的函数匹配起来的。你只需将头文件包括到你的源文件中,然后就可以象调用操作系统的函数一样来调用库函数了。在大多数情况下,通常将 .h .lib 的文件放置于源文件目录中,就像 CodeWarrior 定义的一样。这个路径通常在你的工程文件所在的目录,或者其下的子目录中。需要注意的是,CodeWarrior 提供的某些特定的库,例如它的 MSL,就不是放在你的工程所在的目录中。实际上,CodeWarrior 有一个特定的存取路径,叫做 system paths, 它指向 CodeWarrior 自带的库和头文件存放的目录。
使用一个共享库
第三方厂商将库函数与它们的产品打包起来发行,而不是以源文件的方式发行这些库。如果你是一个开发商,使用这样的方式来发行你的库会更安全一些,特别是你希望由此来保护你的知识产权时。但是这种方法也存在一个问题,就是如果在库中存在一个 bug 的话,用户是没办法对之进行修改的,只能从开发商那里取得一个修正后的版本。但是这做起来也有许多困难。
共享的 DLL
DLL 和库文件非常相似,两者之间唯独的不同就是库文件通常都会被编译并连接到你的应用程序中,而 DLL 则是存放在你的系统目录下或者是在编译后的应用程序的目录下的一个独立的文件。当应用程序运行时,它会自己去到该 DLL 并使用它的服务。DLL 文件的后缀名为 .dll
使用共享的DLLs 的好处
DLL 的优点就是它能够被共享。那么,共享的好处在哪里呢?首先,可以让多个应用程序使用一个 DLL。当几个程序都使用一个占用很大磁盘和内存空间的 DLL 时,就很有用了。另外,如果一个 DLL 包含了一个 bug,然后被修正了,那么所有使用这个 DLL 的程序都会被自动更新,这样就可以大大地节约你的开发时间。那么哪种库应该由多个应用程序共享呢?比如操作系统中的每一个视窗就是使用共享的 DLL 得来的。
使用共享 DLL 需要注意的地方
CodeWarrior IDE CD-ROM 中有许多库,在因特网上还有成千上万的库。记住,当你使用第三方厂商的库时,千万要先查查毒。这就如同出门前要关门一样的有必要。
CodeWarrior 自带的库分为两类。一个是 MSL,它提供 ANSI C/C++ 的标准函数。MSL 已经被移植到许多平台上了,因此可以在 WindowsMac OS Solaris 系统中使用它的函数。另一个是供特定平台使用的库。例如,在 Windows 版的光盘中,你可以发现提供了使用 Windows APIs MFC 库的Windows 32 支持库,它提供了编写面向对象的 Windows 应用程序的框架。随着你的编程经验的增长,你将发现这些特定库的更多的用途。
MFC是什么?
Microsoft Foundation Classes (MFC) 提供了快速开发 Windows 应用程序的函数集。虽然你已经可以自由地直接调用 Win32 APIs,但使用 MFC 会更简单一些,因为 MFC 提供了诸如创建一个窗口或者使用智能默认设置和错误检查代码来编写文件这样的基本服务。MFC 可以在 Windows 版的 CodeWarrior 光盘中到,但不一定是最新的版本。你可以查看 Metrowerks 的帮助页面来获得相关信息,因为微软公司会为 CodeWarrior 的发行版本提供一些升级补丁。
对于特定平台,比如Macintosh 的开发者可以使用 Metrowerks 自己的 PowerPlant 类库。PowerPlant 是为编写 Mac 应用程序而设计的面向对象的应用程序框架。我们在本课程中并不打算讨论 PowerPlant,但你应知道,它与 MFC 在许多方面是类似的。你在这里学到的知识也将帮助你理解 PowerPlant。我们讨论 MFC 时,其中的许多概念也可以应用到 PowerPlant 上。
注意: Macintosh 版的 CodeWarrior 光盘中也有一个 MFC 的库文件,但不像 Windows 版的光盘,这里不包含源代码。使用 MFC,你可以编写跨平台的代码,还可以同时为 Macintosh(使用 PowerPlant) PC(使用 MFC) 编译这个代码。是不是很酷?
应用程序框架
就像前面提到过的一样,MFC 是一个应用程序框架。也就是说,MFC 是一个源代码或库格式的 C++ 类集。使用这个框架可以在较短时间内创建一个支持高级操作系统功能的完整的应用程序。此外,MFC 还包含了用以构造你的用户界面的工具。这些工具,特别是图象编辑器、对话框编辑器和资源编译器,能够帮助你构造用户界面中的图形元素(在本课最后一节中将解释如何安装这些有用的组件)。一个应用程序框架只涉及到应用程序的标准用户界面,而与该应用程序的特有的内容无关。换句话说,这个框架能够帮助你构造一个功能强大的用户界面,把你的时间节省下来以便让你有更多时间去编写实现应用程序目的的其它函数。因此你不必担心会花很多时间来创建一个和其它一些运行在操作系统环境中具有同样 Windows 化界面的应用程序,因为你只需去运用那些库就可以达到这些目的了。
7-1 MFC 的用户界面构造器
应用程序框架还能处理分发消息(例如用户击键的动作、鼠标点击、绘画消息等等)到应用程序中的许多按钮、列表、窗口和控件上。这个功能帮助你能够集中注意力到实现应用程序的功能而不是界面上,把那些创建界面的工作留给应用程序框架吧。
应用程序框架提供了许多超过老式风格的 roll-your-own 方法。首先,这些代码是成熟和经过严格测试的。每天都有成百上千的开发者使用 MFC 来开发应用程序。每当 Windows 操作系统增加了一些新的功能的时候,MFC 也会不断地被升级,你可以通过使用新版本的 MFC 来编译你的应用程序,以便获得所有新的功能特,而且几乎不用对应用程序的源代码做任何修改。
应用程序框架还提供可重用的代码。代码重用是象 C++ 这样的面向对象的编程方法得以发展的主要原因之一。通过将可重用的代码封装为类,你可以很容易地使用已经存在的类来添加需要的功能。代码重用使得你可以使用经过严格测试的代码来构造你的应用程序,并很容易获得预期的功能。而且你还可以很容易地将你编写的类与其它需要同样功能的人进行共享。
与你自己创建所需框架而言,使用应用程序框架有时会使得你的程序稍大一些。因为现代计算机通常都配置了足够数量的内存,而且这个框架可以作为一个 DLL 来使用,所以增加这么一些程序体积还是很值得的。PowerPlant MFC 是两个很值得你去熟练使用的高效的工具。
如何安装和使用 MFC 接口工具
MFC 接口工具(包括对话编辑器和图象编辑器)可以在 CodeWarrior 安装光盘上到。但是,它们并不是作为 CodeWarrior 安装程序的一部分存在的,因此在完成了 CodeWarrior 的安装后,在硬盘上你还是不到 MFC。为了安装这些工具,请按照以下步骤进行:
iTOOLS.Exe 文件。这是安装 MFC 接口工具必须的安装程序。iTools.Exe 文件可以在 C:\Program Files\Metrowerks\CodeWarrior\BinSDK\bin\ 目录下到。如果在那儿不到这个文件,那么就到 CodeWarrior 的安装光盘上去。该文件应该在 Extras\SKDs\Win32\Microsoft Win32 SDK Tools\目录下。
注意: 该安装文件 (iTOOLS.Exe) 将会询问你想在哪里保存 MFC 接口工具。但不行的是,它却没有提供定位目录用的浏览(browser)按钮可用,因此你必须手工的输入一个路径。如果你并不能确定你输入的路径是否就是你想要保存这些工具的路径,你可以先在那个目录中到一个文件,然后通过查看该文件的属性(在该文件上点击右键然后选择 Properties ),从这个属性框上你可以复制到完整的路径,并把它填入到安装过程中提示要输入安装路径的编辑框中。
这些工具将要被安装到目的安装路径的 \bin\ 目录中。在这个路径中你将可以到对话框编辑器(DlgEdit.Exe),图象编辑器(ImagEdit.Exe)和其它一些有用的工具;
如果你正在开发 Windows 95 Windows NT,需要的工具可以在 \bin\win95 \bin\winnt 目录中到。
附原文:
Libraries
What's a library? This is a question you need to know the answer to! A library is a file that contains compiled code that your programs can call upon when needed. Typical functions that libraries can offer are file and screen I/O, memory management services, 3-D graphics display, and scientific computations. Libraries can save you time in the development process and help simplify your software applications. Chances are that you will use libraries quite often in developing programs. There are several ways to use library code in your programs:
Write it yourself in a source code file, and optionally compile it as a library or DLL.
Use a library provided by someone else.
Use a shared library (a DLL) provided by someone else.
Writing Your Own Library or DLL
The first method is simple. When you include code within a C or C++ source file, you complete the program by simply compiling, linking, and executing it. This is the way we've been working thus far during this course. See, you were using them and you didn't even know it. You're good.
Using Someone Else's Library
Using someone else's library changes things a bit. Libraries provided by third-party vendors contain several files: the library file itself (usually with a name ending in .lib), a header file (.h), and (hopefully) some documentation (.doc). The header file is required so that the compiler can match function calls in your program to functions within the library. You include the header file in your source file and then simply call the library functions as if they were part of the operating system. In most cases, you place the .h and .lib files in your source path, as defined by CodeWarrior. This path is usually in the directory where your project file is located, or inside a subdirectory of this directory. Note that certain libraries CodeWarrior provides, such as its MSL, reside in a different directory from the one your project lives in. In fact, CodeWarrior has its own special access paths, called system paths, which point to the directories of its own libraries and header files.
Using a Shared Library
Third-party vendors ship library files with their products so that they don't have to part with their source code. If you're the vendor, it's much safer to include the library and a header file (as opposed to a C or C++ source file and a header file), especially if you want to protect your intellectual property. The problem with this approach is that if there's a bug in the library you've gotten from a third-party vendor, you'll be unable to fix it yourself. You'll have to obtain a fix for the library from that vendor, and who knows how easy that will be?
Sharing DLLs
A DLL is very similar to a library file. In fact, the only difference is that a library file usually gets compiled and linked into your application, while a DLL is a separate file that either sits in your system directory or in the same directory as your compiled application. When your application runs, it locates the DLL and uses its services on the fly. DLLs are typically identified by the .dll extension.
Benefits of Sharing DLLs
The benefit of a DLL is that it can be shared. OK, that was pretty obvious. What is the benefit of sharing? First, more than one application can make use of a DLL. This can be especially useful when several programs use a large DLL that takes up lots of disk space and/or memory. Also, if a DLL contains a bug and is updated, all programs that share it will be automatically updated. This saves you and your run-time processing large amounts of time. So what kind of library would need to be shared by multiple applications? Oh, stuff like the very windows you see in your operating system environment.
Note on Using Shared DLL files
The CodeWarrior IDE CD-ROM contains dozens of libraries, and hundreds more are available on the Internet. Always, always, always run a virus check or disinfectant program before incorporating third party code into your product. It's a good habit, like locking the door every time you leave the house.
The libraries included with CodeWarrior fall into two categories. The first is the MSL, which provides ANSI C/C++ standard functions. The MSL has been ported to many platforms, so its functions are available in various flavors of Windows, the Mac OS, and Solaris. A second set of libraries is intended for platform-specific uses. For example, on the Windows CD, you'll find Windows 32 support libraries that interface to the Windows APIs and the MFC libraries, which provide an object-oriented application framework for writing Windows applications. When you gain more programming experience, you will find more uses for the specialized libraries.
What is MFC?
The Microsoft Foundation Classes (MFC) provides a set of functions that let you quickly write Windows applications. While you're free to call the Win32 APIs directly, it's easier to use the MFC because the classes provide basic services such as creating a window or writing a file along with intelligent default settings and error-checking code. MFC is available on the Windows CodeWarrior CD but may not be the very latest version of the classes. Check with the Metrowerks help desk for that information, as Microsoft will send update patches between CodeWarrior releases.
For the sake of platform parity, Macintosh developers can use Metrowerks' own PowerPlant class libraries. PowerPlant is an object-oriented application framework designed for writing Mac applications. Although we will not discuss PowerPlant here, it and MFC are similar in many ways, and what you learn here will help you to understand PowerPlant as well. When I discuss MFC, it is safe to assume that the same concepts apply to PowerPlant.
Note: There is also a library version of MFC on the CodeWarrior Macintosh CD, but unlike the Windows CD, the source code is not included for the Macintosh. Using MFC, you can write cross-platform code. Once you have written the code, you can compile it for both the Macintosh (using PowerPlant) and the PC (using MFC) at one time. Pretty cool, huh?
Application Framework
As mentioned earlier, MFC is an application framework. This means that MFC is a collection of C++ classes in source code or library format. The framework can create a complete application that supports advanced operating system features in a fraction of the time it would take to do it all by hand. Note also that MFC contains tools to facilitate building your user interface. These tools, specifically the Image Editor, Dialog Editor, and Resource Compiler, help you build the graphic elements of the user interface (the last section of the lesson explains how to install these useful components). An application framework concerns itself primarily with the standard user interface of the application, as opposed to the specific content of the application. Put another way, the framework helps you build a robust application interface to interact with the user, giving you more time to write the other functions that implement the application's purpose. So you don't have to worry about spending time and energy coding a scroll bar or close box from scratch to appear like all other applications running in the operating system environment. You can just use the libraries and get on with the real creative stuff.
Figure 7-1 MFC's user interface builder.
Application frameworks also handle the dispatching of messages (such as user keystrokes, mouse clicks, drawing messages, etc.) to the many buttons, lists, windows, and controls in your application. This allows you to keep your application's code focused on its own features rather than on addressing interface questions like "How do I tell when the user pressed the Enter key?" or "How do I know when I need to redraw the contents of a window?" It's really the little things that mean the most once you are actually using the application.
Application frameworks provide many benefits over the old-style roll-your-own approach. For one thing, the code is mature and well tested. Hundreds of applications are being built by hundreds of developers using MFC every single day. It is constantly being updated as new features are added to the Windows operating system. In many cases, you can recompile your application with a new version of MFC and take advantage of all the new features with few if any changes to your source code.
Application frameworks also provide reusable code. Code reuse is one of the main reasons object-oriented coding methodologies like C++ were developed. By writing code in reusable classes, you can easily build upon existing classes to add necessary features. Code reuse allows you to depend on well-tested code throughout your application and feel confident that the code will work as expected. It is also very easy to share classes that you build with others who may need the same features.
Application frameworks can sometimes make your programs a bit larger than they might be if you were to create them completely from scratch. Since modern computers are usually equipped with substantial amounts of memory, and since the framework can be used as a DLL, it's fairly easy to justify the added bulk. PowerPlant and MFC are both very powerful tools that you should consider using.
Your best bet now is to focus on learning more about MFC and how it can assist you -- and you can make the decision on how much you want done for you, or how much you want to dig in and do yourself.
How to Install and Use MFC Interface Tools
The MFC interface tools (including Dialog Editor and Image Editor) are located on the CodeWarrior CD. Unfortunately, they're not part of the CodeWarrior Installation, so after the initial CodeWarrior installation, you won't find them on your hard drive. In order to install the tools, simply perform the following steps:
You need to locate the file named iTOOLS.Exe. This is the installation program you will use to install the MFC Interface Tools. The iTools.Exe file should be located at C:\Program Files\Metrowerks\CodeWarrior\BinSDK\bin\. If you can't find the file there, then go to the CodeWarrior CD. The file iTOOLS.Exe can be located in the folder Extras\SKDs\Win32\Microsoft Win32 SDK Tools\.
Note: The Installation program (iTOOLS.Exe) will ask you where you want to save the MFC Interface Tools. Unfortunately, there is no Browse button to locate a folder location, so you will have to manually type in a directory path. If you are unsure of the exact path for where you want to save the tools, locate a file that resides in the folder where you want to save the tools and retrieve the complete path for this folder by examining the file's properties (right-click on the file and select Properties). Here's a quick tip: you can actually copy the path from this Properties box and then paste the path when prompted by the installation program.
The tools will be located in the \bin\ folder of the target installation folder. The \bin\ folder is where you'll find the Dialog Editor (DlgEdit.Exe), Image Editor (ImagEdit.Exe), and other useful tools.
If you're developing applications for Windows 95 or Windows NT, the necessary tools are available in the folders \bin\win95 and \bin\winnt, respectively.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。