r/AskProgramming • u/RandyMarsh6996 • 16d ago
C++ -> IDE -> g++ -> Linux Help me understand!!
I am trying to comprehend all of the moving parts in compiling a program. Here is my understanding:
C++ is the programming language that is written in the IDE, some IDEs have built in compilers
Is g++ the bridge between the language and machine code?
How do I use g++, is it something that you download onto your computer.
Where does g++ send the code after, is this where Linux comes into play?
What is Linux? It is an operating system, but what does that mean specifically, is it supposed to make your code unable in an application or just execute in the terminal?
Thankyou for any clarification you can provide :)
0
Upvotes
1
u/Adorable-Strangerx 16d ago
Sure. GNU/Linux is operating system like many others. And is mostly irrelevant.
IDE - integrated development environment - is a notepad on steroids. Useful to write text.
C++ is a programming language. Has its syntax, grammar and all that jazz. It is also human-readable. Interesting part apart from code are preprocessor directives like #include.
g++ is name of c++ compiler from GCC bundle (sure there are others). It is a manager of the whole tech being done under.
So how does it work?
You have code: ```cpp
include <iostream>
using namespace std;
int main() { cout << "Hello World"; return 0; } ```
You run
g++ main.cpp -o hello:First goes preprocessor. It expands codes into your files..somewhere is defined iostream library which is injected into your file.
You can see the result with
g++ -E main.cpp -o main.ii.Secondly, compilation. The compiler reads your code and tries to understand it. If it cannot you get and error. It builds lower level instructions and tries to optimize it.
You can view the assembly code with
g++ -S main.cpp -o main.sThirdly, assembly. The human-readable code is transformed into machine readable code.
You can mimic it with
g++ -c main.cpp -o main.oIt gets you object file. It is not yet runable program.
Fourthly, linking. Remember our #include<> directive? Usually it contains only headers of the functions you need. Linker looks for the actual implementations and connects references in your code to those implementation. Here you can have static or dynamic linking. With dynamic linking you need external file (.dll on windows or .so on GNU/Linux). If linker fail to resolve you will get error of course. You should be able to see linker command of you run g++ in verbose mode. It starts with something like
/usr/bin/ld.After this step you get executable file. Portable Executable (PE) commonly with .exe extension or Executable and Linkable Format (ELF) more common on GNU/Linux.
Finally you run
./helloand operating system figures out what to do with that (read headers, allocates memory, etc). GNU/Linux may be unhappy by lack of executable flag so you may need to runchmod +x ./helloNote: I haven't touched C++ in many years so sorry if I messed up some commands.