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 g++ command in Bash on Ubuntu on Windows 10.
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 g++ compiler in Windows 10 Bash
To install g++
compiler in Windows 10 Bash, Open bash and run this command
apt-get install g++
Make sure compiler is installed on your Windows 10 Bash. Type the following command to verify that g++ is installed:
which g++
Sample outputs:
/usr/bin/g++
Find out version of g++, run:
g++ --version
3. Write your first program on bash
Use a text editor such as nano or vi to create a C++ program called hello.cpp:
nano hello.cpp
Type the following lines (program):
#include <iostream>
using namespace std;
int main()
{
cout<<"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.cpp
, and create an executable file called hello, enter:
g++ hello.cpp -o hello
To execute program first, enter:
./hello
Output:
hello world!!