CodeWarrior 使用教程 联系客服

发布时间 : 星期日 文章CodeWarrior 使用教程更新完毕开始阅读

是 Hello World 工程的调试版本。在下拉菜单的右边有几个图标,它们的存在允许你不必使用菜单命令就可以方便地编译、链接和运行这个工程。

注意: 在某些版本的 CodeWarrior 中,链接标签被称为段(segments)标签。有些人可能在某些 X86 的处理器上编程时使用的是分段(segmented)代码。但使用 Windows 版的 CodeWarrior 工具时就不是这样,这是产生的 X86 代码使用的是\扁平的\,或者叫做非分段的(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 应用程序的运行结果

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(\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 \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 \Windows!\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.