-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compile_And_Execute_In_Terminal
45 lines (27 loc) · 2.26 KB
/
Compile_And_Execute_In_Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# COMPILE AND EXECUTE
Code → Save → Compile → Execute
C++ is a compiled language.
That means that to get a C++ program to run, you must first translate it from a human-readable form to something a machine can “understand.”
That translation is done by a program called a compiler.
When you program in C++, you mainly go through 4 phases during development:
Code — writing the program
Save — saving the program
Compile — compiling via the terminal
Execute — executing via the terminal
And repeat (debug the errors if needed).
Note: Up until now, we’ve been doing the magic for you; Step 2, 3, and 4 were all done by simply pressing the Run button.
------------------------------------------------ COMPILE AND EXECUTE USING TERMINAL ------------------------------------------------
Compile: A computer can only understand machine code. A compiler can translate the C++ programs that we write into machine code. To compile a file, you need to type g++ followed by the file name in the terminal:
g++ hello.cpp
The compiler will then translate the C++ program hello.cpp and create a machine code file called a.out.
Execute: To execute the new machine code file, all you need to do is type ./ and the machine code file name in the terminal:
./a.out
The executable file will then be loaded to computer memory and the computer’s CPU (Central Processing Unit) executes the program one instruction at a time.
--------------------------------------------------------- NAMING EXECUTABLE ---------------------------------------------------------
Compile and Execute (Naming Executables)
Compile: Sometimes when we compile, we want to give the output executable file a specific name. To do so, the compile command is slightly different. We still need to write g++ and the file name in the terminal. After that, there should be -o and then the name that you want to give to the executable file:
g++ hello.cpp -o hello
The compiler will then translate the C++ program hello.cpp and create a machine code file called hello.
Execute: To execute the new machine code file, all you need to do is type ./ and the machine code file name in the terminal:
./hello
The executable file will then be loaded to computer memory and the computer’s CPU will execute the program one instruction at a time.