27)

Time for a more advanced compiler

  Introduction  
Alas, the time has come to move from the old trusty Miracle C compiler to something a bit more substantial. Miracle C is perfect for learning 'C' programming, but it has its limitations. For example, you can only create single source file projects, and also only console applications ( DOS type applications ). By now though, you should have a feel for C programming and know whether you want to continue onwards or not.

I believe the majority of professional Windows programmers use Microsoft Visual C, although others like Borland and Watcom C are also used. The problem with Visual C though, is that it's not free, and not really that cheap either. On many of the later C and C++ programming books that have CD's with them, there is normally a version of Visual C lite on the CD. I'm not sure whether or not it's available as a free download from the Internet though. I think the Borland and Watcom compilers do have a large free download available on the net, and you may consider downloading and using them. Bare in mind though that, probably all the features of Visual C are contained within them, they may be called different things, and accessed through different menu options though. There is also another popular free compiler on the net, Bloodshed's Dev C++, which seems to be growing in popularity.

All the programs from now on though will be written with Visual C, and may require slight changes to work with other compilers. I don't intend on focusing on all compiler setups, for two reasons, first, I don't know them all, and second, it would probably take a lot more text to do so. With Dev C++ for example, there is quite a bit of documentation around on the net which explains how to set up DirectX for it.

 

Projects
While there are probably people who would disagree with using Visual C, instead of other compilers, this does have the one advantage, that it is made by Microsoft, who also produce the DirectX libraries, so in theory Microsoft would have done all the compatibility stuff already ( in theory :) )

Whichever compiler you are using though, will need to allow multiple source files to be included in a single project. So far the programs have all consisted of a single source code file ( eg prog1.c, prog2.c and so on.. ). You may have heard already that the source code for many programs can run into many 1,000's, 10,000's, 100,000's or even millions of lines long. Programs over 100,000 lines long are not uncommon by any means. If we were using a compiler like Miracle C, this would make for a huge amount of text all on the same screen ! Searching through this amount of code to find the bits you're working on would be a real nightmare, but there is a solution - projects

Projects can be thought of as collections of source code text files which can all be linked together to form an entire program. It's very similar in analogy to functions which were covered on page15. In the first few programs up to page 15, the entire programs that were written were contained in the main() function, which meant potentially, this function could have become very large and unstructured. Functions allowed programs to be split up into seperate parts, which made writting some of the other programs a lot easier and definately a lot more structured. Projects do the same, not with functions though, but with source code files, they enable programmers to write large programs in small chunks, in a structured and manageable fashion. This explanation probably makes them sound a lot more complicated than what they are though, so don't worry if it sounds confusing just yet.

 

Creating a new Visual C project
The first thing that is advisable to do, is to create a folder on your hard disk drive to hold all of your programs. I'll assume this is on the C drive and is called progs, but the drive and naming is entirely up to you. You can use this folder to hold every program you write, each with a seperate sub-folder inside it.

Now start Visual C ( version 5 or 6 ) and select File->New. You should see a dialog box appear, with the following TAB's along the top :Files, Projects, Workspaces, Other documents.

With the Projects tab active, look down the list in the white box below, and click the Win32 Console Application entry to highlight it. Windows console applications are like the programs that have been written so far, using a console ( DOS like ) output, they are not DOS programs though, they are still windows 32 bit programs. The only other option these pages will use is the Win32 Application, this will be when the DirectX stuff is reached though.

On the right hand side of the dialog, there is a text box labelled Project name and below that another text box labelled Location: In the location box remove the text that's already there and enter c:\progs ( or whatever name and drive you created the folder as above ).

In the Project name box, enter prog1 ( you'll notice the contents in the location box change as you type, to, c:\progs\prog1 ( or whatever folder and drive you specified earlier, again ).

What you are doing here, is telling Visual C the following information

  • What type of application you are creating ( Win32 Console Application )
  • The name of your project ( prog1 )
  • And finally, the location on your drive where you want to store the program ( c:\progs\prog1 )

After this click the OK button to continue.

What happens from here depends on the version of Visual C you are using ( 5 or 6 ), follow below for which ever version you have, they will both arrive at the same place in the end.

Version 5 Version 6


When the next screen appears, there will be a white "pane" on the left with the text prog1 classes displayed in it. At the bottom of this pane, there will be three options : ClassView, FileView and InfoView. Click on the FileView option and you should see that the prog1 classes has now changed to prog1 files.

Position the mouse over the prog1 files and click the right mouse button. Select New Folder... from the drop down menu, and when the next dialog appears type in the Name of the new folder text box, Source Files. You can leave the File extensions blank if you like. Then click the OK button.

You will notice a + will have appeared on the left of prog1 files. Click this and it will reveal a newly created folder called, guess what, Source Files :).

