Compile C program with gcc compiler on Bash on Ubuntu on Windows 10 & 11

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. You can compile a C program by using the gcc command in Windows 10 Bash on Ubuntu.

1.Enable Bash on Ubuntu on Windows 10

If you don't have a enable Ubuntu on Windows check out Stepwise Guide to Enable Windows 10’s Ubuntu Bash Shell (Windows Subsystem for Linux).

2. Install gcc compiler in Windows 10 Bash

To install gcc compiler in Windows 10 Bash, Open bash and run this command

apt-get install gcc

Make sure compiler is installed on your Windows 10 Bash. Type the following command to verify that gcc is installed:

which gcc

Sample outputs:

/usr/bin/gcc

Find out version of gcc, run:

gcc --version

3. Write your first program on bash

Use a text editor such as nano or vi to create a C program called hello.c:

nano hello.c

Type the following lines (program):

#include <stdio.h>

int main()
{
  printf("hello world\n");
  return 0;
}

After writing your program, press Ctrl + O and hit Enter key to save your program. To exit nano press Ctrl + X.

4. Compile and Run Program

To compile C program hello.c, and create an executable file called hello, enter:

gcc hello.c -o hello

To execute program first, enter:

./hello

Output:

hello world


Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.