Compiling the C++ v2.0 library

Compiling the C++ v2.0 library

A few weeks ago, we released a new version of our C++ library. The main new feature is SSL/TLS encryption for communications. This new feature changes the way to compile our C++ library with your project. Let's take a look at what's new.






Instead of rewriting SSL/TLS support, we decided to use the Mbed TLS library. As the saying goes, "there's no point in reinventing the wheel", and this is particularly true of cryptographic libraries.

In the course of this integration, we modified the tree structure of our source files, and therefore the way we compile our library.

To keep this post relatively short, we won't explore in details how to configure each IDE to compile the C++ library in all the available IDEs. Instead, we'll focus on the steps required to compile our library and add it to a project.

If you've never used our C++ library before, we recommend you start with these three posts:



Compiling the library without SSL/TLS

It's possible to compile the library without including the cryptography part and the Mbed TLS library. This solution requires the fewest changes to your existing projects. The only change required is to define the NO_YSSL macro in your makfile or IDE.

All code using the Mbed TLS library is encapsulated into #ifdef and #ifndef directives. This allows our library to be used as before.

Here is an extract from the source code of our library. If the NO_YSSL macro is defined, the SSL/TLS part is not compiled.

int yTcpInitMulti(char *errmsg)
{
    int res;
    res = yTcpInitBasic(errmsg);
#ifndef NO_YSSL
    if (!YISERR(res)) {
        res = yTcpInitSSL(errmsg);
    }
#endif
    return res;
}



All compilers and IDEs can globally configure a macro/definition to be passed to the preprocessor. For example, GCC uses the -D option.

In Visual Studio, this can be done in the project properties.

Defining NO_YSSL in a Visual Studio project
Defining NO_YSSL in a Visual Studio project



In summary, to compile the library without SSL/TLS support, you need to add the Sources directory to your include path so that the compiler can find the yocto_xxx.h files.

You also need to define the NO_YSSL macro and compile the following files:

  • C++ files used by your program in the Sources directory
  • All C files in the Sources/yapi directory


Compiling the library with SSL/TLS

To compile the library with SSL/TLS support, you need to include the Mbed TLS library files in addition to our "traditional" library files. These files are included in our C++ library archive in the Sources/yapi/mbedtls subdirectory.

So, to compile the library, you also need to add the following directories to your include path:

  • Sources directory
  • Sources/yapi/mbedtls/include directory


The following files must then be compiled:

  • C++ files used by your program in the Sources directory
  • All C files in the Sources/yapi directory
  • All C files in the Sources/yapi/mbedtls/library directory


Finally, under Windows only, you must also add the bcrypt.lib library during the linking phase.

In Visual Studio, this option is called "Additional Dependencies" and is accessible in the project properties.

Under Windows, link with the bcrypt.lib library
Under Windows, link with the bcrypt.lib library



Supported environments


The compilers supported and tested are GCC, Clang and Microsoft C++ (MSVC), but normally any C++ compiler should work.

As always, the Examples subdirectory of our C++ library contains a wealth of examples, so you don't have to start from scratch.

Each example contains a Visual Studio project, Xcode, Code::Blocks, and a GNU makefile. There's also a Prog-CMake example using CMake. In the vast majority of cases, you should find a working example that uses your IDE or build environment.

We've tried to keep it as portable as possible, but if you can't compile the library in your IDE, don't hesitate to contact Yoctopuce support (support@yoctopuce.com).

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.