This C++ code compiling example is also version control exercise of the Tampere University of Applied Sciences included in Bachelor of Engineering studies (Information and Communications Technology).
-
When executed program calculates square of given number.
-
Afterwards program is expanded to calculate volume of cube (also known as regular hexahedron) with same user given number.
Code is created and tested with Ubuntu Workstation 24.04.1 LTS with Visual Studio Code as editor. Code can be compiled using VS Code's terminal or Ubuntu's own terminal.
VS Code documentation has install instructions for Ubuntu and Debian based distros at:
https://code.visualstudio.com/docs/setup/linux#_debian-and-ubuntu-based-distributions
...or you can use script below I have created according to documentation:
https://github.com/sloul1/script/blob/main/install-vscode-ubuntu.sh
Open terminal by pressing Ctrl + Alt + T keys simultaneously.
OS version can be checked by command:
lsb_release -aExample print:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noblewhereis build-essential gitIf build essential and git are not installed update repositories' package lists and install build-essential and git packages by:
sudo apt update && sudo apt install build-essential git -yIn our example build-essential packet installs:
gcc: The GNU Compiler Collection, which compiles C and C++ code.
g++: The compiler for C++ programs based on GCC.
make: The program that executes the commands specified in a Makefile to build software from source.
libc6dev: GNU C Library: Development Libraries and Header Files
or
libc-dev: virtual package provided by libc6-dev
More info on packages of your Ubuntu version can be found at: https://packages.ubuntu.com/
3. Clone this repository from https://github.com/sloul1/cpp_make_tutorial.git
Code:
git clone https://github.com/sloul1/cpp_make_tutorial.gitMakefile can be ran by commanding:
makeAs result few new files are created (main main.o compute.o).
Make note that .gitignore file is used for ignoring these and other unnecessary files.
g++ -Wall -g -c main.cpp
g++ -Wall -g -c compute.cpp
g++ -Wall -g -o main main.o compute.oNew file "main" can be executed with number 3:
./main 3Which gives 9 as result (square of 3).
9Files created can be removed afterwards by command:
make cleanResulting:
rm -f main.o compute.o mainAdded code for calculating volume of cube can be checked from 'additions' file.
After running 'make' again main program calculates also cube volume:
make./main 3resulting:
9
27