install gcc compiler and write a hello world program with cygwin on windows 10 and windows 11
watch the tutorial here:
a tutorial on using GCC with Cygwin:
### Step 1: Install Cygwin
1. **Download the Installer**:
- Go to the official Cygwin website (https://cygwin.com/) and download the `setup-x86_64.exe` (for 64 - bit Windows) or `setup-x86.exe` (for 32 - bit Windows).
2. **Run the Installer**:
- Launch the installer. In the installation wizard, choose your download source (you can select “Install from Internet”).
- Select the root installation directory (usually the default is fine).
- Choose a local package directory where the downloaded packages will be stored.
- Select your Internet connection type.
- For the mirror site, choose a reliable one close to your location.
3. **Select Packages**:
- In the package selection step, search for “gcc”. You need to select the relevant GCC packages. At a minimum, you should install `gcc - core` (for C compilation) and `gcc - g++` (for C++ compilation). Expand the “Devel” category to find these packages. Click on the “Skip” button next to the packages you want to install, and it will change to a version number.
4. **Complete the Installation**:
- Click “Next” to start the package download and installation process. This may take some time depending on your Internet speed.
### Step 2: Verify the Installation
1. **Open Cygwin Terminal**:
- After the installation is complete, open the Cygwin terminal. You can find it in the Start menu.
2. **Check GCC Version**:
- In the Cygwin terminal, type the following command to check if GCC is installed correctly and to see its version:
```bash
gcc --version
```
If GCC is installed properly, it will display the version information. Similarly, you can check the C++ compiler with:
```bash
g++ --version
```
### Step 3: Write and Compile a Simple C Program
1. **Create a C Program**:
- Use a text editor (you can use the built - in `nano` or `vim` in the Cygwin terminal). For example, to create a simple “Hello, World!” program:
```bash
nano hello.c
```
In the `nano` editor, type the following code:
```c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
```
Press `Ctrl + X`, then `Y` to save the file, and then `Enter` to confirm the file name.
2. **Compile the Program**:
- In the Cygwin terminal, use the following command to compile the `hello.c` program:
```bash
gcc -o hello hello.c
```
Here, `-o` specifies the output file name. In this case, the output executable will be named `hello`.
3. **Run the Program**:
- After successful compilation, you can run the program by typing:
```bash
./hello
```
You should see the output “Hello, World!”.
### Step 4: Compile a C++ Program
1. **Create a C++ Program**:
- Similar to the C program, create a simple C++ “Hello, World!” program.
```bash
nano hello.cpp
```
In the `nano` editor, type the following code:
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
```
Save the file as before (`Ctrl + X`, `Y`, `Enter`).
2. **Compile the C++ Program**:
- Use the `g++` compiler to compile the C++ program:
```bash
g++ -o hello_cpp hello.cpp
```
3. **Run the C++ Program**:
- Execute the compiled program:
```bash
./hello_cpp
```
You should see the output “Hello, World!”.
This basic tutorial should help you get started with using GCC for C and C++ programming in a Cygwin environment.