0 - Introduction
If you are a engineering or computer science student or if you just have interest in C, GCC is the first compiler that you should try. It is made by the GNU foundation and is also very easy to setup and use on your machine.
But before that, let’s update your system:
sudo apt update
sudo apt upgrade
1 - Install
We will install ‘build-essential’ as it not only has gcc and g++ but it also contains other utilities like make and dpkg-dev.
sudo apt install build-essential
You can check the success of the previous step by running:
gcc --version
2 - Hello World in C
Now that we have a C compiler let’s make a simple file named ‘hello.c’ with nano, or any other text editor.
nano hello.c
In the file we write the following simple program that just prints ‘Hello World’ to the console:
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Now that we have our file we can run gcc to compile it passing ‘-o’ as the executable name:
gcc hello.c -o hello
And if it compiles, you can run the program with:
./hello
And that’s it, a very simple program compiled with GCC.
3- Bonus!! Hello World in C++
An Hello World program in C++ is very similar to C, let’s take a look:
nano hello.cpp
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
Now instead of using gcc we use g++, but with the similar arguments:
g++ hello.cpp -o hello
And if it compiles, you can run the program with:
./hello
And that’s it, now you know how to make an Hello World both in C and C++, we hope you liked this tutorial (and the mini bonus) and thanks for reading!
Stay tuned for more tech insights and tutorials. Until next time, and keep exploring the world of tech!