Setup
You can code C++
- using an IDE (e.g. Visual Studio on Windows, Xcode on Mac)
- using any text editor plus the C++ compiler (e.g. GCC)
- using an online editor (e.g. https://www.programiz.com/cpp-programming/online-compiler/)
In this tutorial, we will install GCC (tailored for Linux/Mac users).
Installing GCC
Install gcc:
# On linux
# build-essential includes gcc, g++ and make
sudo apt install build-essential
# On mac
brew install gcc
Verify your gcc installation:
gcc --version
Compiling your code
Save the following code, for example as hello.cpp.
#include <cstdio>
int main() {
printf("Hello, world!\n");
return 0;
}
Compile your code using gcc.
You can specify the name of your output file using -o <filename>; otherwise gcc will use a.out as the default output filename.
# it will output an executable file named `hello`
gcc helo.cpp -o hello
Run the code. For the example above, it should print Hello, world!.
./hello
References
- How to Install GCC Compiler on Ubuntu 18.04 — https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/
- Homebrew Formulae: gcc — https://formulae.brew.sh/formula/gcc