CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2005 The process of creating a project with Microsoft Visual Studio 2005.Net is similar to the process in Visual Studio 2003.Net, but Microsoft added a few more options and changed the user interface at some places. To create a C Language Console Project, you should follow the steps outlined below. 1. Getting Started Launch Microsoft Visual Studio.Net 2005. After the flash screen disappears you should be presented with the Microsoft Development Environment [design] window. Depending on the installation configuration the window can contain different views. By default it should display the Start Page view. If the Start Page is missing, you can invoke it from the main menu bar opt for Window, Show Start Page or View, Other, Start Page. The screen could look a bit different from the one shown in Figure 1 if you have already created some projects they will be listed in the Projects tab list. You can also see some other views displayed. Figure 1. Microsoft Development Environment Start Page view displayed. CST8152 Compilers Page 1 of 11
2. Creating a project - Selecting a Project Type Selectr File, New, Project from the main menu bar. The following New Project window should appear as shown in Figure 2. Select Visual C++ in the left pane (Project Types) and Win32 Console Application in the right pane (Templates). Enter the name of the project in the Name text filed, for example, cdtypes. Choose the location of the project, for example, C:\Compiler\Lab0 or better N:\Compiler\Lab0. Uncheck Create directory for solution check box. Click OK. The Win32 Application Wizard should appear as shown in Figure 3. Figure 2. Selecting the Project Type and Naming the Project. 3. Creating a project - Selecting an Application Type In the Win32 Application Wizard click the Application Settings on the left. The Application Wizard window should change as shown in Figure 3a. CST8152 Compilers Page 2 of 11
Figure 3. Selecting Console Application. Make sure Console application is selected as an Application type. Uncheck the Precompiled header and check Empty project under Additional options as shown in Figure 3a. Click Finish. The Solution Explorer view should appear on the right. If not, choose View, Solution Explorer from the main menu. Figure 3a. Selecting Console Application. CST8152 Compilers Page 3 of 11
In the Solution Explorer view window you should see your project node tree. It contains three folders: Source Files, Header Files, and Resource Files as shown in Figure 4.If you try to open some of the folders, you will find that they are empty. You can remove the Resource Files node. Select Resource Files, right-click, select Remove and confirm with OK. You can also close the Start Page. Figure 4. An Empty Project If you do not like the name of the project, you can change it. Simply select Solution, right-click, select Rename and type the new name of the project. Then, select the cdtypes project node, right-click, select Rename and type the new name. 4. Setting the Project Properties Before setting the project properties a C/C++ program file must be added first. Otherwise C/C++ option will not be visible in the right pane of the Project Properties window. Section 5 explains in details how to add a new or existing file. To add a new file you should right click on the Source Files folder (or on the project name cdtypes) and select Add, Add New Item. Select Code in the right pane, and C++ File in the left pane. Type the name of the file in the Name: text field. The file extension must be.c. Click the Add button at the bottom. The program editor view should appear on the right. The next step is to set the project properties. Select Project, Properties from the main menu (or right-click on cdtypes project name and select Properties. The Property Pages window should appear as shown in Figure 5. On the left side you will see the Configuration Properties tree. The currently selected property is highlighted in grey. On the right side you can see the default settings. When the window opens the CST8152 Compilers Page 4 of 11
General property should be selected by default (see Figure 5.). Clicking on the + icons you can expand the tree node and choose and configure different setting of your project as shown below with the C/C++ node expanded. Figure 5. The Property Pages Window. To change a configuration parameter (or property) you must select the corresponding line, click on the pull-down list icon to reveal the list of allowed options, and select the appropriate one as shown in Figure 6. Figure 6 show how to change the Character Set property to Not Set. Once you are done with the page, you should click Apply and move to the next page. CST8152 Compilers Page 5 of 11
Figure 6. Configuration Properties, General changing a property. 4.1. Changing the C/C++ Project Properties (settings) Click on the + icon in front of the C/C++ node. The C/C++ property nodes will appear as shown in Figure 7. Select the General property page. In Figure 7 the default Warning Level property - Level 3 (/W3) is changed to Level 4 (/W4). No changes are required in Optimization, Preprocessor, Code Generation, Precompiled Headers, Output Files, and Browse Information. You can peruse the property pages for further reference. CST8152 Compilers Page 6 of 11
Figure 7. C/C++ Property Pages Warning level changed in General Go to Language, select Disable Language Extension and select Yes(/Za) as shown in Figure 8. This will force the compiler to be ANSI C compliant, and if your program contains any non ANSI C language statements (extensions) the compiler will generate syntax errors or warnings. For example, if you have C++ type single line comments (//), they will generate warnings like the one shown bellow CST8152 Compilers Page 7 of 11
Figure 8. Making the compiler ANSI compliant. Move to the Advanced page. Change Compile As from Compile as C++ Code (/TP) to Compile as C Code (/TC) as shown in Figure 9. Figure 9. Forcing the C++ compiler to work as a C language compiler. Take a look at the Command Line option. Do not change anything there. CST8152 Compilers Page 8 of 11
4.2. Changing the Linker Project Properties No any changes are required for the Linker Properties. In Linker, General you can change the name of the executable file which will be generated by the linker. Note: If you want to change the name of the project (output exe file) you must make the change in Linker, General, Output File using the <Edit > option. Explore the Linker properties. 4.3. Changing the Other Project Properties You can explore the rest of the property pages, but there is no need to change any of the default setting for this particular project. So click Apply and OK to finish with the project properties configuration. Select File, Save All to save the project settings. 5. Adding Source Code to your Solution (Project) To add a C program to your project, select Source Files in Solution Explorer pane, right-click, select Add. Now you have the option to add a new program file or an existing program file as shown in Figure 10. If you program or header file dos not exist, you can add a new item to your project. Select Add New Item and in the Add New Item window select Code folder icon on the left, select the C++ File or Header File on the right, type the name of your source file in the Name text filed as shown in Figure 12. Click Add and the file will be added to your project (Figure 11) and an empty program editor will appear on the screen. If it has not, double click the cdtypes.c program file name. You can type your program now. If you program has been already written, before performing the add operation, you should copy the file into the project directory. After selecting Add Existing Item the Add Existing Item window will appear and you can select the c-file from the list. Click Add and the file will be added as a subnode to your Source Files folder as shown in Figure 11. Now you can start working with your program. Figure 10. Adding a Source Code Item Figure 11. A Program is Added to the Project. CST8152 Compilers Page 9 of 11
Figure 12. Adding a New Source File. 5. Compiling a C program In Solution Explorer select the C file you want to be compiled, right-click and select Compile. The result of the compilation will be shown in the Output view. The Compile action creates an object file only, not an executable. Pay attention to the warnings. 6. Building a Project To build a project means to make a request to the IDE to compile all your project files (c and h) and to link the compilation results (the obj file) into an executable (.exe). To build a project, select the project node (for example, cdtypes) in Solution Explorer view, right-click, and select Build. You can use the Build option from the main menu for the build the project (or the solution).the results from the building process will be reported in the Output view. If the operation of building the project is successful, an executable file will be created in your project Debug directory. Pay attention to the warnings. 7. Running the Program You can run your program within the IDE, or as a stand alone program from the command prompt in a Command Prompt (DOS or console) window. CST8152 Compilers Page 10 of 11
To run the program within the IDE, select Debug, Start Without Debugging (Ctrl+F5). The result of the execution will be shown in an application window. If your program requires command line arguments, you must specify them in the Project properties, Configuration Properties, Debugging properties page. To run the program from the command prompt (which is the normal way of invoking a console application) you need to open a Command Prompt window. To open a Command Prompt window Start, Run, type cmd or command, and click OK. 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 the command line arguments and press Enter. Now it is time to close the Net and count the fish. Enjoy it. S^R, 2009 CST8152 Compilers Page 11 of 11