Installing the C++ library with CMake

Installing the C++ library with CMake

This week, we are going to show you how to compile our C++ programming library under Linux and to install it as a system library. This installation is very simple thanks to cmake.

First of all, let's have a short reminder of C++ and of the different steps of the life of a program written in C++.


The first step consists in compiling the source files, the .cpp and .h files, to generate .o object files containing the binary code that the machine is able to run. During this phase, the compiler needs the .cpp file, but it also needs to have access to all the .h files which are referenced in the .cpp file. Under Linux, headers are usually stored in the /usr/include and /usr/local/include directories, but there is an option for the compiler to add other directories to this list.


The second step is linking, consisting in taking all the .o files and the external libraries to form the executable file. During this step, the linker checks that all the parts are compatible with one another and each referenced function or object is available. Finally, it generates the executable file.

The linker has two ways to use an external library. It can integrate it into the executable file (static edition) or it can store a reference to the library (dynamic edition). With static edition, the libraries are included into the executable file. The generated program is therefore larger, but it contains all the code necessary for its execution.

With dynamic edition, the executable file includes only a reference to the library. The executable file is smaller, but when the program is running the library must be installed on the machine.

Under Linux, the (dynamic or static) libraries are usually stored in the /lib and /usr/lib directories, but there is an option for the linker to look for libraries in other directories.


When executing, the operating system checks that the dynamic libraries referenced by the executable file are available in the /lib or /usr/lib directories. If it is the case, it adds them dynamically and the execution of the program can go on. Otherwise, the application crashes with a "cannot open shared object file: No such file or directory" message.

Why install our library globally?

The advantage of installing the static version of our library is to simplify the build script. When it is globally installed, all the tools, compilers, linkers, and so on have access to the library. You don't need anymore to specify in you project where the files of our library are located. It's especially useful with docker and cross compilation tools.

The advantage of installing the dynamic version of our C++ library is almost non-existent. Theoretically, this enables you to spare space of the hard disk if several programs use the same library, because all the programs don't need to include the code of the library anymore. The counterpart is that the code works only on machines which have this library installed.

Installing the static library under Linux

After this lengthy introduction, here is how to install the Yoctopuce library on your Linux system. The simplest solution is to use cmake and to run the following commands at the root of the Yoctopuce library.

mkdir build_static cd build_static cmake ../Sources/ cmake --build . sudo cmake --install .

The first two commands create a build_static temporary directory which is used by cmake. We must then create the cmake project by passing it the source files of the Yoctopuce library, and by compiling it with the cmake --build command. These commands only compile the library for the machine architecture, but the library is not yet installed into the correct directory.

The last command, sudo cmake --install ., installs the static library and the headers into the standard directories of your Linux installation. By the way, this is why you must run this command with sudo, as the directories are protected.

When the static library is installed, you don't need to specify the library and header path. All the compilers and linkers can use the Yoctopuce C++ programming library.

Installing the dynamic library under Linux

If you want to install the dynamic library, you must simply add the -DBUILD_SHARED_LIBS=ON option when generating the cmake project.

mkdir build_dynamic cd build_dynamic cmake ../Sources/ -DBUILD_SHARED_LIBS=ON cmake --build . cmake --install .


You now know how to easily compile and install the Yoctopuce C++ programming library. If you use docker, this allows you to simplify the installation of the library into your containers.

For other situations, we still recommend to include the source files of our library into your project. The advantage of this solution is that it gives you full control over compilation options. Moreover, the final executable file contains everything needed to use the Yoctopuce modules and will work on all the machines.

Incidentally, we have a post explaining how to add the source files from our library into most IDE.

Add a comment No comment yet Back to blog

Yoctopuce, get your stuff connected.