Compilation process for C++ application? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been programming for 2 years now and always have faced difficulty when dealing with the compilation process.
I did not study Computer Science during my Engineering, but necessity drove me towards learning C++.
I tried understanding the compilation process from some blogs, but they were always in a language I could not understand.
So I searched this site for a similar question, but could find none.
So I would like to know how the text from a .cpp is converted to a binary executable?

Basically, the preprocessor runs first resolving all your #includes, #defines, etc with simple text substitution. Then the compiler creates a compilation-unit for each .cpp file which pretty much boils everything down to machine-code except for "connections" or linkages between shared data and functions. There may be many levels of optimisation for speed and/or space performed. This is repeated for all your .cpp files. Finally, a link phase ties all these compilation-units and the libraries they use together into an executable.

Related

Flex/Bison based compiler - binary generation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a compiler of Pascal subset written using Flex/Bison and C++. As input I use own .pas files. I have noticed that when I run my compiler with those input it works more like an interpreter because I get an instant result. I would like to get the binary file to avoid the need of using the compiler every time. My question is:
What is the best approach to generate binary file?
You can do this for example in this way (I done it in this way many years ago):
Using Flex/Bison, you have to generate pseudo asm code.
In the next step, you have to translate it to macro assembly for the given CPU architecture (x86/ARM/MC68k/PPC/...)
Compile output asm file into object file (.o)
Link .o file/files with needed libs (.lib) into executable file.
Besides, as commented above, try to find some book about compilers. The best one will be the Dragon Book, a kind of compiler bible: https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools

Why is Boost.ProgramOptions not header-only? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Some boost libraries are header-only, some are not, and for various reasons etc.
Is there a specific reason/design decision why Boost.ProgramOptions is not header-only?
I'm wondering because it claims to be a "small" library in its the documentation and I don't see any system-related reason (like threads or asio).
Program Options claims to be small, but it turns out to be the second largest library we were building, after Regex. (It is bigger than boost Filesystem and Thread libraries.) I believe that you should be glad they're building a library for it instead of choking your project with a ton of included headers. Perhaps the author thought it would be small when he started and forgot to change the comment when it continued to grow and add features.
Not all C++ code can be written in just headers due to one-definition-rule violations.
For example, the storage reservation for a static member of a class needs to be in exactly one translation unit (although future C++ standards may obviate that).
The original intention was for Boost to be header only, but they had to quickly relinquish that aspiration.

What are the risks of a large c++ file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I asked a question about global variables and one of the answers raised another question which is what is the risk of very large cpp file?
Is the concern here is the maintainability of the program or something else?
My original question
Only maintainability. There is no compilation issues, as it is common for compilers to combine all #include files into a translation unit and then compile that. Thus each .cpp file winds up being many times larger than the input anyway, before moving on to later stages of compilation.
For a single programmer working on his own program, when size become an issue is a personal choice. For a team of programmers at some company, having some reasonable number of C++ files for an application allows each team member to work on a separate file in parallel. Although there are tool sets that can merge separate edits made to the same source file(s), dealing with potential conflicts (someone has to check and/or fix the conflicts), is an issue.

Programatically creating and compiling from a program in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let's say we've got a first program called Program1.exe which contains the necessary information to create and compile another application called Program2.exe. Actually it could also load that information from a txt file or whatever.
Googling, I've found that this is "easy" to do in C#, using Visual Studio:
How to programatically build and compile another c# project from the current project
Programmatically Invoke the C# Compiler
The problem is that I'm not using (and can't use) C#, but C++. Summing it up, my question is if that I can do this same thing using C++.
I would prefer to do it without additional libraries, but if that's not possible, or if it's too hard to do, you can also recommend any library allowing it.
I think you'll probably have noticed it, but my goal is to use it under Windows so I don't care if it's not portable.
Thanks everybody.
It's trivial (if maybe a bit odd) for a C++ program to compile and run another based on code stored in a text file. Debugging that other program, however, isn't.

Building a compiler steps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been thinking about building my own compiler for a while and a few days ago I finally started on it. My compiler works like this:
Parse code from my own file. (With a .exe file made with c++)
Create assembly code
Create a file containing those assembly code
Compile that assembly file if it is made (done with a vbs script)
Link the .obj file
And we have our .exe file
Now I am having difficulties with finding the best way to parse my code. I haven't really made this yet but I will put my ideas here.
Find all variables and declare them. variables will be preceded with a 'var ' (for now). uninitialized variables will be put in the .data? section and initialized ones in the .data section.
Find the main procedure and start executing the functions and operations.
Now I was simply wondering if someone can improve my ideas. Or if someone has a better idea to make some kind of compiler and your own programming language.
Get yourself a copy of A. V. Aho, M. S. Lam, R. Sethi, J. D. Ullman: Compilers: Principles, Techniques, and Tools and start studying
The book covers the necessary theoretical background, especially:
Context-free grammars
Recursive-descent, LL, LR parsing
Symbol handling
Intermediate representation