Our C++ and vcpkg library

Our C++ and vcpkg library

Vcpkg is an open-source package manager maintained by Microsoft that allows you to install C++ libraries. If you've never heard of vcpkg, neither had we. One of our users introduced us to this project. Let's take a look at what it's all about.





As we said, we discovered this project when a user started porting our C++ library to this package manager. As usual, we looked into whether we could make life easier for our users by adding our library to vcpkg, and even though we're not completely convinced by vcpkg, we decided to publish our library on this manager.

What is vcpkg?

Vcpkg is a command-line tool that allows you to download and compile the C++ libraries used in a project. It's similar to Npm for JavaScript or Pip for Python.

Although it is maintained by Microsoft, vcpkg is cross-platform and works on Windows, Linux, and macOS. It can be integrated with CMake, Visual Studio, and Visual Studio Code.

How to install vcpkg?

To install vcpkg, you need to clone the Git repository, run the bootstrap-vcpkg command, and add vcpkg to your PATH.

git clone https://github.com/microsoft/vcpkg.git .\bootstrap-vcpkg.bat set "VCPKG_ROOT=C:\path\to\vcpkg" set PATH=%VCPKG_ROOT%;%PATH%


You must also have CMake and a C++ compiler installed.

A small project

To illustrate how to use vcpkg, we take the example from the vcpkg documentation, but we use our C++ library and the code from our tutorial.

The code in the example simply displays the Yoctopuce modules connected to the USB ports.

#include "yocto_api.h"

using namespace std;

int main(int argc, const char * argv[])
{
  string      errmsg;

  // Sets up the API to use local USB devices
  if(YAPI::RegisterHub("usb", errmsg) != YAPI::SUCCESS) {
    cerr << "RegisterHub error: " << errmsg << endl;
    return 1;
  }

  cout << "Device list: " << endl;

  YModule *module = YModule::FirstModule();
  while (module != NULL) {
    cout << module->get_serialNumber() << " ";
    cout << module->get_productName()  << endl;
    module = module->nextModule();
  }
  YAPI::FreeAPI();
  return 0;
}


We then use vcpkg to create a new project and add a dependency to our yoctolib library.

C:\tmp\helloworld>vcpkg new --application C:\tmp\helloworld>vcpkg add port yoctolib


Next, we need to create the CMakeLists.txt file that is used by CMake

cmake_minimum_required(VERSION 3.10) project(HelloWorld) find_package(yoctolib CONFIG REQUIRED) add_executable(HelloWorld helloworld.cpp) target_link_libraries(HelloWorld PRIVATE yoctolib::YoctoLib)


Finally, all that remains to do is to compile and execute the project using CMake:

C:\tmp\helloworld>cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/[path_to_vcpkg/scripts/buildsystems/vcpkg.cmake C:\tmp\helloworld>cmake --build build C:\tmp\helloworld>.\build\Debug\HelloWorld.exe Device list: YPWMRX01-AE391 Yocto-PWM-Rx RELAYLO1-27EAB Yocto-Relay YBUTTON1-2072D Yocto-Knob YPWMTX01-B9625 Yocto-PWM-Tx


Conclusion

As we said, we are not completely convinced by vcpkg. We prefer to manage the installation and compilation of our libraries manually.

But the purpose of our programming libraries is to facilitate the integration of our modules into our customers' applications. That is why we have added our library to the vcpkg repository.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.