C++ QR Code Generator with libqrencode and CQRcode

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

C++ QR Code Generator: Complete Guide



QR codes have become an integral part of the modern web and mobile applications. They are used for quick link navigation, transferring contact data, payments, and authentication. If you are developing in C++ and need a built-in QR code generator — without external services or APIs — this article is for you. We will cover two main libraries: libqrencode (low-level, flexible) and CQRcode (high-level, with PNG output).



Why generate QR codes in C++?

Main use cases:

  • Offline generation in desktop applications (inventory, tickets).
  • Embedding in server-side C++ solutions (high performance).
  • Working with limited resources (embedded systems, microcontrollers).
  • Full control over the encoding and error correction process.


libqrencode Library

libqrencode is the most popular C/C++ library for creating QR codes. It implements the ISO/IEC 18004 standard, supports all versions (1–40) and error correction levels (L, M, Q, H). The library is compact, has no dependencies, and works on Linux, macOS, and Windows (via MinGW or MSVC).



Installation

# Linux (Debian/Ubuntu)sudo apt install libqrencode-dev

# macOS (Homebrew)brew install libqrencode

# Windows (vcpkg)vcpkg install libqrencode


Example of generating a QR code into a byte array (black and white image)

#include <qrencode.h>#include <iostream>#include <vector>

int main() { // Create a QR code for the string "https://pythonlib.ru" QRcode *qr = QRcode_encodeString("https://pythonlib.ru", 0, QR_ECLEVEL_M, QR_MODE_8, 1); if (!qr) { std::cerr << "Error creating QR code"; return 1; }

int width = qr->width; std::cout << "QR code size: " << width << "x" << width << "";

// Data: qr->data — byte array (0 or 1) for (int y = 0; y < width; ++y) { for (int x = 0; x < width; ++x) { bool black = (qr->data[y * width + x] & 0x01); std::cout << (black ? "██" : " "); } std::cout << ""; }

QRcode_free(qr); return 0;}

This code outputs the QR code to the console using characters. Saving to PNG or BMP requires additional processing.



CQRcode Library

CQRcode is a wrapper around libqrencode that can immediately save the result to a PNG file. It is perfect if you need a ready-made graphic file without extra code. Supports color, module size, and center logo customization.



Installation

# Download the source from GitHub: https://github.com/rickyzhang82/CQRcode# Build via CMake:mkdir build && cd buildcmake ..makesudo make install


Example of saving a QR code to PNG

#include <CQRcode.h>

int main() { // Create a generator object CQRcode qr; // Set data (URL, text, or vCard) qr.setData("BEGIN:VCARDVERSION:3.0FN:PythonLibURL:https://pythonlib.ruEND:VCARD"); // Configure parameters qr.setModuleSize(8); // size of one module in pixels qr.setMargin(4); // margin (white border) qr.setCorrectionLevel(QR_ECLEVEL_H); // maximum error correction // Save to file if (qr.savePNG("pythonlib_qr.png")) { // Success } else { // Error } return 0;}


Library Comparison

  • libqrencode — minimalistic, provides raw data. Requires

Recommendations