DEV Community

Cover image for How to install and use C/C++ compiler in WSL (Windows Subsystem for Linux)
Alberto Monraz
Alberto Monraz

Posted on

How to install and use C/C++ compiler in WSL (Windows Subsystem for Linux)

Check it out this article if you want to enable WSL on your machine

First you need open your distro console (in my case I use ubuntu) and execute the following commands

$ sudo apt-get update 
$ sudo apt-get install build-essential gdb
$ whereis gcc g++ gdb
Enter fullscreen mode Exit fullscreen mode

then you got the next output (or similar)

gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/gcc /usr/share/man/man1/gcc.1.gz
g++: /usr/bin/g++ /usr/share/man/man1/g++.1.gz
gdb: /usr/bin/gdb /etc/gdb /usr/share/gdb /usr/share/man/man1/gdb.1.gz
Enter fullscreen mode Exit fullscreen mode

Now we going to create a simple program for addition in C and sorting in C++.

First create a directory and files to work in this project

$ mkdir addition
$ cd addition
$ touch main.c ari.h ari.c input.txt output.txt
Enter fullscreen mode Exit fullscreen mode

now add this code to file "ari.h"

#ifndef ARI_INC
#define ARI_INC

int add(int, int);

#endif
Enter fullscreen mode Exit fullscreen mode

add this code to file "ari.c"

#include "ari.h"

int add(int a, int b){
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

add this code to file "main.c"

#include <stdio.h>
#include "ari.h"

int main(){
  int a = 0, b = 0, r = 0;
  scanf("%d %d", &a, &b);
  r = add(a, b);
  printf("%d+%d=%d\n",a,b,r);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

add this text to file "input.txt"

22 33
Enter fullscreen mode Exit fullscreen mode

execute the following command:

$ gcc -g <code file(s) .c> -o <executable file without extension>
Enter fullscreen mode Exit fullscreen mode
  • $: Ignore it, this is the indicator of terminal row
  • gcc : Is used for C programs
  • -g : Basically creates an info link between source code and executable
  • code file(s) : If you are working only with one code file you put the name with extension example hello.c but in this case we use main.c ari.c
  • -o : Basically indicates the output executable
  • executable file without extension : Only the name of the file to be the executable without extension (even you can use .exe because you are in windows but I don't recommend you)

Real example of full command:

$ gcc main.c ari.c -o main
Enter fullscreen mode Exit fullscreen mode

then execute the following command:

$ ./main <input.txt >output.txt
Enter fullscreen mode Exit fullscreen mode
  • ./ : This is the way to execute programs in Linux
  • < : Indicates that the input is in a text file and program read the text file named input.txt (you can rename the input file as you like)
  • > : Indicates that the output is in a text file and program writes the text file named output.txt (you can rename the output file as you like)
  • If you don't want to use an input.txt and output.txt you can write the input manually in console during execution and output will be show in the console display.

To see what is the output, execute the following command:

$ cat output.txt
22+33=55
Enter fullscreen mode Exit fullscreen mode

You can execute it, in only one line like this:

$ gcc -g main.c ari.c -o main && ./main <input.txt >output.txt && cat output.txt
22+33=55
Enter fullscreen mode Exit fullscreen mode

Now for C++ the sorting project:

$ cd ../
$ mkdir sorting
$ cd sorting
$ touch main.cpp in.txt out.txt
Enter fullscreen mode Exit fullscreen mode

add this code to main.cpp

#include <iostream>
#include <algorithm>

int main(){
  int len, i;
  std::cin>>len;
  int arr[len];
  for(i=0;i<len;++i) std::cin>>arr[i];
  std::sort(&arr[0], &arr[0] + len);
  for(i=0;i<len;++i) std::cout<<arr[i]<<" ";
  std::cout<<"\n";
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

add this text to in.txt

5
3 4 5 1 9
Enter fullscreen mode Exit fullscreen mode

then execute the following command:

$ g++ -g main.cpp -o main && ./main <in.txt >out.txt && cat out.txt
1 3 4 5 9
Enter fullscreen mode Exit fullscreen mode
  • g++ : Is used for C++ programs

In the next time I write some articles to use and understand correctly the options for the compiler.

Thanks you for reading, good luck!

Discussion (0)