My First C/C++ App Built with CMake on Windows

If you have a program designed for multiple platforms, you definitely don’t want to waste time configuring and maintaining the building environment. I was thinking how to compile my C/C++ code for Windows, Linux and macOS more conveniently, and after that, I decided to learn CMake. CMake is an open-source, cross-platform tool designed to build, test and package software. This post is just the start for Windows platform.

Required Tools

How to Build C/C++ Barcode Reader with CMake

After reading the fundamental CMake tutorial step by step, you can quickly get familiar with the common build system of CMake. Here I will share how to set up a more complicated project.

Create a CMake project

The basic project just needs a CMakeLists.txt and a .cxx file. To use Dynamsoft Barcode Reader, I copied Barcode Reader 5.2\Components\C_C++\Lib\DBRx86.lib and Barcode Reader 5.2\Components\C_C++\Redist\DynamsoftBarcodeReaderx86.dll to platforms\win folder. The include folder contains all dependent header files.cmake project structure

How to build the binaries in target folder using command line tool

When running CMake GUI, we can specify the destination of source code and binaries.cmake gui

If you prefer command line tool, it’s also effortless:

mkdir build
cd build
cmake ..
cmake --build .

How to configure the search path to link an existing static library

To locate the library file and corresponding header file, use link_directories and include_directories:


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

You need to change the link directory for Linux and macOS.

Link the library:

add_executable(BarcodeReader BarcodeReader.cxx)
target_link_libraries (BarcodeReader "DBRx86")

How to copy DLL file to output folder

cmake --build . --target install

We can use CTest to check whether the program can work.

Add CTest to CMakeLists.txt:

add_test (BarcodeReaderRuns BarcodeReader)

Run CTest under build folder:


The following error message warns you that DynamsoftBarcodeReaderx86.dll file cannot be found.

ctest error

Don’t forget to copy the DLL file to the output folder after building the application. Add custom command to CMakeLists.txt:

    # Copy DLL files to output directory
    add_custom_command(TARGET BarcodeReader POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different

How to install software to a custom folder

By default, CMake will install applications to C:/Program Files (x86)/BarcodeReader. However, it will show errors if there is no administrative privilege.cmake install error

You can run cmd.exe as an administrator or change the value of CMAKE_INSTALL_PREFIX:

    set(CMAKE_INSTALL_PREFIX "f:/BarcodeReader")
    install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" DESTINATION bin)

install (TARGETS BarcodeReader DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/BarcodeReaderConfig.h" DESTINATION include)

Run the command line barcode reader

f:\BarcodeReader\bin\BarcodeReader.exe [barcode image file]

cmake barcode reader

Source Code