Create another folder from prog1 files and this time name it Header Files


When prompted "What kind of console application do you want to create ?", choose An empty project, and click Finish. A dialog will appear with New project information, just click OK to clear it and continue.

When the next screen appears, there will be a white "pane" on the left with the text prog1 classes displayed in it. At the bottom of this pane, there will be three options : ClassView, FileView and InfoView. Click on the FileView option and you should see that the prog1 classes has now changed to prog1 files. Click the + by the prog1 files and you should now see three folders listed below it.

Version 6, automatically creates three Project folders for you called Source Files, Header Files, and Resource Files

After you've followed the process above for the version of Visual C you have, then you should end up with folders called Source Files and Header Files under the prog1 files in the left pane. If you are using version 6, then you'll also have a Resource Files folder, we don't need this and you can delete it if you like. To delete it, highlight it, by clicking on it with the left mouse button and press the delete key on the keyboard. The header files folder isn't needed either for this first app, but it does no harm in leaving them.

Note : That the folders created or listed are physical folders on your hard disk drive, they are project folders. They can be named anything you like and you can add more as well.They are nothing more than convient virtual places to store you program code files in a neat organised way

 

Creating a source file and entering the source code
The folders that have been created allow a place to store the source files for the project. Remember, at the top of this page, that a project is a collection of source code files. This doesn't mean more than one, it means one or more, in other words, a project can contain just one source code file, the same as what has already been done with Miracle C.

This first program with Visual C will only contain a single source file, as the purpose of the rest of this page is to show how you compile and run programs with Visual C. To do that though, the code has to be entered :

With the right mouse button click on the Source Files folder, and from the drop down menu that appears, select Add files to folder... As we haven't got any source code program files yet, enter prog1.c in the filename text box of the dialog that appears and click OK. You will get a messagebox appear, informing you that the file does not exist and asking if you want to add a reference to it anyway, select Yes.

Your Source Files folder now should have a + by it, indicating that there is at least one file in there. Click the + to open the folder and you should see listed prog1.c. Double click on the prog1.c file to open it, you will get a message saying that the file doesn't exist, and asking if you want to create a new one, select Yes.

At last, you now get somewhere on the screen to start typing program code in :-) In the text window that appears enter the following program ( it shouldn't need explaining ).

#include <stdio.h>
#include <conio.h>

void main(void)
{
printf("Hello World");
getch();
}

After entering this program, you are ready to compile and run it. It may seem a bit longer to get to this point than with Miracle C, but it's not too bad after you do it a few times.

Note : I've had to put the getch(); line at the end of the program here to stop the console display disappearing after the program has run. Try it without the getch(); to see what I mean.

 

Compiling and running the program
There are a few ways to compile and run a program in Visual C, I prefer to use the F7 ( compile )and F5 ( compile and run ) keys on the keyboard, but if you prefer using menus, then Build drop down menu has the options there.

If you execute ( run ) the program without compiling it after you've made some changes, Visual C will compile it first and then run it.

If there are any errors in the code, the horizontal pane at the bottom of the Visual C window will highlight these. You can double click on any error or warning within this pane and it will take you to the line that is causing the compilation error.

Hopefully though, everytime you compile your programs, it will come up as 0 error(s) and 0 warning(s). I doubt it though :-)

 

Summary
It seems like quite a lot on this page, but at the end of it, there's only a small "hello world" program. Using compilers that have the ability to have more than one source code file definately complicates things a little, but it is one of those situations where the benefits far outway the extra bit of effort.

After reading this page, you should have a few snippets of info about what projects are, you won't understand fully yet because there hasn't been a program yet than needs more than one source file. You should also know how to create a project in Visual C ( either versions 5 or 6 ), create project folders to hold source code files, and add files to those folders. Finally, you should know how to compile and run a project within Visual C.

If you are using Dev C++, then the process described on this page about creating a new project, is very similar, I don't think there are project folders in Dev C++ though, but that's probably the only real difference.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001