C++ (Windows)

You are currently viewing the documentation for the latest version (3.0.0). To access a different version, click the "Switch version" button located in the upper-right corner of the page.

■ If you are not sure which version of the product you are currently using, please feel free to contact Mech-Mind Technical Support.

This topic introduces how to use the C++ samples of Mech-DLK SDK on Windows. For sample descriptions, refer to Sample Usage Guide.

Installation Package Contents

The following describes the C++ sample files in the Mech-DLK SDK installation package:

Contents Description

cpp/bin

Dependent third-party libraries (.dll files and the hasp_rt.exe file) and C++ example executable files (which can be run directly)

cpp/docs

C++ interface documentation

cpp/examples

C++ example programs

cpp/include

C++ header files

cpp/lib

C++ library files

cpp/Licenses

Dependency library license file

cpp/resources

Files needed to run samples such as model packages and test images

  • The generated example executable file is stored in the cpp/bin directory. To modify the code or re-compile a sample, refer to the compilation and execution steps for that specific sample.

  • If the file or directory name used to run or compile the sample contains Chinese, find the Language and Region option in Windows Settings, open Beta: Use Unicode UTF-8 for worldwide language support, and then restart your computer.

Prerequisites

To use the C++ samples in Mech-DLK SDK, the following software must be installed:

Install Mech-DLK SDK

Obtain the latest version of Mech-DLK SDK from Installation Guide and complete installation.

Install CMake

  1. Download CMake Installation Package: Select the installation package that corresponds to Windows x64 Installer.

  2. During installation, it is recommended to select Add CMake to environment variables.

    cmake 1

Before you compile C++ samples, ensure that CMake 3.10 or above is installed.

Download Visual Studio

  1. Download the Visual Studio installation package.

  2. During installation, select Desktop development with C++ in the Desktop & Mobile category. Then, click Install in the lower-right corner.

    sample c workload
  • It is recommended to use Visual Studio 2019 or above. It is recommended to use Release and x64 configurations when compiling and running samples.

Install OpenCV (Optional)

If you need to compile and run the OpenCV samples, download and install OpenCV.

This sample uses OpenCV 4.12.0 as an example. If you are using other versions, adjust the configuration according to the actual situation.

Install HALCON (Optional)

If you need to compile and run HALCON samples, install HALCON first and configure the corresponding License.

Sample Adjustment Instructions

Before compiling and running various C++ samples, you can adjust the input images, model packages, and related resource files according to your actual business needs. The following content provides examples of common adjustment items.

  • Input image: Replace the image to be detected, or modify the image reading path according to actual requirements.

  • Model package: A defect segmentation model package is provided by default in the installation package. You can replace it with the one you need according to your actual business needs.

  • Output: Adjust how detection results are output based on project requirements, including modifying the save path, file name, or output location of result images.

  • Resource files: You can add or replace images, model packages, and related resource files required by the business in the cpp/resources directory.

Compile and Run Samples

After completing relevant adjustments, you can follow these steps to build and run the sample.

Basic Sample

Compile the Sample

  1. Open CMake, select the directory containing the build script in the Source directory, i.e., cpp/examples, and select or enter the directory used to generate the project in the build directory, such as cpp/build.

    Where is the source code

    xxx\cpp\examples

    Where to build the binaries

    xxx\cpp\build

  2. Click Configure. If the build directory does not exist when you configure it for the first time, please click Yes in the pop-up dialog box to create a directory.

    create build cpp

    On the configuration page, select the installed version of Visual Studio, set the platform to x64, and then click Finish.

    configuration

    When the configuration completes, the statement Configuring done will appear in the last line of the log.

    cpp windows configuring done
  3. In the CMake configuration item, confirm that the SDK path is configured correctly:

    • DL_SDK_INCLUDE_PATH points to the cpp/include directory (used to locate header files).

    • DL_SDK_LIB_PATH points to the cpp/lib directory (used to the link library files).

      If the path is not configured correctly, the compilation may fail. Click Configure again to confirm the modification and then proceed to the next step.

  4. Click Generate. When the generation completes, the statement Generating done will appear in the last line of the log. Then, click Open Project to open the solutions in Visual Studio.

    cpp windows generating done
  5. On the toolbar of Visual Studio, set Solution Configuration to Release, and set Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

    generate solution
  6. After the compilation, you can see the generated example_basic.exe file in the build directory (i.e., cpp/build/Release).

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where example_basic.exe is stored.

  2. Copy the resources folder in the cpp/resources folder to the same directory as example_basic.exe.

    copy resources and dlls
  3. Double-click the generated example_basic.exe file in the cpp/build/Release directory to run the sample. If you want to run the sample in Visual Studio, set the example_basic project as a startup project, and then click Local Windows Debugger to run the sample.

    run basic sample

The cpp/resources directory contains the model and resource files needed for the sample to run. Ensure that they are in a location that is properly accessible to the program.

Advanced Sample (Multi-Thread Inference)

Compile the Sample

