How to Add an External C++ Library to Your Project

Libraries in C++ are collections of code that someone else wrote. They prevent you from having to reinvent the wheel when you need to implement some desired functionality. You can think of libraries as a plugin or add-on that gives you more functionality.

For example, I wanted to write a program that is able to multiply two matrices together. Instead of writing the code from scratch, I searched the web for a linear algebra library in C++ that contained functionality for multiplying matrices. I found the library named “Eigen.” I added the library to my project in the CodeLite IDE, and I was ready to go.

Without further ado, here is the step-by-step process for adding an external C++ library to your project using the CodeLite IDE and Visual Studio IDE.

Note that this process will be different if you are using another IDE for C++, but the two basic steps are the same for all IDEs:

  1. Add the path for the header files
  2. Add the path for the actual code (i.e. the library)

How to Add an External C++ Library to Your Project Using the CodeLite IDE

Step 1: Go to the website of the library.
For example, for the linear algebra library, Eigen, you go to this page: Eigen Main Page

Step 2: Download the zip file that contains all the code.

Step 3: Unzip the zip file to your computer.

Step 4: Open CodeLite (i.e. your IDE)

Step 5: Open a new Project

Step 6: Right click on project name and click on Settings

Step 7: Click the Compiler tab and add the Include Paths:
e.g. the folder that contains the folder named ‘Eigen’…C:\XYZ\eigen-eigen-21301928\
This is where the compiler can find the .h (i.e. header) files

Step 8: Click Linker and add the Libraries Search Path
e.g. C:\XYZ\eigen-eigen-21301928\
The path above needs to be the location where the linker can find the libraries (usually suffixed with .a, .dll, .lib, .so)

  • Static Libraries are – XYZ.lib for Windows, UNIX/Linux/Max – libXYZ.a
  • Dynamic Libraries are – XYZ.dll for Windows, Unix/Linux/Mac – libXYZ.so

Step 9: Go to main.cc (i.e. your source code file…could also be main.cpp) and add the preprocessor directives at the top of the source file. You can use this code to test that everything is setup properly.

#include <iostream>
#include <Eigen/Dense>

using Eigen::MatrixXd;

int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
}

Step 10: That’s it. You are ready to rock and roll!

How to Add an External C++ Library to Your Project Using the Visual Studio IDE

Step 1: Go to the website of the library.
For example, for the linear algebra library, Eigen, you go to this page: Eigen Main Page

Step 2: Download the zip file that contains all the code.

1-zip_fileJPG

Step 3: Unzip the zip file to your computer.

2-unzipJPG

Step 4: Move the folder to some directory on your computer.

I’ll move it to the C:\libraries directory. At this stage, Eigen is along the following path for me:

C:\libraries\eigen-3.3.7\eigen-3.3.7\Eigen

3-directoryJPG

Step 5: Open Visual Studio.

Step 6: Create a new C++ project.

Step 7: Write the following code into a new Source file.

I’ll name this file main.cpp.

#include <iostream>
#include <Eigen/Dense>
 
using Eigen::MatrixXd;
 
int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
}

Step 8: Click on the Project tab at the top and go to Additional Include Directories.

Project -> Properties -> C/C++ -> General (you can find it by expanding the little triangle next to C/C++) -> Additional Include Directories

4-additional-includeJPG

Step 9: Click the blank next to “Additional Include Directories”.
A small triangle dropdown should appear.

Step 10: Click <…Edit>.

Step 11: Click the Add New Line icon.

5-add-new-lineJPG

Step 12: Add the path to the folder containing Eigen

If you click the blank, an icon with three periods “…” should appear. Click that icon and add the path to the folder that contains Eigen.

In my case, the path is:

C:\libraries\eigen-3.3.7\eigen-3.3.7\

because the actual Eigen library is this folder:

C:\libraries\eigen-3.3.7\eigen-3.3.7\Eigen

Step 13: Click OK.

6-after-includeJPG

Step 14: Click Apply and then OK.

7-apply-then-okJPG

Step 15: Go to main.cpp (or whatever the name of the source code you wrote earlier is).

8-write-the-codeJPG

Step 16: Compile and Run the Code.

To compile the code, go to Build -> Build Solution.

Then to run the code, go to Debug -> Start Without Debugging.

Here is the output you should see:

