Resource Base
Table of contents

Thanks for downloading Dynamsoft Barcode Reader Package!

Your download will start shortly. If your download does not begin, click here to retry.

User Guide for C++ Language

In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using C++ language.


  • Operating System:
    • Windows 7, 8, 10, 2003, 2008, 2008 R2, 2012, 2016
    • Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc
    • Linux arm 32bit
    • Linux arm 64bit (not included in the trial package, click here to download the SDK)
    • MacOS 64bit: 10.12+ (not included in the trial package, contact us to get the SDK)
  • Developing Tool
    • Visual Studio 2008 or above
    • G++ 5.4+


If you haven’t downloaded the SDK yet, download the C/C++ Package now from Dynamsoft website and unpack the package into the directory of your choice.

For this tutorial, we unpack it to [INSTALLATION FOLDER], change it to your unpacking path for the following content.

Build Your First Application

Let’s start by creating a console application which demonstrates how to use the minimum code to read barcodes from an image file.

You can download the entire source code and compiled program from Here.

Create a New Project

For Windows

  1. Open Visual Studio. Go to File > New > Project, create a new Empty Project and set Project name as DBRCPPSample.

  2. Add a new source file named DBRCPPSample.cpp into the project.

For Linux/ARM/Mac

  1. Create a new source file named DBRCPPSample.cpp and place it into the folder [INSTALLATION FOLDER]/Samples.

Include the Library

  1. Add headers and libs in DBRCPPSample.cpp.

     #include "[INSTALLATION FOLDER]/Include/DynamsoftBarcodeReader.h"
     using namespace std;
     using namespace dynamsoft::dbr;
     #if defined(_WIN64) || defined(_WIN32)
         #ifdef _WIN64
             #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x64/DBRx64.lib")
             #pragma comment(lib, "[INSTALLATION FOLDER]/Lib/Windows/x86/DBRx86.lib")

Initialize a Barcode Reader Instance

  1. Create an instance of Dynamsoft Barcode Reader.

     CBarcodeReader dbr;
  2. Initialize the license key.

     dbr.InitLicense("<insert DBR license key here>");

    Please replace <insert DBR license key here> with a valid DBR licensekey. There are two ways to obtain one:

    • Search InitLicense and find the license from [INSTALLATION FOLDER]/Samples/BarcodeReaderDemo/BarcodeReaderDemo.cpp.
    • Request a trial license from Customer Portal.

Configure the Barcode Scanning Behavior

  1. Set barcode format and count to read.

     char sError[512];
     PublicRuntimeSettings runtimeSettings;
     runtimeSettings.barcodeFormatIds = BF_ALL; 
     runtimeSettings.barcodeFormatIds_2 = BF2_POSTALCODE | BF2_DOTCODE; 
     runtimeSettings.expectedBarcodesCount = 32;
     dbr.UpdateRuntimeSettings(&runtimeSettings, sError, 512);

    The barcode formats to enable is highly application-specific. We recommend that you only enable the barcode formats your application requires. Check out Barcode Format Enumeration for full supported barcode formats.

    If you know exactly the barcode count you want to read, specify expectedBarcodesCount to speed up the process and improve the accuracy.

Decode and Output Results

  1. Decode barcodes from an image file.

     int iErrorCode = -1;
     iErrorCode = dbr.DecodeFile("[INSTALLATION FOLDER]/Images/AllSupportedBarcodeTypes.png", "");
     if(iErrorCode != DBR_OK)
         cout << dbr.GetErrorString(iErrorCode) << endl;

    For the error handling mechanism, the SDK returns Error Code for each function and provides a function GetErrorString to get the readable message. You should add codes for error handling based on your needs. Check out Error Code for full supported error codes.

  2. Get and output barcode results.

     TextResultArray* pResult = NULL;
     if (pResult != NULL && pResult->resultsCount > 0)
         cout << pResult->resultsCount <<" total barcode(s) found."<< endl;
         for (int iIndex = 0; iIndex < pResult->resultsCount; iIndex++)
             cout << "Result " << iIndex + 1 << endl;
             cout << "Barcode Format: " << pResult->results[iIndex]->barcodeFormatString << endl;
             cout << "Barcode Text: " << pResult->results[iIndex]->barcodeText << endl;

    The SDK returns multiple barcode information, including barcode count, barcode format, barcode text, location, barcode raw data, etc. Check out TextResult for full supported result data.

Release Allocated Memory

  1. Release the allocated memory for the barcode results.

     if(pResult != NULL)           

Please change all [INSTALLATION FOLDER] in above code snippet to your unpacking path.

Build and Run the Project

For Windows

  1. In Visual Studio, set the solution to build as Release|x64.

  2. Build the project to generate program DBRCPPSample.exe.

  3. Copy ALL *.dll files under [INSTALLATION FOLDER]\Lib\Windows\x64 to the same folder as the DBRCPPSample.exe.

  4. Run the program DBRCPPSample.exe.

The SDK supports both x86 and x64, please set the platform based on your needs.

For Linux/ARM/Mac

  1. Create a file named Makefile with following content and put it in the same directory as the file DBRCPPSample.cpp.

     LDFLAGS=-lDynamsoftBarcodeReader -lstdc++ -L $(DBR_LIB_PATH) -Wl,-rpath=$(DBR_LIB_PATH) -Wl,-rpath=./
     # build rule for target.
     $(TARGET): $(OBJECT)
         $(CXX) -o $(TARGET) $(OBJECT) $(LDFLAGS)
     # target to build an object file
     $(OBJECT): $(SOURCE)
     # the clean target
     .PHONY : clean
         rm -f $(OBJECT) $(TARGET)
  2. Open a terminal and change to the target directory where Makefile located in. Build the sample:

  3. Run the program DBRCPPSample.


You can download the entire source code and compiled program from Here.

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 8.6.0
  • Version 8.4.0
  • Version 8.2.5
  • Version 8.2.3
  • Version 8.2.1
  • Version 8.2.0
  • Version 8.1.3
  • Version 8.1.2
  • Version 8.1.0
  • Version 8.0.0
  • Version 7.6.0
  • Version 7.5.0
Change +
© 2003–2021 Dynamsoft. All rights reserved.
Privacy Statement / Site Map / Home / Purchase / Support