1、CodeWarrior 使用教程第一课 认识 CodeWarriorTranslated from CodeWarriorU.COMCodeWarrior 能做些什么?当你知道自己能写更好的程序时,你一定不会再使用别人开发的应用程序。但是常常会发生这种情况,就是当你写了无数行代码后,却找不到使得整个程序出错的那一行代码,导致根本没法编译和链接整个程序。这可能更令人灰心丧气。本文将告诉你如何使用 CodeWarrior 这一工具解决上述问题。从现在开始,我们将集中精力学习如何在 CodeWarrior 中使用 C/C+ 进行编程。为了学习本课程,你必须已经能够比较熟练地使用上述两种语言之一。Co
2、deWarrior 也可以支持 Java 开发,但那是另一门课程的内容。本课程仅限于在 Windows 平台上使用 CodeWarrior 进行的开发。一旦你精通了 CodeWarrior 编程后,你可以试试在其它平台上使用 CodeWarrior。本文中讨论过的大部分内容都可以应用到开发 Mac 应用程序中。CodeWarrior 能够自动地检查代码中的明显错误,它通过一个集成的调试器和编辑器来扫描你的代码,以找到并减少明显的错误,然后编译并链接程序以便计算机能够理解并执行你的程序。你所使用过的每个应用程序都经过了使用象 CodeWorrior 这样的开发工具进行编码、编译、编辑、链接和调试
3、的过程。现在你在我们的指导下,自己也可以去做这些工作了。你可以使用 CodeWarrior 来编写你能够想象得到的任何一种类型的程序。如果你是一个初学者,你可以选择编写一个应用程序(比如一个可执行程序),比如象微软公司的文本编辑器 WordPad 这样的应用程序。应用程序可能是最容易编写的程序了,而那些庞大的商业软件,比如象 Adobe Photoshop,Microsoft Word以及 CodeWarrior 软件都是极其复杂的。其它类型的程序指的是控制面板(control panels),动态链接库(dynamic linked libraries,DLLs) 和插件(plug-ins)
4、。我们先来简单的讨论一下这些类型的程序。在 Windows 中,控制面板程序是一些(通常比较小的)存放在控制面板目录下的程序,可以在开始菜单的控制面板项中看到它们。控制面板实际上是设置系统功能的一个简单界面,比如可以在控制面板中完成诸如设置音量和鼠标移动速度这样一些参数。动态链接库 (DLLs) 提供了一些很有用的被系统和应用程序共享的函数。通常它们都是没有用户界面的。插件 (Plug-ins) 有点类似于 DLLs,插件也是可供其它软件使用的小程序,但插件只能被特定的软件使用。比如说,Photoshop 的插件允许程序员方便地扩展 Photoshop 的功能,而不需要去修改它的源代码。同样的
5、,许多 Web 浏览器也使用插件来增加功能,或者为了让你能够看到页面上的新的内容类型。实际上,有许多程序,包括 CodeWarrior,都使用这种插件结构。一旦你掌握了 CodeWarior 和你所使用的操作系统的编程接口时,你可以编写许多其它类型的程序。实际上,本课程也是你深入学习其它编程方法的基础课程。你已经找到了一个最优秀的地方开始你的编程之旅。好,现在请坐好,放松,去吸取这门课程的精华吧,你将踏上一条成为一个编程武士的道路!CodeWarrior 入门CodeWarrior 是一个复杂的应用程序,你必须花点时间来了解它的各种各样的组件和功能。第一课将讲述CodeWarrior IDE
6、的安装。我们将学习菜单、窗口和其它的一些方面。建议你最好一边学习本课程,一边学习使用 CodeWarrior 的集成开发环境。我们从运行 CodeWarrior 开始,如果你按照默认方式安装的CodeWarrior 软件,那么在 Windows 的桌面上将会有一个该软件的快捷方式图标。双击该图标启动CodeWarrior 的集成开发环境。如果在 Windows 桌面上没有这个快捷图标(这种情况经常发生) ,你可以在C:Program FilesMetrowerksCodeWarriorBin 中找到 IDE.exe 文件,然后运行它。工具条启动 CodeWarrior 后,你将会看到,在屏幕上
7、方的菜单下面有一个工具条。这个工具条(见图 1-1)包含了一些常用菜单项的快捷方式。在后面的章节中,你将学到如何设置这个工具条,甚至是整个 IDE 环境。现在,你应记住,这个工具条用于快捷使用 IDE 中的功能。图 1-1: CodeWarrior IDE 的工具条文件菜单CodeWarrior IDE 的菜单是按照标准方式设置的。其中的文件菜单用于处理和文件相关的一些操作,比如创建、打开、保存和打印等等。编辑菜单CodeWarrior 的编辑菜单和其它的 Windows 应用程序也很相像。其中包括了剪切、复制和粘贴等操作,以及其它一些使得程序员能够更方便地管理源码版面布局的选项。在编辑菜单中
8、还有一个 Preferences 项,我们在这一课的后面将提到它。查看菜单查看菜单(图 1-2)用于安排工具条和其它窗口在 CodeWarrior 环境中如何显示的选项。所谓的“其它窗口” 包括许多特殊的窗口,比如观察点(Watchpoints)窗口,表达式(Expressions)窗口,过程(Processes)窗口和全局变量(Global Variables)窗口等。图 1-2: 查看菜单中的工具条子菜单查找菜单查找菜单用于在单个文件或硬盘的目录中查找指定的代码。你可以使用它来方便地替换文本块或在你的代码中搜索指定的项目。即使是在小工程中使用这个工具也非常方便。工程菜单工程菜单中的工具用于
9、管理 CodeWarrior 工程。一个工程包括组成你正在编写的程序的所有的文件,包括头文件、源代码和库文件。工程窗口显示了所有这些文件的名称。我们将在第二课中深入讨论这部分内容。调试菜单这是在编制程序中最常用到的工具。我们将在第五课中详细介绍它的使用方法。窗口菜单用于在 CodeWarrior 环境管理窗口显示方式的菜单。帮助菜单通过帮助菜单可以到网上寻求关于 CodeWarrior 任何问题的在线解答。够酷的吧?了解 CodeWarrior 集成开发环境的设置CodeWarrior IDE 提供了许多设置以便让你定制你的工作环境。当你选择了编辑菜单中的 Preferences 项时,你将会
10、看到一个设置对话框(如图 1-3)。在该对话框中,有控制 CodeWarrior 编辑器、调试器和其它许多功能的界面和行为的选项。在这一节中,我们将学习这些设置的使用。图 1-3: 设置对话框你可以在自己的 CodeWarrior 中试试上述这些设置项。你可以先点击问号标志,然后点击你感兴趣的项目,就可以得到一个有关该项目的用途的简短介绍,也可以从帮助菜单中得到更详细的信息。通用设置 编译设置(Build Settings): 选择是否在执行编译之前保存已打开的源文件,以及有多少内存被用于编译工作; IDE 之外(IDE Extras): 几个独立的设置。比如指定 CodeWarrior 是否
11、使用一个第三方的文本编辑器因为集成的编辑器并不是很完美,这可以通过指定一个你惯用的编辑器来替代它; 插件设置(Plug-In Settings): 供插件开发商调试他们的插件; 隐藏文件夹(Shielded Folders): 在这里指定的文件夹在工程设计期间,或执行查找和比较操作期间,将要被忽略掉。如果在你的工程级有一个巨大的“数据”文件目录,而你又不想让这些文件降低CodeWarrior 的操作速度时,这个设置就很管用了; 资料树(Source Trees): 用于指定 CodeWarrior 在编译程序时用不着的目录。编辑器设置 编辑器设置(Editor Settings): 几个用于定
12、制编辑器显示、管理文本和窗口的设置项; 字体和制表符(Fonts and Tabs): 设置编辑器中的文本大小、字体、制表符和其它显示设置; 文本颜色(Text Colors): 用于指定特定语言元素(比如程序的注释)在编辑窗口中的显示的颜色。调试器设置 显示设置(Display Settings): 几个用于定制调试器显示的设置项; 视窗化(Windowing): 设定调试器如何管理它的窗口(比如隐藏所有打开的编辑器窗口); 全局设置(Global Settings): 几个用于定制调试器在全局层次如何工作的设置。比如当一个包含了程序调试信息的文件被打开时,是否启动这个程序; 远程连接(Re
13、mote Connections): 允许通过 TCP/IP 进行远程调试。这样,你就可以在地球的另一边调试你的软件了; Java 设置/Java 调试(Java Settings/Java Debugging): 允许你设置特定的 Java 编程语言选项。本课程不涉及这部分内容。RAD 工具 Layout 编辑器: 几个用于在 CodeWarrior 中定制快速应用程序开发工具的使用的设置。正如你所见到的,CodeWarrior 有许多设置项和选项。设置完后点击“Save”按钮保存你所作的修改,点击“Close”按钮将忽略你所作的所有修改。在进入下一节课之前,多练习几次。最后请确认“ 默认的
14、文本文件格式(Default Text File Format)”(在编辑器设置面板中) 保持为 DOS 设置。(待续)附原文:Lesson 1: Getting to Know CodeWarriorWhat Will CodeWarrior Do for Me?It can be frustrating to use other peoples applications when you know you can make something better. But it can also be frustrating when you have a zillion lines of co
15、de and cant find the one typo thats 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, well focus on writing programs in CodeWarrior using the C or C+ programming languages.
16、 This course assumes that youre 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,
17、 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 edito
18、r), 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 teac
19、h 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, Microsofts text editor, for example.
20、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. Lets talk about these types for
21、 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
22、s motion speed, for example.DLLs supply useful functions that are shared by the operating system and applications. They typically dont have a user interfacePlug-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
23、 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 t
24、he 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 wh
25、ich you will learn more in-depth methods of programming your computer. Youve chosen an excellent place to begin your journey, however. So sit back, relax, and absorb the lessons in this course, and youll be well on your way to becoming a CodeWarrior!An Introduction to the CodeWarrior ApplicationCode
26、Warrior 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 a
27、cronym gets thrown around a lot, and its short for Integrated Development Environment. ) Well 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 defau
28、lts 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 FilesMetrowerksCodeWarriorBinIDE.exe.ToolbarWhen
29、you launch the CodeWarrior IDE, youll 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. Youll learn how to customize this toolbar - and even the entire IDE - in a later lesson. For now, notice
30、 that this toolbar can be used to quickly access features within the IDE.Figure 1-1: The CodeWarrior IDE toolbar.File MenuThe 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
31、let you create, open, save, and print files.Edit MenuThe Edit menu will also seem familiar if you use other Windows applications. Youll 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
32、item in the Edit menu that well discuss later in this lesson.View MenuThe 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, Express
33、ions window, Processes window, and Global Variables window.Figure 1-2: The Toolbar submenu of the View Menu.Search MenuThe Search menus 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 sear
34、ch your source code for references to other source code. These tools can be very handy, even for small projects.Project MenuTools 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 co
35、de, and libraries. The Project window serves as a container for these files. Well discuss this in more depth in Lesson 2.Debug MenuWhen your source code doesnt do exactly what you want it to (or your computer crashes when you run your program), youll develop a close relationship with the items on th
36、e Debug menu. Well discuss debugging in Lesson 5.Window MenuThe Window menu gives you the basic options to manage cascading and tiling windows in the CodeWarrior environment.Help MenuLastly, use the Help menu to get online help with just about any item in CodeWarrior. Pretty cool setup, huh?Getting
37、to Know IDE PreferencesThe 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 wi
38、th the IDE Preferences dialog box (Figure 1-3). There, youll find options that control the appearance and behavior of the CodeWarrior editor, debugger, and lots more. In this section, well take a look at these preferences.Figure 1-3: The Preferences dialog box.As each item is described below, follow
39、 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 youre interested in. Youll see a terse description of the selected items purpose. More detailed information is also available from the Help
40、 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 dont belong anywhere else. Heres where you can have CodeWarrior interoperate with a third-party text edit
41、or. 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
42、 (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
43、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 brows
44、er 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).
45、 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 programs debugging information. Remote Connections: Allows debugging over TCP/IP. You can debug software on a computer on the other s
46、ide 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 setting
47、s and options that can be set. Clicking the Save button will save any changes youve made. Clicking the Close box for the Preferences window will cause CodeWarrior to ignore any changes youve 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.