9-outputJPG

That’s it! You’re all good to go.

How to Install and Launch ROS2 Using Docker

In this tutorial, we will install and launch ROS2 using Docker within Ubuntu Linux 20.04. To test our installation, we will launch and run the popular Turtlebot3 software program.

What is Docker?

You can think of a Docker container on your PC as a computer inside your computer.

Docker is a service that provides portable stand-alone containers that come bundled with pre-installed software packages. These containers  make your life easier because they come with the right software versions and dependencies for whatever application you are trying to run.

Docker makes a lot more sense when you actually use it, so let’s get started.

Install Docker

Go to this link to download the appropriate Docker for your system.

Install Docker on your computer. Since I’m using Ubuntu 20.04 in Virtual Box on a Windows 10 machine, I’ll follow the Ubuntu instructions for “Install using the repository.”

If you’re using a Mac or Windows, follow those instructions. I actually find it easier to use Ubuntu, so if you’re using Windows, I highly suggest you do this tutorial first to get Ubuntu up and running, and then come back to this page.

Pull and Start the Docker Container With ROS2

Open a new terminal window, and create a new folder.

mkdir new_folder

Let’s pull a docker container. This one below comes with ROS2 already installed.

sudo docker pull /tiryoh/ros2-desktop-vnc:foxy

Now we need to start the docker container and mount the folder on it (the command below is all a single command).

sudo docker run -it -p 6080:80 -v /new_folder --name ros2_new_folder tiryoh/ros2-desktop-vnc:foxy

Note that ros2_new_folder is the name of the container.

Type the following in any browser: http://127.0.0.1:6080/

Here is the screen you should see:

1-screen-you-should-seeJPG

Install TurtleBot3

Click the menu icon in the very bottom left of the container.

Go to System Tools -> LX Terminal.

Download TurtleBot3.

mkdir -p ~/turtlebot3_ws/src
cd ~/turtlebot3_ws
wget https://raw.githubusercontent.com/ROBOTIS-GIT/turtlebot3/ros2/turtlebot3.repos
vcs import src < turtlebot3.repos

Wait a while while TurtleBot3 downloads into the container.

2-wait-a-whileJPG

Compile the source code.

colcon build --symlink-install

Wait for the source code to compile.

3-source-code-compileJPG

Let’s set the environment variables. Type these commands, one right after the other.

echo 'source ~/turtlebot3_ws/install/setup.bash' >> ~/.bashrc
echo 'export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:~/turtlebot3_ws/src/turtlebot3/turtlebot3_simulations/turtlebot3_gazebo/models' >> ~/.bashrc
echo 'export TURTLEBOT3_MODEL=waffle_pi' >> ~/.bashrc
source ~/.bashrc

Launch an Empty World in Gazebo in ROS2

Now we will launch the simulation using the ros2 launch command.

ros2 launch turtlebot3_gazebo empty_world.launch.py

It will take a while to launch the first time, so be patient. You might see a bunch of warning messages, just ignore those.

When everything is done launching, you should see an empty world like this.

4-empty-gazebo-worldJPG

Go back to the terminal window by clicking the icon at the bottom of the screen.

Press CTRL+C to close it out.

For more simulations, check out this link at the TurtleBot3 website.

How to Stop the Docker Container

To stop the Docker container, go back to Ubuntu Linux and open a new terminal window. Type the following code.

sudo docker stop [name_of_container]

For example:

sudo docker stop ros2_new_folder

How to Restart the Docker Container

If you ever want to restart it in the future, you type:

sudo docker stop [name_of_container]

For example:

sudo docker restart ros2_new_folder

Type the following in any browser. http://127.0.0.1:6080/

That’s it.

How To Install Ubuntu and Raspbian on Your Raspberry Pi 4

In this tutorial, we will set up a Raspberry Pi 4 with both the Ubuntu 20.04 and Raspbian operating systems.

You Will Need

2020-08-29-150822

This section is the complete list of components you will need for this project.

Install Ubuntu

Prepare the SD Card

Grab the USB MicroSD Card Reader.

2020-08-29-151538

Take off the cap of the USB MicroSD Card Reader.

2020-08-29-151621

Stick the MicroSD card inside the Card Reader.

Stick the Card Reader into the USB drive on your computer.

