### How to Make a "Hello, World!" Program in C++ Using GCC
Creating a "Hello, World!" program is often the first step when learning a new programming language. In this article, we'll guide you through the process of writing and compiling a simple C++ program using the GNU Compiler Collection (GCC).
#### Step 1: Write the C++ Code
First, open a text editor or an integrated development environment (IDE) where you can write your C++ code. Here’s a basic "Hello, World!" program:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
```
Let’s break down what this code does:
- `#include <iostream>`: This line includes the standard input-output stream library, which allows us to use `std::cout` for printing output.
- `int main() { ... }`: This is the main function where program execution begins. Every C++ program must have a `main` function.
- `std::cout << "Hello, World!" << std::endl;`: This line prints "Hello, World!" to the console.
- `return 0;`: This line indicates the successful termination of the `main` function.
#### Step 2: Save Your Code
Save the file with a `.cpp` extension, for example, `hello.cpp`. Choose a directory where you can easily locate the file.
#### Step 3: Open a Terminal or Command Prompt
Next, open a terminal or command prompt on your operating system. This is where you’ll compile your C++ code using GCC.
#### Step 4: Compile the Code
To compile your `hello.cpp` program using GCC, use the following command:
```
g++ -o hello hello.cpp
```
- `g++`: This is the command to invoke the GNU C++ compiler.
- `-o hello`: This option specifies the output file name. In this case, the output file will be named `hello`.
- `hello.cpp`: This is the source file containing your C++ code.
If there are no errors in your code, GCC will generate an executable file named `hello` in the same directory where you ran the command.
#### Step 5: Run the Program
Finally, to run your "Hello, World!" program, use the following command:
```
./hello
```
You should see the output:
```
Hello, World!
```
Congratulations! You’ve successfully written, compiled, and executed a "Hello, World!" program in C++ using GCC. This simple exercise is foundational for understanding how to write and compile C++ programs on your machine.
Creating a "Hello, World!" program is often the first step when learning a new programming language. In this article, we'll guide you through the process of writing and compiling a simple C++ program using the GNU Compiler Collection (GCC).
#### Step 1: Write the C++ Code
First, open a text editor or an integrated development environment (IDE) where you can write your C++ code. Here’s a basic "Hello, World!" program:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
```
Let’s break down what this code does:
- `#include <iostream>`: This line includes the standard input-output stream library, which allows us to use `std::cout` for printing output.
- `int main() { ... }`: This is the main function where program execution begins. Every C++ program must have a `main` function.
- `std::cout << "Hello, World!" << std::endl;`: This line prints "Hello, World!" to the console.
- `return 0;`: This line indicates the successful termination of the `main` function.
#### Step 2: Save Your Code
Save the file with a `.cpp` extension, for example, `hello.cpp`. Choose a directory where you can easily locate the file.
#### Step 3: Open a Terminal or Command Prompt
Next, open a terminal or command prompt on your operating system. This is where you’ll compile your C++ code using GCC.
#### Step 4: Compile the Code
To compile your `hello.cpp` program using GCC, use the following command:
```
g++ -o hello hello.cpp
```
- `g++`: This is the command to invoke the GNU C++ compiler.
- `-o hello`: This option specifies the output file name. In this case, the output file will be named `hello`.
- `hello.cpp`: This is the source file containing your C++ code.
If there are no errors in your code, GCC will generate an executable file named `hello` in the same directory where you ran the command.
#### Step 5: Run the Program
Finally, to run your "Hello, World!" program, use the following command:
```
./hello
```
You should see the output:
```
Hello, World!
```
Congratulations! You’ve successfully written, compiled, and executed a "Hello, World!" program in C++ using GCC. This simple exercise is foundational for understanding how to write and compile C++ programs on your machine.
Last edited: