Sign in to follow this  

C++ Generating your own EXE?

This topic is 4547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I was just wondering how you would go about compiling/generating your own EXE's. Is this possible? Are there any links you can give me? [EDIT]I mean thorugh C++ code[/EDIT] Thanks in advance. [Edited by - sathenzar on July 1, 2005 1:37:15 AM]

Share this post


Link to post
Share on other sites
Yes compiling / linking generates an .exe file. C/C++ is not like BASIC, an .exe file is the only way to execute your program.

Share this post


Link to post
Share on other sites
thank you, any other suggustions are welcome, and yes, Saurman I already know how to compile/generate an EXE thorugh VC++ or another compiler. I'm talking about thorugh actual code you can write in C++. I always admired people that made compilers b/c I can only imagine what goes into them.
Any other help is welcome. Thanks again.

[Edited by - sathenzar on July 4, 2005 6:35:31 PM]

Share this post


Link to post
Share on other sites
o ok, I see why everyone is confused with my post. Sorry, I meant through code. my fault. I guess I should put more detail in my description then I do for my title. Sorry everyone.

Share this post


Link to post
Share on other sites
One more question, I've been looking through google and I havn't really been able to find what I'm looking for. DevLiquidKnight, could you send me some links? I really appreciate everyone helping me so far.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for a lot of exe-"generation" purposes, just storing prebuilt executables and modifying their resources might be sufficient. however, if that's not enough, wotsit has some documentation on the PE-format. www.wotsit.org

Share this post


Link to post
Share on other sites
Quote:
Original post by sathenzar
... I always made me admire people that made compilers b/c I can only imagine what goes into them....


Yeah, it can be pretty tough. For my school you have to learn discreet systems (mathematics), and a lot about grammars in their various forms. And that doesn't even begin to get into the actual work of compiling. That basically can get your syntax into a symbol language, which can then be compiled (even allowing multiple languages to use the same symbol language and be compiled by the same compiler). I've taken all the courses up to the compiler course, but have not gotten to the compiler course yet. I did take a course on writing an interpreter though.

Share this post


Link to post
Share on other sites
Quote:
Original post by sathenzar
Hey everyone, I was just wondering how you would go about compiling/generating your own EXE's. Is this possible? Are there any links you can give me?
[EDIT]I mean thorugh C++ code[/EDIT]
Thanks in advance.



While I wouldn't say creating compilers are difficult per-se they are typically very time consuming, can require a lot of planning, and are not a particularly easy undertaking. This of course can also depend on the complexity of the language you are trying to implement.

If you are truely interested in creating a compiler you will want to familiarize yourself with some of the tools that are commonly used. You'll see mention of using Flex and Bison for creating the lexical and grammar parsers. Both Flex and Bison are fairly dated and typically lacking when compared to other tools. Personally I recommend using ANTLR as it's capable of doing the job of both, automatically builds abstract syntax trees (AST), and has tons of documentation.

Tools like flex, bison, and antlr are just a small portion of what you'll need. You'll still need to do a certain amount of syntax checking, manage symbols, and of course do code generation (assembler, bytecode, or directly executable code). If you're serious about writing a compiler I recommend checking out two or more of the following books:

"A Retargetable C Compiler"
"Compilers: Principles, Techniques, and tools" - usually referred to as "The Dragon Book"
"Advanced Compiler Design and Implementation"
"Writing Compilers and Interpreters"
"Compiler Construction"
"Engineering a Compiler"


Links:

LCC from "A Retargetable C Compiler" : http://www.cs.princeton.edu/software/lcc
ANTLR: www.antlr.org
comp.compilers newsgroup: http://compilers.iecc.com

Share this post


Link to post
Share on other sites
Quote:
Original post by sathenzar
thank you, any other suggustions are welcome, and yes, Saurman I already know how to compile/generate an EXE thorugh VC++ or another compiler. I'm talking about thorugh actual code you can write in C++. I always made me admire people that made compilers b/c I can only imagine what goes into them.
Any other help is welcome. Thanks again.


C is actually quite simple, to the point where you could write yourself a really simple compiler for a subset of C pretty quick. (for a vm, of cource. You don't EVER want to compile to machine code.... its bloody hard compared to the compiling part).

First, remove whitespace.
Second turn it into a massive array.
Third turn it into a tree.
Forth, you start compiling, turning simple things like I++ into inc i, as well as doing expression parsing (which is hard)
Fifth, you compile the variables, adding the variable loads/stores, depending on vm.
sixth, you do things like loops, if's, ect.
seventh, function calls
eighth, you compile to vm machine code from the asm you made.
ninth, you can do some keyhole optmiisations. (like div eax, 2 == lshift x, 1)

Thats about it.

For eg.

int main() {
if(x == 3) {
y = 7
}
}



First remove whitespace.


int main() {if(x == 3) {y = 7}}



Turn into a tree

Main
cmp x, 3
jneq 2
Set y, 7
exit



Change variables to registers


Main
cmp eax, 3
jneq 2
Set ebx, 7
exit



Function calls replaced by adresses. (cause you know exactly how big each function is, you can give them apsolute adresses)

Now, you go from tree to asm.


cmp eax, 3
jneq 2
Set ebx, 7
exit



And from there you compile to asm...

From,
Nice coder

Share this post


Link to post
Share on other sites
Maybe you want to look into the source of UPX. Since it generates executables (after compression), there may be code that shows what you want to do.

Share this post


Link to post
Share on other sites
One option would be to use an existing compiler, write the source code that you want, then execute the existing compiler (for example, with system() or similar), and use the output.

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
C is actually quite simple, to the point where you could write yourself a really simple compiler for a subset of C pretty quick. (for a vm, of cource. You don't EVER want to compile to machine code.... its bloody hard compared to the compiling part).

First, remove whitespace.
Second turn it into a massive array.
Third turn it into a tree.
Forth, you start compiling, turning simple things like I++ into inc i, as well as doing expression parsing (which is hard)
Fifth, you compile the variables, adding the variable loads/stores, depending on vm.
sixth, you do things like loops, if's, ect.
seventh, function calls
eighth, you compile to vm machine code from the asm you made.
ninth, you can do some keyhole optmiisations. (like div eax, 2 == lshift x, 1)


1. Why is compiling to machine code any more difficult than compiling to bytecode? The exact same issues apply.

2. Steps one, two, and three would be better explained if you included the topics of lexical analysis and tokenizing the input stream. This might sound overly technical or unnecessary but there are significantly more resources available explaining them rather than just "make a massive array and turn it into a tree". An array of what? What kind of tree?

3. Simply turning "I++" into "inc i" doesn't always work. Most people find it more difficult to deal with postfix expressions in comparison to expressions like "a + b". This is one of the reasons using C as the target language is not always a good choice for first time compiler writers. i = 400; i = ++i / 2 * i++ + 2;

4. You've missed issues such as variable scope, initializers, and arrays not to mention local storage for variables. Even assuming the target is a virtual machine these are issues that will likely need to be addressed at some point and time.


Trying to simplify compiler design and implementation is not fair to the original poster. It leaves out way too many things that he needs to be prepared to address. It's better to know what you're facing rather than get into the middle of coding it and be totally unprepared.

Share this post


Link to post
Share on other sites

This topic is 4547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this