Adding the C++ library to your IDE in 2021

Adding the C++ library to your IDE in 2021

This week, we update a post dating from 2013: "Include the C++ library into your project". Since 2013 XCode and Visual Studio have evolved and new IDEs are present. So, here is how to include our C++ library in the main IDEs in 2021.





Before we see how to create a C++ project and include our library, we remind you that in the Examples sub-directory you can find many examples of use of our modules, including projects already configured for Visual Studio, Xcode, CMake, and Code::Block. If you start from scratch, it's certainly simpler and faster to open one of these examples and to modify the code. However, if you want to start from zero or if you want to add our library to an existing project, here is how to add the sources of our library in the most popular IDEs.

For this post, we are going to create an command line application in C++ which is simply going to display on the standard output the list of all the Yoctopuce modules connected on the USB ports. We are going to create a new C++ project, include the source files of the Yoctopuce library, and write the few lines of code of the main function. Here is the code of our small test program.

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

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



Xcode 12

Xcode changed a lot since 2013, but the process to add the Yoctopuce library remains the same. When creating the project, you must make sure to select C++ as language. To include the source files of our library in the project, the simplest solution is to drag-and-drop all the files of the library Sources directory directly in the Xcode project explorer. This triggers a wizard which adds the files to the project. Don't forget to select your application in the "add to target" field of this wizard, otherwise Xcode includes the files but doesn't compile them. You don't need to change the include path (Xcode does it for you). With the latest versions of Xcode, you don't need to link the code with the CoreFundation.framework and IOKit.framework frameworks. Xcode does it automatically.

  



Visual Studio C++ 2019

With version 2019 of Visual Studio, things have also become a little simpler. You don't need to change the parameters of precompiled header. To add files to the project, the simplest method is to drag-and-drop the .h, .cpp, and .c files directly into Visual Studio. Beware, if you add .rc and .txt files, they are going to be compiled and will generate a compilation error.

Visual Studio doesn't automatically update the include path. Therefore you must manually add the directory where the Yoctopuce library sources are located in the project properties. To do so, you must access the project properties, and in section VC++ Directories add the Sources directory of our library to the Include Directories property. Note, if you project has several configurations, for example for 32bit and 64bit compilation, you must change the include path for both configurations.

  



CLion

Internally, CLion uses CMake to manage the build process. As our library is compatible with CMake, you only need to reference in the main CMakeLists.txt file our CMakeLists.txt file which is located in the Sources directory. We have a post on this topic which explains the whole process: "Compiling the C++ library with CMake".

  



Code::Blocks

For Linux, we support Code::Blocks since version 10. Before creating the project, you must make sure that the libUSB 1.0 with its headers is also installed. If you use a Debian-based distribution (Ubuntu, Raspian, and so on), you only have to run "sudo apt-get install libusb-1.0-0-dev".

After having created a "command line" project with Code::Block, you must add the source files of the Yoctopuce library. To include those files, the simplest way is to use the "add files recursively..." function and to select only the required extensions (.h, .cpp, and.c). You must also add the directory where the sources of the Yoctopuce library are located to the project include path. Finally, you must inform the linker that you need the usb-1.0 and pthread libraries.

  



C++ Builder XE4

As with Visual Studio, the simplest is to drag-and-drop the files directly in the project (here as well, don't include the .rc files). No need to modify the include path, C++ Builder takes care of it all.

  



Comments

In this post, we added all the C++ files which are included in the library. This guaranties that you can use in your code all the functions of all the modules. If you want to lower compilation time, you can include only the files for the functions that you use. You must minimally include yocto_api.cpp, and then the yocto_xxx.cpp file corresponding to the function that you want to use. For example, if you use only a temperature sensor and a relay, you must include yocto_api.cpp, yocto_temperature.cpp, and yocto_relay.cpp.

Conclusion

We hope that this post helps you integrate our C++ library more easily into your projects. As usual, you can contact support (support@yoctopuce.com) or post a comment if you have trouble integrating our library into your projects.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.