Include the C++ library into your project

Include the C++ library into your project

Yoctopuce libraries include multiples examples showing how to use our devices. In the C++ libary we also include binaries for all supported OS, Makefiles and preconfigured project for all supported IDE. So you can modify an recompile our examples very easily. But we never explained how to create such projects from scratch. For some environments this is very easy and intuitive, but for some other you need to configure a few more things. This week we will show you how to integrate our C++ library from source into an IDE project created from scratch.


We have an updated article that cover the more recent version of IDE: Adding the C++ library to your IDE in 2021.

The example project will be a simple command line application that lists on the standard output all Yoctopuce modules connected on USB. For this we will create a brand new project, import all sources files of our library and write few lines of code that will use the library.


Here are the few lines to copy into the main function.

// Setup 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

For XCode, you need to create an OSX C++ project (we will document this for Objective-C in another post) and include all source files. The easiest way is to drag and drop all files of the Sources folder into project Navigator of XCode directly from the Finder. That will pop up a wizard to import files into the project. At this point you will need to check the application name into the "add to target" field. Failure to do this would include the files into the project but without actually compiling them.
You do not need to update the include path because the Wizard has already do that for you. But you need to add two frameworks to the project (CoreFundation.framework and IOKit.framework).


  



Visual Studio C++ 2010

With Visual Studio 2010, things are a bit more complex. First our library is not designed to be used with "precompiled headers". You need to create a project that do not use "precompiled headers". If you want to integrate our library with an existing project that use "precompiled headers" you will need to disable this feature for all .cpp and .c files of our library.

The easiest way to import sources files is to drag and drop .h, .cpp and .c files of the library into the project. Be careful not to add .rc files, as they would be compiled (unlike XCode) and would generate error during the compilation. Visual Studio does not automatically update the include path, so you will need to add the Sources folder into the the include path of the project (this can be done under project ->properties).

  



Code::Blocks

Under Linux we support Code::Blocks since version 10. Before creating the project, you have to ensure that the libUSB 1.0 (with headers) is installed on your computer. If you are using a Debian based distribution (Unbuntu, Raspian, etc.) you only have to execute "sudo apt-get install libusb-1.0-0-dev".

After creating a new command line project, you will need to add sources files from the Yoctopuce library. To do that, the easiest way is to use the "add files recursively.." function. You need to select only .h, .cpp and .c files. You need to update the include path of the project to include the Sources folder of the library. The last step is to specify that the linker need to add usb-1.0 and pthread libraries.

  



Visual Studio 2012 C++ for Windows Desktop


If you are want to use Visual Studio 2012, the steps are exactly the same as Visual Studio C++ 2010.

C++ Builder XE4

The current published C++ library does not compile with C++ Builder, but some customers have requested support for this IDE. So the next release of the library will work with C++ Builder. If you want to test it already you can download this beta version. Like XCode and Visual Studio, the best way to add the sources files is to drag and drop .h, .cpp and .c files. Like with Visual Studios, do not include .rc file or it will generate errors. You do not need to update the include path, this has already be done for you.
EDIT:the new release of the C++ library (build 12553) work now with C++ builder.

  



We hope that will help you to integrate our C++ library to your project. As usual if you have any trouble with our library you can send us an email to support@yoctopuce.com or post a comment.




1 - quickly Friday,july 19,2013 7H27

Thank you for this - I have used the beta new version with C++ Builder, and it has allowed me to finish and ship my factory tester for a new product!

Thank you for your help and support and porting to C++ Builder.

Yoctopuce, get your stuff connected.