How to Port Visual Studio C++ Project to Linux with CMake

Dynamsoft Barcode Reader is an enterprise-class barcode SDK implemented in C/C++. Although the SDK is available for Windows, Linux, and macOS, there is only one Windows sample showing how to invoke the latest video APIs in version 7.x. To make developers experience the example in Linux or other platforms, I decided to refactor the project build environment with CMake.

Download

Building OpenCV

No matter what operating system you are using, you can build OpenCV libraries from the source code.

Windows

Install Visual Studio 2017 and cmake-3.15.2-win64-x64.msi

Extract the OpenCV source code and create a temporary directory:

cd opencv-3.4.7
mkdir build
cd build

Build the OpenCV libraries:

cmake -G"Visual Studio 15 2017 Win64" ..
cmake --build . --target install

Linux

Install the required packages:

sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libjpeg-dev libpng-dev libtiff-dev

Extract the OpenCV source code and create a temporary directory:

cd opencv-3.4.7
mkdir build
cd build

Build the OpenCV libraries:

cmake ..
cmake --build . --target install

Replacing Visual Studio Project with CMake

Extract the barcode reader example in Windows:

vsproject

Delete *.sln and *.vcproj files.

Reorganize the project structure and create a CMakeLists.txt file for build configuration.

cmake project

Editing CMakeLists.txt

Check platforms:

if (CMAKE_HOST_WIN32)
    set(WINDOWS 1)
elseif(CMAKE_HOST_UNIX)
    set(LINUX 1)
endif()

Configure a header file to pass some of the CMake settings to the source code:

configure_file (
    "${PROJECT_SOURCE_DIR}/BarcodeReaderConfig.h.in"
    "${PROJECT_BINARY_DIR}/BarcodeReaderConfig.h"
)

Add link directories of Dynamsoft Barcode Reader and OpenCV:

if(WINDOWS)
    link_directories("${PROJECT_SOURCE_DIR}/platforms/win/lib/") 
elseif(LINUX)
    link_directories("${PROJECT_SOURCE_DIR}/platforms/linux/") 
    find_package(OpenCV REQUIRED)
endif()

Add header file directories:

include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include/")

Add the target executable file and link dependent libraries:

add_executable(BarcodeReader BarcodeReader.cxx)
if(WINDOWS)
    target_link_libraries (BarcodeReader "DBRx64" "opencv_core347d.lib" "opencv_highgui347d.lib" "opencv_videoio347d.lib")
else()
    target_link_libraries (BarcodeReader "DynamsoftBarcodeReader" ${OpenCV_LIBS})
endif()

Copy DLL files to the output directory for Windows:

if(WINDOWS)
    add_custom_command(TARGET BarcodeReader POST_BUILD 
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/platforms/win/bin/"      
        $<TARGET_FILE_DIR:BarcodeReader>)
endif()

CMake build for Windows

From Dynamsoft\Barcode Reader 7.1\Components\C_C++\Lib, copy DBRx64.lib to platforms\win\lib.

From opencv-3.4.7\build\lib, copy opencv_core347d.lib, opencv_highgui347d.lib, opencv_videoio347d.lib to platforms\win\lib.

From Dynamsoft\Barcode Reader 7.1\Components\C_C++\Redist\x64, copy all DLL files to platforms\win\bin.

From opencv-3.4.7\build\bin\Debug, copy opencv_imgproc347d.dll, opencv_videoio347d.dll, opencv_highgui347d.dlland opencv_imgcodecs347d.dll to platforms\win\bin.

Create a build folder:

mkdir build
cd build

Edit CMakeLists.txt to replace the installation path with yours:

set(CMAKE_INSTALL_PREFIX "e:/${PROJECT_NAME}")

Generate project configuration files:

cmake -G"Visual Studio 15 2017 Win64" ..

Build the project:

cmake --build .

Run the app:

build\Debug\BarcodeReader.exe

CMake build for Linux

Copy Dynamsoft/BarcodeReader/lib/WITHOUTSTDC++LIB/libDynamsoftBarcodeReader.so to platforms/linux/.

Create a build folder:

mkdir build
cd build

Generate project configuration files.

cmake ..

Build the project:

cmake --build .

Run the app:

./BarcodeReader

cmake cpp barcode reader for Linux

About Dynamsoft Barcode Reader

Dynamsoft Barcode Reader SDK enables you to efficiently embed barcode reading functionality in your web, desktop, or mobile application with a few lines of code. It saves development time and extra costs. With the SDK, you can create high-speed and reliable barcode scanner software to meet your business needs.

Supported Barcode Types

1D barcode

Code 39, Code 93, Code 128, Codabar, EAN-8, EAN-13, UPC-A, UPC-E, Interleaved 2 of 5 (ITF), Industrial 2 of 5 (Code 2 of 5 Industry, Standard 2 of 5, Code 2 of 5), ITF-14

2D barcode

QRCode, DataMatrix, PDF417 and Aztec Code

Source Code

https://github.com/yushulx/cmake-cpp-barcode-qrcode-mrz/tree/main/examples/9.x/opencv_camera