CST8152 – Compilers Creating a C Language Console Project with Microsoft™ Visual Studio C++ 5.0/6.0

Creating a Console Project with MS-Visual C++ 5.0/6.0

MS-Visual C++ IDE in its many incarnations is the “de-facto” development tool for developing Windows applications. It supports 32-bit applications only. Our compiler will be 16-bit application, but you can still use MS-Visual C++ to develop it.

To create a Console Project with MS Visual C++ 6.0 IDE follow the steps (rather clicks ☺) outlined bellow:

S1. Launch the MS-Visual C++ 6.0 compiler (IDE)

Integrated Development Environment (IDE) window will appears with an empty gray screen.

S2. Create a Project

2. Select File, New from the main menu bar. The New dialog window will appear on your screen. It contains several tab-lists. The default tab is the Projects tab, which displays the type of projects you can create. Select Win 32 Console Application as the project’s type. Type the name of your project into the Project name text box. For example: cdtypes . Enter the location (the folder or directory) you want to store your project – you can type it or use … button to select it. For example: c:\compiler or better n:\compiler . Make sure that the check box in front of Win32 in the Platform text field is checked and the radio button Create new workspace is ”pressed” (contains a dot). When you click the OK button a new dialog window will appear: Win32 Console Application – Step 1 of 1 The default selection is An empty project . If not, select it. Click Finish and the New Project Information dialog window will pop up. The New Project Information text box must contain only the following two lines:

+ Empty console application. + No files will be created or added to the project.

If everything seems, click OK . This completes the basic project creation. A new folder (directory) called cdtypes (the project name) will be automatically created on your hard drive inside the folder location that you have indicated as the Location: entry. This folder is called “project workspace” in MC Visual C++ IDE. The workspace folder will contain the project definition files, your source files, and another folder (directory) named Debug . The definition of the project is stored in a file with the extension .dsp (or .mak in some previous versions).

CST8152 – Compilers Page 1 of 5

Additionally, the new workspace will automatically be opened in MS Visual C++ IDE. You will see that two tabs have been added to project Workspace window: Class View and File View . You are to use the C language to write your compiler and since C is a procedural language, the Class View is irrelevant. Click the File View tab and should see the following

It shows your workspace - project name (cdtypes) the root of the project files ( cdtypes files ). Click the + to expand the tree. You will see tree folders: Source Files , Header Files , and Resource Files . All those folders are empty at this point. You can remove (select and press Delete) the Resource Files folder since it is not need for console type applications.

S3. Specify the Project Options

You can set a range of options for a project through Project, Settings … menu. These options determine how your source code is to be processed during the compile and link stages. When you create a new project workspace, MS Visual C++ IDE will automatically create configurations for producing two versions of your application. One includes information, which will help you to debug your program, and is called Debug version. The other, called Release version, has no debug information included and has the code optimization options for the compiler turned on. You can choose which configuration of your program to work with by selecting Build, Set Active Configuration . During the development and test phases of your application, you will be working with the debug configuration. Once your application has been tested and proven to be working correctly, you would typically rebuild the program as a release version.

3. Select (highlight) the Workspace or project files line in the Workspace window (for example: Workspace ‘cdtypes’: or cdtypes files ). Select Projects, Settings . The Project settings dialog window will appear. The default tab is General . The Microsoft Foundation Classes : list field must contain Not Using MFC . If not set it to that option.

Click the C/C++ tab and make the following changes to the default settings:

Category: General Warning Level: Level 3 Change to Level 4 .

Category: Customize

CST8152 – Compilers Page 2 of 5

Click the Disable language extensions checkbox (this will force the compiler to be ANSI C compliant).

Category: Precompiled Headers Click the Not using precompiled headers radio button.

Category: Listing Files Listing file type: No Listing Do not change anything but explore the options in the list box.

No other default project settings need to be changed but you can click the other tabs and see the different settings they define. For example, the Link tab allows you to change the name of your executable target (in your case cdtypes.exe).

Click OK to save the project settings.

4. Now you can save your project settings. Select File, Save Workspace . To open an existing project select File, Open Workspace , highlight the file with the extension .dsw , and click the Open button.

S4. Add a Source File to the Project

5. To add a new file, select File, New . The New dialog window will show up with the File tab as a default. Select C++ Source File . If you want to add a new header file you should select C/C++ Header File . Click Add to project checkbox. Type the name (for example, cdtypes.c ) in the File name: text filed. The extension of the file MUST be .c in order to force MS Visual C++ IDE to switch to the C language compiler. Check the contents of the Location: field and click the OK button. You should see your c file as shown in the figure below:

6. To add an existing file ( .c or .h ), select Project, Add To Project, Files and use the Insert Files into Project dialog window to add the file. Alternatively, in the workspace window right-click the project name and select the Add Files to Project option or right-click the corresponding folder, for example Source , and use Add Files to Folder option. You should see your c file as shown in the figure above.

7. To remove an existing file from the project: in the workspace window select File view tab, click the + in front of the project files (for example cdtypes files ), click + in front of CST8152 – Compilers Page 3 of 5

the Source file folder icon, select (highlight) the file you want to remove end press the Delete key. This action will remove the file from the workspace - project but will not physically delete the file.

Save the project (see step S3, item 4).

S5. Write or Edit the Source Files

8. When you add a new file to the project the editor window will open automatically and you should see the file name displayed in the editor window title bar. You can begin writing your program. If the already exists and it has been added to the project, you can open it for modifications. To do so, the file in the project workspace and double click on it, or use File, Open .

S6. Compile and Build your Application

9. To compile and/or link your program, you can use the Build option in the main menu bar (to compile, the .c source code file window must be active), the BuildMiniBar floating tool bar (Hint: use the “bubble” help boxes to find out the functions assigned to the buttons),

or you can click with the right button on the corresponding file in the workspace window. You should first compile your .c files one by one and after all errors and warnings are cleared, build the project.

S7. Run the Application (Program)

19. To run the program, you should open a Command prompt or DOS window. Do not use the ! button. There are many ways to open a Command Prompt window: you can choose Start, Programs, Accessories, Command Prompt or alternatively, Start, Run, type cmd or command , and click OK; in our labs you can also click on the Windows Command Processor icon located on the right of the Start button on the Task bar. Once you have the command prompt window opened, use the cd command to navigate to your project Debug directory type the name of your executable (for example, cdtypes ) and press Enter. If your application requires command line arguments (and the compiler project will require some) you must type them separated with a space after the program name.

Possible problems

As indicated above MS-C++ generates only 32-bit applications (the size of the integer type is 4 bytes). We will work with a16-bit application. To avoid possible problems you must test your application with Borland 5.02 before submitting it.

CST8152 – Compilers Page 4 of 5

MS-C++ does not support alloc.h. Use instead malloc.h or stdlib.h. You need these header files for the functions: alloc(), realloc(), and free().

S^R, 2008

CST8152 – Compilers Page 5 of 5