Tips for compiling pure C programs using Visual Studio. NET

Source: Internet
Author: User

Address: http://www.nnllok.cn/myBlog/archives/2005/1180.html

 

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 for Visual Studio does not provide a C language compiler. We can only compile the C program using the C ++ compiler. 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. 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

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.

  • Previous: compilation environment of Assembler
  • Next article: Pb error. Number, explanation of various errors in Chinese
※Re: the prompt "mountain315" for compiling pure C programs using Visual Studio. NET was posted on the personal homepage 22:19:00 | reference | return | Delete | Reply

Similar to writing a console program, if you want to view the results, you need to use menu debugging-> start execution (not debugging), that is, CTRL + F5, in this way, the CMD window can be left to view the results. If F5 is used, the CMD window will be closed when it is flashed.

In addition, the advantage of this method is that it is much faster than opening TC20 on win2000server, but every time you remember to remove the c file that was run in the previous step from the solution, can compile and execute another c file, because each c file has a main, but in a project, only one main

Also, during compilation, the system will report "these project settings are outdated", which is useless. If you select "yes", the system will generate

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.