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.
This article is Part 3 in a 16-Part Series.
- Part 1 - Building a C/C++ Barcode & QR Code Reader for Raspberry Pi with Dynamsoft SDK
- Part 2 - CMake: Build C++ Project for Windows, Linux and macOS
- Part 3 - How to Port Visual Studio C++ Project to Linux with CMake
- Part 4 - Insight Into Dynamsoft Barcode SDK Decoding Performance
- Part 5 - Building ARM64 Barcode and QR Code Scanner on Nvidia Jetson Nano
- Part 6 - How to Decode QR Code on Mac with Apple Silicon
- Part 7 - How to Develop a Desktop GUI Barcode Reader with Qt and C/C++
- Part 8 - How to Build a Desktop Barcode Scanner with Webcam Support Using Qt QCamera
- Part 9 - Building Command-line Barcode and QR Code Reader in C++
- Part 10 - How to Build Linux ARM32 and Aarch64 Barcode QR Scanner in Docker Container
- Part 11 - How to Link MSVC DLLs with MinGW GCC in Windows
- Part 12 - Transforming Raspberry Pi 4 into a Barcode Scanner with a C++ App, USB Camera, and OLED Display
- Part 13 - Building Windows Desktop Barcode Reader with Win32 API and Dynamsoft C++ Barcode SDK
- Part 14 - How to Build a Command-Line Barcode Reader with Rust and C++ Barcode SDK
- Part 15 - How to Decode Barcode and QR Code from WebP Images in C++ and Python
- Part 16 - Building a Desktop C++ Barcode Scanner with Slimmed-Down OpenCV and Webcam
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:
Delete *.sln and *.vcproj files.
Reorganize the project structure and create a CMakeLists.txt file for build configuration.
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.dll
and 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
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