This probably been asked for but I can't google "How to make a compiler in C++" because I will just get "How to compile C++" as the results. Anyway, for my question, I'd like to make a simple programming language in C++. Now I understand basic file IO stuff but what I don't get is how to build a EXE. The problem here is I don't know how EXEs exactly are "planned out", granted that most people don't. I was going to simply parse the language into assembly and assemble it using an assembler. But I don't want to do that, I want to actually compile it directly into a EXE. Does anyone know how this would be done? PS: To all you people who say making a compiler is virtually impossible, it's a fairly fast process, it's just implementing OOP features that's hard
-
It's not clear whether you want to make a C++ compiler, or to write a compiler using C++ for no target language in particular.– whatsisnameCommented Jun 10, 2013 at 4:31
-
3And why do you think people would say writing a compiler is virtually impossible? With flex/bison/antlr, etc, you could write a basic compiler or interpreter for many languages over a weekend.– whatsisnameCommented Jun 10, 2013 at 4:32
-
1Probably a relevant link: coursera.org/course/compilers Assembler code generation is covered in that course.– KolyunyaCommented Jun 10, 2013 at 5:18
-
3You say OOP features are hard; and you say generating .exe's is hard. They are completely unrelated though: OOP features are handled in the early stages of compilation, by the time you're ready to emit binaries, the OOP stuff should be boiled down to straightforward imperative spaghetti code.– tdammersCommented Jun 10, 2013 at 8:31
-
3Have you seen this? stackoverflow.com/questions/1669/learning-to-write-a-compiler– Robbie DeeCommented Jun 10, 2013 at 9:33
2 Answers
I think you're asking the wrong question:
How you make I compiler in C++?
As in with any other language, actually. You'll need to write something that takes an input (likely from files) and compiles this input into something that's machine-readable and follows a specific format (in your case, C++).
So your question isn't correct and relevant. The questions you should be asking are:
What are the specifications for an executable (.exe
) file on the Windows platform?
Look into the specs of the PE format. More details available here:
How to write a compiler for C++ in C++?
That is, of course, assuming that you want to compile c++ files, but I'm just guessing here as you don't clearly state what you want to compile...
And from the way you formulate your answer, you already know how to do that, though you raise points about the OO model.
Please clarify your question if you need more.
There is not much difference between understanding assembly and understanding the machine code that it creates. You should create your own assembler or at least study the disassembly of asm that has been assembled by some toolchain, such that you can create your own machine code. Again not a stretch if you are proficient in assembly. The next issue is the EXE file format. Googling should make that quite easy. As far as planning out what goes in the EXE, that is basic programming, not specific to an EXE.
You will most likely be successful if you start by having your language compiler output assembly language, then use an existing toolchain (assembler and linker) to turn that assembly language into an EXE. Create and debug your language and compiler. Then replace the assembler and linker by generating the machine code directly.