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 \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 \
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 \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 \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.