Download the Raspberry Pi Imager for your operating system. I’m using Windows, so I will download Raspberry Pi Imager for Windows.

Open the Raspberry Pi Imager. Follow the instructions to install it on your computer.

When the installation is complete, click Finish.

Open the CHOOSE OS menu.

Scroll down, and click “Ubuntu”.

Select the Ubuntu 20.04 download (32-bit server).

Click CHOOSE SD. 

Select the microSD card you inserted. 

Click WRITE, and wait for the operating system to write to the card. It will take a while so be patient.

While you’re waiting, grab your Raspberry Pi 4 and the bag of heat sinks.

2020-08-29-154338
2020-08-29-154824

Peel off the backup of the heat sinks, and attach them to the corresponding chips on top of the Raspberry Pi.

2020-08-29-154344

Grab the cooling fan.

2020-08-29-155325

Connect the black wire to header pin 6 of the Raspberry Pi. Connect the red wire to header pin 1 of the Raspberry Pi.

2020-08-29-155750

Install the Raspberry Pi inside the case.

2020-08-29-155840

Connect the PiSwitch to the USB-C Power Supply. It should snap into place.

2020-08-29-160447

Once the installation of the operating system is complete, remove the microSD card reader from your laptop.

Set Up Wi-Fi

Reinsert the microSD card into your computer.

2020-08-29-161429

Open your File Manager, and find the network-config file. Mine is located on the F drive in Windows.

Open that file using Notepad or another plain text editor.

Uncomment (remove the “#” at the beginning) and edit the following lines to add your Wi-Fi credentials (don’t touch any of the other lines):

wifis:
  wlan0:
  dhcp4: true
  optional: true
  access-points:
    <wifi network name>:
      password: "<wifi password>"

For example:

wifis:
  wlan0:
  dhcp4: true
  optional: true
  access-points:
    "home network":
      password: "123456789"

Make sure the network name and password are inside quotes.

Save the file.

Set Up the Raspberry Pi

Safely remove the microSD Card Reader from your laptop.

Remove the microSD card from the card reader.

Insert the microSD card into the bottom of the Raspberry Pi.

Connect a keyboard and a mouse to the USB 3.0 ports of the Raspberry Pi.

2020-08-29-165525

Connect an HDMI monitor to the Raspberry Pi using the Micro HDMI cable connected to the Main MIcro HDMI port (which is labeled HDMI 0).

Connect the 3A USB-C Power Supply to the Raspberry Pi. You should see the computer boot.

Log in using “ubuntu” as both the password and login ID. You will have to do this multiple times.

You will then be asked to change your password.

Type:

sudo reboot

Type the command: 

hostname -I 

You will see the IP address of your Raspberry Pi. Mine is 192.168.254.68. Write this number down on a piece of paper because you will need it later.

Now update and upgrade the packages.

sudo apt update
sudo apt upgrade

Now, install a desktop.

sudo apt install xubuntu-desktop

Installing the desktop should take around 20-30 minutes or so.

Once that is done, it will ask you what you want as your default display manager. I’m going to use gdm3.

Wait for that to download.

Reboot your computer.

sudo reboot

Your desktop should show up.

Type in your password and press ENTER.

Click on Activities in the upper left corner of the screen to find applications.

If you want to see a Windows-like desktop, type the following commands:

cd ~/.cache/sessions/

Remove any files in there.

Type:

rm 

Then press the Tab key and press Enter.

Now type:

xfdesktop

Connect to Raspberry Pi from Your Personal Computer

Follow the steps for Putty under step 9b at this link to connect to your Raspberry Pi from your personal computer.

Install Raspbian

Now, we will install the Raspbian operating system. Turn off the Raspberry Pi, and remove the microSD card.

Insert the default microSD card that came with the kit.

Turn on the Raspberry Pi.

You should see an option to select “Raspbian Full [RECOMMENDED]”. Click the checkbox beside that.

Change the language to your desired language.

Click Wifi networks, and type in the password of your network.

Click Install.

Click Yes to confirm.

Wait while the operating system installs.

You’ll get a message that the operating system installed successfully.

Now follow all the steps from Step 7 of this tutorial. All the software updates at the initial startup take a really long time, so be patient. You can even go and grab lunch and return. It might not look like the progress bar is moving, but it is.

2020-08-29-212546

Keep building!