Tips for compiling pure C programs using Visual Studio. NET

Source: Internet
Author: User
Tips for compiling pure C programs using Visual Studio. NET

Author: Lover_p

The so-called pure C program refers to the library function used by the source code of the Program (including executable programs or libraries) in strict compliance with the c89 standard, either the standard library functions supported by the c89 standard, or the library functions written by a third party in pure C program.

The above is a non-standard and recursive definition of "pure C program.

All those who use Visual Studio know that the visual C ++ integrated development environment as Visual Studio does not provide a C language compiler. We can only use the C ++ compiler to compile the C program. Although Eckel Bruce believes that using a C ++ compiler to compile a C program can discover potential errors in the Program (see think in C ++), however, I insist that this is not a good solution: this will only allow everyone to write C Programs that use a large number of C ++ features. This type of program is not desirable. Bjnare stroustrup also believes that C ++ should be a completely new programming language independent of C. However, I think, C should be a language that is independent of C ++ and old-tree evergreen. Therefore, we need a development environment and compiler that can write pure C Programs.

The C ++ compiler in Visual Studio. NET provides us with this function! The C ++ compiler in Visual Studio. net not only greatly enhances the support for the c ++ standard, but also supports compiling pure C Programs through some configurations! In this article, I will give a brief introduction to the preparation method, hoping to help those who like pure C programming. I also hope that new C language programmers can write beautiful C Programs.

Configuration of integrated development environment

First, select the Win32 console application for the project creation purpose, but be sure to select the "Empty Project" option. This prevents the Visual Studio wizard from generating unnecessary source files (CODE) and setting unnecessary compiler options for us. For specific operation methods, see:

Select "file"> "new"> "project ":

In the "new project" dialog box, select "Visual C ++ Project" in the "project type" Li table box, and select the "Win32 Control Project" template in the "template" list box. Do not forget to enter the project name in the "name" text box. Here I name it "purec ":

Click "OK". The "Win32 Application Wizard" dialog box is displayed. Do not rush to "OK ":

Initially, when I saw this dialog box, I did not really pay attention to the "application settings" option (maybe I am too stupid-_-B ). Therefore, we need:

Click the "application settings" tab and select "Empty Projects" in "Additional Options ":

This step is the key to the problem. If "Empty Project" is not selected, Visual Studio. net Wizard will add some source files (such as "stdafx. h "," stdafx. c), and some compiler settings. Although these settings are very useful for compiling Windows API programs, they are very unfavorable for compiling pure C Programs. Therefore, it is necessary to select "Empty Project ".

After clicking "OK", we get an empty project and observe the "solution Resource Manager" list box. We found that, there is no unnecessary source file generated by the wizard for us. At this time, we need to manually add our source files to the project. Here we add a new source file "purec. c ". Right-click the "source file" folder chart in the "Solution Explorer" list box and choose "add"> "Add new item ":

In the "Add new project" dialog box that appears, select "C ++ File. Enter the name of the source file in the "name" text box. Note that the extension ". c" must be specified in the file name; otherwise, the IDE will automatically add the ". cpp" extension to the file:

Next, we need to set the properties of the project. Right-click the project in Solution Explorer ("purec" Here) and select "properties" in the pop-up menu ". The "property page" dialog box is displayed. Expand the "C/C ++" node in the Tree View on the left and select the "advanced" node. In the list box on the right, change the "compile as" list item to "compile as C code (/TC )":

Click OK.

Console option Configuration

This question is a bit ...... That ...... . My intuitive feeling is that the people who use the console command line to compile the program should all be prawns (even if it is not prawns, it must also be prawns). I should be familiar with the command line options. In short, you only need to add the/TC option in the command line to compile a source file into pure C code.

Test

After such training, we have an environment for compiling pure C code. We have to use a program to verify it. Add the following code for our purec. C:

/*
* Model: purec. c
* Author: lover_p
* Date: 2004-6-3
*
* Test the pure C envirment in vs. net
*/

Struct {
Int I;
};

Int main (){
A;
A. I = 10;
}
/* End */

This code is completely correct for C ++ programs and can be compiled without errors or warnings. But this is not a "pure C program", because in the C standard, when the structure type is used as the variable type, the struct keyword must appear explicitly in the variable declaration statement. Therefore, compiling this code in our "pure C environment" will get n + error reports.

Therefore, we need to change the first line of the main () function to a declaration statement in the pure C form:

Struct a;

In this way, the re-compilation will be a 0 error 0 warning.

Summary

Finally, this "pure C environment" is only auxiliary. To learn how to compile a "pure C program", you must first carefully study the Standard C language. Don't expect this environment to teach you to write "pure C Programs". Just like in the above example, without a keyword, There will be so many errors in Visual Studio. NET 2003:

E:/workspace/C ++/purec. C (14): Error c2065: "A": Undeclared identifier
E:/workspace/C ++/purec. C (14): Error c2146: syntax error: Missing ";" (before identifier ")
E:/workspace/C ++/purec. C (14): Error c2144: syntax error: "<Unknown>" should be preceded by "<Unknown>"
E:/workspace/C ++/purec. C (14): Error c2144: syntax error: "<Unknown>" should be preceded by "<Unknown>"
E:/workspace/C ++/purec. C (14): Error c2143: syntax error: Missing ";" (before "identifier)
E:/workspace/C ++/purec. C (14): Error c2065: "A": Undeclared identifier
E:/workspace/C ++/purec. C (15): Error c2224: The left side of ". I" must have a structure/Union type.

If you are not familiar with standard C, you cannot start with so many errors. Therefore, language learning is very important, and the Environment is only an auxiliary function.

Finally, I hope you can learn the most advanced language.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.