Lab 1 - Development Environment & First C++ Program
Responsible: Ing. Petr Šopák
This laboratory introduces the basic development environment used throughout the BPC-PRP course.
The goal is to get familiar with Linux, command-line tools, and to compile and run a simple C++ program.
By the end of this lab, you should be able to:
- navigate the Linux file system using the CLI,
- create and edit source files,
- compile and run a basic C++ program,
- understand the role of an IDE and ROS 2 in this course.
Linux & Command Line Basics (≈ 60 min)
Installation (optional)
To install Linux, please follow the Linux chapter.
Exercise
- Explore the system GUI.
- Open a terminal and navigate the file system.
- Practice basic CLI commands (see the Linux chapter):
- Check the current directory:
pwd - Create a directory:
mkdir <dir> - Enter a directory:
cd <dir> - Create a file:
touch <file> - List directory contents:
ls -la - Rename or move a file:
mv <old> <new> - Copy a file:
cp <src> <dst> - Remove a file:
rm <file> - Create/remove a directory:
mkdir <dir>,rm -r <dir>
- Check the current directory:
- Try a text editor:
nanoorvim
I installed vim and accidentally opened it. What now?
You can exit Vim with: press Esc, then hold Shift and press Z twice (Shift+Z+Z).More Info: https://www.vim.org/docs.php
More details about Linux will be introduced during the course.
First C++ Program – CLI Compilation (≈ 60 min)
Create a new project folder in your home directory and enter it.
Create a file main.cpp with the following content:
#include <iostream>
#define A 5
int sum(int a, int b) {
return a + b;
}
int main() {
std::cout << "My first C++ program" << std::endl;
int b = 10;
std::cout << "Sum result: " << sum(A, b) << std::endl;
return 0;
}
Compile the program using g++ (GCC C++ compiler):
g++ -o my_program main.cpp
Run the compiled binary:
./my_program
There are other alternatives, like Clang, LLVM, and many others.
TASK 1
- In your project folder, create an
includefolder. - In the
includefolder, create alib.hppfile and write a simple function in it. - Use the function from
lib.hppinmain.cpp. - Compile and run the program (tip: use
-I <folder>with g++ to specify the header search path).
TASK 2
- In the project folder, create
lib.cpp. - Move the function implementation from
lib.hpptolib.cpp; keep the function declaration inlib.hpp. - Compile and run the program (tip: you have to compile both
main.cppandlib.cpp). - Helper:
g++ -o <output_binary> <source_1.cpp source_2.cpp ...> -I <folder_with_headers> - Discuss the difference between preprocessing, compiling, and linking.
IDE Overview – CLion (≈ 15 min)
Installation
Install CLion using the Snap package manager:
sudo snap install --classic clion
Alternatively, download CLion from the official website and get familiar with it (see CLion IDE). By registering with your school email, you can obtain a free student license.
To learn how to control CLion, please take a look at the tutorial or the official docs.
TASK 3
- Learn how to control CLion
- Open the previously created C++ program (main.cpp) in CLion.
- Build and run the program using the IDE controls.
ROS 2 – Course Context (≈ 15 min)
ROS 2 (Robot Operating System 2) is a framework for building robotic systems. In this course, ROS 2 will be used later for:
- communication between software components,
- visualization (RViz, rqt),
simulation (Gazebo).
For installation and basic commands, see the ROS 2 chapter: ROS 2.