Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

closed as too broad by gnat, Joris Timmermans, Kilian Foth, Dan Pichelman, MichaelT Sep 2 '13 at 15:59

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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. –  whatsisname Jun 10 '13 at 4:31
3  
And 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. –  whatsisname Jun 10 '13 at 4:32
    
Probably a relevant link: coursera.org/course/compilers Assembler code generation is covered in that course. –  Kolyunya Jun 10 '13 at 5:18
2  
You 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. –  tdammers Jun 10 '13 at 8:31
2  
Have you seen this? stackoverflow.com/questions/1669/learning-to-write-a-compiler –  Robbie Dee Jun 10 '13 at 9:33

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.