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

Xiao Ling
3 min readNov 6, 2017

--

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.

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.

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:

if(WIN32)link_directories("${PROJECT_SOURCE_DIR}/platforms/win")endif()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:

include(CTest) add_test (BarcodeReaderRuns BarcodeReader)

Run CTest under build folder:

ctest

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

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

if(WIN32)# Copy DLL files to output directoryadd_custom_command(TARGET BarcodeReader POST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_if_different"${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll"$<TARGET_FILE_DIR:BarcodeReader>)endif()

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.

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

if(WIN32)set(CMAKE_INSTALL_PREFIX "f:/BarcodeReader")install (FILES "${PROJECT_SOURCE_DIR}/platforms/win/DynamsoftBarcodeReaderx86.dll" DESTINATION bin)endif()install (TARGETS BarcodeReader DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/BarcodeReaderConfig.h" DESTINATION include)install (DIRECTORY "${PROJECT_SOURCE_DIR}/include" DESTINATION include)

Run the command line barcode reader

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

Source Code

https://github.com/dynamsoft-dbr/cmake

Originally published at www.codepool.biz on November 6, 2017.

--

--

Xiao Ling
Xiao Ling

Written by Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover

No responses yet