DIY a C + + compiler

Source: Internet
Author: User

I. Principle

1.1 Notepad programming

We use Notepad to create a new file, the file suffix is changed to. c, for example, the filename is called "1.c", the file is written in the simplest C or C + + code,

 1  #include <stdio.h>2  # Include<stdlib.h>3   main ()  4  { 5  printf (  Notepad programming   "  6  system ( " pause   "  7  return  0   8 } 

How do I compile and run the case without using VS or codeblock to open it?

1.2 compile with Codeblock's own mingw.

Suppose your computer has a codeblock with MinGW installed, and if it is not available to download HTTP://WWW.CODEBLOCKS.ORG/DOWNLOADS/26

Click Start----> Run in the lower left corner of Windows, enter cmd into the command line, enter the Codeblock MinGW folder on the command line, run the Windows batch file Mingwvars.bat. The effect is as follows:

This effect indicates that the Mingwvars.bat batch file is running.

We then enter the folder where the file "1.c" is to be compiled, and enter "GCC file name", "gcc 1.c", so 1.c is compiled.

At this point, we will find that in the 1.c folder will generate an executable named "A.exe", we enter "A.exe" to execute it, we can execute the program written in 1.c:

Notepad programming A few words, is the result of printf output.

1.3 Compiled with the compiler that comes with Visual Studio.

Assuming that you have installed Visual Studio (my version is 2012), locate the Visual Studio folder in the Start menu of Windows-- open Developer Tools --

From the command line into the 1.c folder--input CL 1.c--> you will be able to see in the 1.c folder generated 1.exe and 1.obj two files--input 1.exe run it

We can see the effect of the execution.

As we can see from the two examples above,. C files can be executed by invoking the compiler's compiler command from the command line, so we just need to invoke the compiler directive in the program, so we can do a DIY one of our own C + + IED

Two. C + + IDE development process

2.1 Establishment of the project

We create a new MFC Application project under Visual Studio, select "Multiple Documents", tick "tabbed documents", project type Select "Office", visual style and color select "Office 2007 Light Green" (you can choose your favorite color),

File extension Input "C", and check the file Type preview processing support, thumbnail support, search processing support

The user interface feature is selected using the Ribbon, and all options are checked,

All advanced features are checked

Base class Selection CRichEditView

Click Done to build a good project.

2.2 Interface Editing

Find the RIBBON in the project's Resource view, double-click Idr_ribbon, Edit tab, we drag a category into the tab from the toolbar, the Caption property of this category changes to "compile", and drag a panel to this tab, so this category has two panels, Modify their caption properties separately ask GCC and vc--> to drag two buttons into two panels, caption set to "Run".

So the interface is done.

2.3 Set the text of our compiler to plain text

If, at this time to run the program, with our compiler and write a text and save, and then open the text in Notepad, you can find a lot of extra text, these are used to record text style information such as text, this is not compiled (do not believe you can try, I will not), So we're going to set the compiler's saved text to ask for plain text.

If you don't understand it, just follow it.

Open MyIDEDoc.cpp in the project, find void Cmyidedoc::serialize (carchive& ar) This function, we can see the bottom of a line of comments

TODO: If serialized as text, set cricheditdoc::m_brtf = FALSE

We just need to move the code to the next line and add ";" Can:

1 voidCmyidedoc::serialize (carchive&ar)2 {3     if(AR. IsStoring ())4     {5         //TODO: Add storage code here6     }7     Else8     {9         //TODO: Add the load code hereTen     } One  A     //calling the base class CRichEditDoc will enable -     //The serialization of the COleClientItem object for the container document.  -     //TODO: If you are serializing as text, set the theCricheditdoc::m_brtf =FALSE; - cricheditdoc::serialize (AR); -}

At this point, the plain text is saved.

2.4 Implementing a simple MinGW compilation

Our core technology is to run the MinGW batch file, which drives the compilation

First we open the Codeblock MinGW folder, in order not to affect the compilation of Codeblock itself, we copy a new Mingwvars.bat and name it mingwvars_a.bat, open it with Notepad, and add the following command:

1 @echo. 2  for using  from%~dp0. 3 @set path=%~dp0bin;%path%45C:6CD C:\Users\Administrator\ Desktop72. C8 a.exe

This means that you go to the folder where you want to compile the file, compile the file named "2.c", and then execute it.

Then we add the event handler

In the VS project, right-click on the GCC "Run" button, click "Add Event handler", select "Cmianframe" in the class list and click Add Edit

Then we add the code to the button's event handler:

1 void Cmainframe::onbutton2 () 2 {3     // TODO: Add Command handler code here 4     System ("\" d:\\codeblocks\\mingw\\mingwvars-a.bat\ ""); 5 }

Change the path to your MinGW folder and the file name to your custom batch file name.

Then we run the project--Enter the interface--and select the upper left corner of the compilation--in the File edit box to enter some code, save to the previous set of batch file to compile the folder, save as "2.c", you can also take another name, change batch file can be, this is a simple example , you can also compile files of any file name by writing to the file.

-click GCC Run

We saw the execution.

2.5 Implementing a simple VC compiler

First we go to the VC folder under the VS installation directory folder, as we call MinGW, we find the VC batch file Vcvarsall.bat, copy it, rename it to vcvarsall-a.bat--> edit-in: x86

Add code between call "%~dp0bin\vcvars32.bat" and Goto:setvisualstudioversion:

 :x86  if  not exist  " %~dp0bin\vcvars32.bat  "  goto   Missingcall  "  %~dp0bin\vcvars32.bat   c:cd C:\Users\ ADMINISTRATOR\DESKTOPCL  2  .c  Span style= "color: #800080;" >2  .exe  goto : Setvisualstudioversion 

Save, meaning, enter the C:\Users\Administrator\Desktop folder is the desktop, and then compile 2.c this file, and then execute the compiled 2.exe file.

Next we add an event handler under the "Run" button on the VC of the VS project (the same way):

1 void Cmainframe::onbutton3 () 2 {3     // TODO: Add Command handler code here 4     System ("\" F:\\microsoft Visual Studio 11.0\\vc\\vcvarsall-a.bat\ ""); 5 6 }

Run the project, add 2.c folders, click on the run under vs layout, get the following effect:

In the 2.c folder we will find 2.obj and 2.exe files.

This makes it easy to implement an IDE

DIY a C + + compiler

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.