The compilation of the multi-thread inference sample is consistent with that of the Basic sample. After finishing compiling the Basic samples, you can find the example_advanced_multi_thread_infer.exe file in the cpp/build/Release directory.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where example_advanced_multi_thread_infer.exe is stored.

  2. Copy the resources folder in the cpp/resources folder to the same directory as example_advanced_multi_thread_infer.exe.

  3. In the cpp/build/Release directory, double-click the generated example_advanced_multi_thread_infer.exe file to run the sample. If you want to run the sample in Visual Studio, set the example_advanced_multi_thread_infer project to be a startup project, and then click Local Windows Debugger to run the sample.

    run multi thread sample
By default, the multi-thread inference sample uses an inference engine that is independent of each thread. If multiple threads share the same inference engine, ensure the safety of concurrent use of multiple threads.

Advanced Sample (OpenCV)

Compile the Sample

  1. Follow Compile Basic Samples steps to complete the initial configuration.

  2. Enable USE_OPENCV on the CMake configuration page, and enter OpenCV_INCLUDE_DIR and OpenCV_LIB_DIR paths.

    opencv cmake configuration
  3. After completing the configuration, click Configure again. After confirming that there is no new configuration item in red, click Generate.

  4. When the generation completes, the statement Generating done will appear in the last line of the log. Then, click Open Project to open the solutions in Visual Studio.

  5. On the toolbar of Visual Studio, set Solution Configuration to Release, and set Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

  6. After the compilation, you can see the generated example_advanced_infer_with_opencv.exe file in the cpp/build/Release directory.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory and paste them to the directory where example_advanced_infer_with_opencv.exe is stored.

  2. Copy the resources folder in the cpp/resources folder to the same directory as example_advanced_infer_with_opencv.exe.

  3. Copy and paste the dependency libraries related to the OpenCV runtime to the same directory as example_advanced_infer_with_opencv.exe.

  4. Double-click the generated example_advanced_infer_with_opencv.exe file in cpp/build/Release to run the sample. If you want to run the sample in Visual Studio, set the example_advanced_infer_with_opencv project as a startup project, and then click Local Windows Debugger to run the sample.

    run opencv sample

Advanced Sample (HALCON)

Compile the Sample

  1. Follow Compile Basic Samples steps to complete the initial configuration.

  2. Enable USE_HALCON on the CMake configuration page, and enter HALCON_INCLUDE_DIR and HALCON_LIB_DIR paths.

    halcon cmake configuration
  3. After completing the configuration, click Configure again. After confirming that there is no new configuration item in red, click Generate.

  4. When the generation completes, the statement Generating done will appear in the last line of the log. Then, click Open Project to open the solutions in Visual Studio.

  5. On the toolbar of Visual Studio, set Solution Configuration to Release, and set Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

  6. After the compilation, you can see the generated example_advanced_infer_with_halcon.exe file in the cpp/build/Release directory.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory and paste them to the directory where example_advanced_infer_with_halcon.exe is stored.

  2. Copy the resources folder in the cpp/resources folder to the same directory as example_advanced_infer_with_halcon.exe.

  3. In the cpp/build/Release directory, double-click the generated example_advanced_infer_with_halcon.exe file to run the sample. If you want to run the sample in Visual Studio, set the example_advanced_infer_with_halcon project as a startup project, and then click Local Windows Debugger to run the sample.

    run halcon sample

FAQs

Compilation Issues

Could Not Find Header Files or Library Files for OpenCV or HALCON

  • Problem: When compiling OpenCV or HALCON samples, an error message saying that no header files or library files related to third-party libraries were found.

  • Solution: Make sure that USE_OPENCV or USE_HALCON is enabled properly during the configuration phase of CMake, and that the corresponding header and library files are filled in correctly.

Could Not Open SDK Header File

  • Problem: An error occurred during compilation, indicating that the SDK header file could not be opened.

  • Solution: Check and fix DL_SDK_INCLUDE_PATH and DL_SDK_LIB_PATH in the CMake configuration entries to ensure that they point to cpp/include and cpp/lib respectively, and then click Configure again to generate a solution.

Operation Issues

Missing .dll Errors

  • Problem: The program failed to run, indicating that a .dll file is missing.

  • Solution: Confirm that the hasp_rt.exe file and all .dll files in the cpp/bin directory are copied to the directory where the generated executable file is stored. If it is an OpenCV or HALCON sample, also ensure that the .dll files that the corresponding third-party libraries depend on can be found by the system normally.

Missing .dll File in OpenCV Sample

  • Problem: When running the OpenCV sample, an error message saying that no OpenCV-related .dll file could be found.

  • Solution: Copy the dependency libraries related to the OpenCV runtime and paste them to the directory where example_advanced_infer_with_opencv.exe is stored.

Missing Resources or Model Error

  • Problem: A resource or model was missing when the program was running.

  • Solution: Confirm that the resources folder in the cpp/resources directory has been copied to the peer directory of the generated executable file and that it contains the model and resource files needed to run the sample.

Is this page helpful?

You can give a feedback in any of the following ways:

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.