DIY is a C/C ++ compiler, and diy is a c compiler.
I. Principles
1.1 notepad Programming
We use NotePad to create a new file with the file suffix changed to. c. For example, the file name is "1.c" and the simplest C or C ++ code is written in the file,
1 # include <stdio. h> 2 # include <stdlib. h> 3 int main () 4 {5 printf ("Notepad programming"); 6 system ("pause"); 7 return 0; 8}
How can I compile and run it without using VS or CodeBlock to open it?
1.2 use the built-in MinGW of CodeBlock for compilation.
Assume that your computer has installed CodeBlock with MinGW, if not, you can download the http://www.codeblocks.org/downloads/26
Click Start in the lower left corner of windows ----> Run, Enter cmd to enter the command line, enter the CodeBlock MinGW folder in the command line, and run the windows batch processing file mingwvars. bat. Shows the effect:
This effect indicates that the mingwvars. bat batch file is running.
Enter the folder where the "1. c" file to be compiled is located, and enter "gcc file name", that is, "gcc 1. c", so that 1. c is compiled.
Then, we will find that an executable file named “a.exe is generated under the folder of 1.C. We enter "a.exe" to execute it and we will be able to execute the program written in 1. c:
Notepad programming is the result of printf output.
1.3 compile with the compiler that comes with Visual Studio.
Suppose that you have installed Visual Studio (my version is 2012), find the Visual Studio folder in the windows Start Menu --> open "Developer Tools" -->
Enter the folder where 1.c is located through the command line --> enter cl 1.c --> you can see the two files 1.exe and 1. obj under the 1.c133 folder --> input 1.exe to run it
We can see the execution results.
We can see from the two examples above that ,. the c file can call the compiler's compilation command through the command line for execution. Therefore, we only need to call the compiler command in the program to DIY a C/C ++ IED of our own.
II. C/C ++ IDE development process
2.1 build a project
Create an MFC Application project under Visual Studio, select "multiple documents", check "option card Document", and select "Office" for the project type. select "office 2007 light green" for visual style and color (you can select your preferred color ),
Enter "c" for the file extension, and select preview support, thumbnail support, and search support for the file type.
Use the functional area of the user interface and check all options,
Select all advanced functions
Select CRichEditView as the base class
Click Finish to create a project.
2.2 UI editing
In the project resource view, find Ribbon, double-click IDR_RIBBON, and edit the tab. we drag a category from the toolbar to the tab. The Caption attribute of this category is changed to "compile". and drag a panel to this tab, so that this category has two panels. Modify the Caption attribute to ask GCC and VC --> and drag two buttons to the two panels respectively, set Caption to "run".
This completes the interface.
2.3 set the text of our compiler to plain text
If we run the program at this time, use our compiler to write and save a piece of text, and then open the text in notepad, we can find a lot of Redundant text, these are texts used to record text styles and other information, which cannot be compiled (if you don't believe it, you can try it, I won't). Therefore, we need to set the text saved by the compiler to ask for plain text.
If you do not understand it, just follow it.
Open MyIDEDoc. cpp in the project and find the void CMyIDEDoc: Serialize (CArchive & ar) function. We can see a line of comment at the bottom.
// TODO: If it is serialized as text, set CRichEditDoc: m_bRTF = FALSE
We only need to move the code to the next line and add:
1 void CMyIDEDoc: Serialize (CArchive & ar) 2 {3 if (ar. isStoring () 4 {5 // TODO: add storage code 6} 7 else 8 {9 // TODO: add and Load Code 10} 11 12 // call the base class CRichEditDoc here to enable serialization of the 13 // container documentation's COleClientItem object. 14 // TODO: If serialized as text, set 15 CRichEditDoc: m_bRTF = FALSE; 16 CRichEditDoc: Serialize (ar); 17}
In this case, only plain text is saved.
2.4 implement simple MinGW Compilation
Our core technology is to run the MinGW batch file to drive compilation.
First, open the MinGW folder of CodeBlock. In order not to affect CodeBlock compilation, we copy a new mingwvars. bat is named mingwvars_A.bat. Open it in notepad and add the following command:
1 @echo.2 @echo Setting up environment for using MinGW with GCC from %~dp0.3 @set PATH=%~dp0bin;%PATH%4 5 c:6 cd C:\Users\Administrator\Desktop7 gcc 2.c8 a.exe
Enter the folder of the file to be compiled, compile the file named "2. c", and then execute it.
Next, add the event handler.
In the VS project, right-click the "run" button of GCC and click "add event handler". In the class list, select "CMianFrame" --> click "add edit ".
Then we add the code to the event handler of the button:
1 void CMainFrame: OnButton2 () 2 {3 // TODO: Add command handler code 4 system ("\" D: \ CodeBlocks \ MinGW \ mingwvars-A.bat \ ""); 5}
Change the path to the folder where your minGW is located, and change the file name to your custom batch file name.
Then we run the project --> enter the interface --> select compile in the upper left corner --> enter some code in the file editing box and save it to the folder to be compiled in the previously set batch file, save it as "2.c". You can also use another name to change the batch processing file. This is a simple example. You can also compile a file with any file name by writing the file.
--> Click Run of GCC
We can see the execution.
2.5 implement a Simple VC Compiler
First, go to the VC folder under the Vs installation directory. Just like calling minGW, we find the VC batch file vcvarsall. bat, copy a copy, rename to vcvarsall-A.bat --> edit --> under: x86
Call "% ~ Add code between dp0bin \ vcvars32.bat "and goto: SetVisualStudioVersion:
:x86if not exist "%~dp0bin\vcvars32.bat" goto missingcall "%~dp0bin\vcvars32.bat"C:cd C:\Users\Administrator\Desktopcl 2.c2.exegoto :SetVisualStudioVersion
Save, meaning, enterC: \ Users \ Administrator \ DesktopThe file folder is also displayed on the table. Then, compile the 2.c file and then compile the 2.exe file.
Next, add the event handler under the "run" button of the vc of the vs Project (same as above ):
1 void CMainFrame: OnButton3 () 2 {3 // TODO: Add command handler code 4 system ("\" F: \ Microsoft Visual Studio 11.0 \ VC \ vcvarsall-A.bat \ ""); 5 6}
Run the project, add the 2.c folder, and click run under the VS layout to get the following results:
Under the 2.c folder, we will find the 2.objand 2.exe files.
In this way, a C/C ++ IDE is implemented.