Setting up GCC, CMake, Boost and Opencv on Windows


Background Story

For a project I’ve been working on, the need came to build the program to run on Windows OS. The project was written in c++ and used OpenCV and Boost libraries. For ease of configuration I employed CMake.

Despite the target being Windows, I was developing and testing everything under GNU/Linux 😛 , fortunately I managed to write the code with minimal amount of native unix API calls. For example, file handling was done via Boost Filesystem and so on.

Therefore the only consideration was running the CMake script in windows, using Visual Studio or GCC (via MinGW). First attempt was done with VS 2013, but compilation failed with a bug of VS c++ compiler related to c++ template classes and getting a later version was taking time. So I gave a try for gcc on windows!

Objectives

First I wanted to see if c++ building on windows with relative ease is possible. Next, I wanted to avoid the dependency of Visual Studio for the matter. This might be specially useful if you code for commercial work, but cannot afford to buy the license or simply dislike Visual Studio 😛

Step 1. Install MinGW

I went for MinGW-w64 (http://mingw-w64.org/doku.php) build since its more accepted and supports 64 bit. (Don’t expect citations justifying this 😀 ).

  1. Mingw Builds (http://mingw-w64.org/doku.php/download/mingw-builds) distribution was chosen as I didn’t want to install cygwin or win builds. Its a simple install. The following combination of settings worked for me.
    • Target Architecture – 64 bit (personal choice, depends on target system)
    • Threads – Win32 (Some recommend POSIX over Win32, However openCV build failed with mysterious problems with POSIX threads)
    • Exception – seh (I didn’t do much research here, just kept first available option
  2. Once the installation is complete, navigate to the install location and look for “bin” folder.
  3. Add that location to the PATH variable
    1. Control Panel -> System -> Advanced System settings -> Environment Variables -> system variables -> choose Path and click Edit
    2. Append the path to “bin” folder into the PATH variable. (There are tons of guides of how to do this)
  4. Open a command line (Start -> cmd.exe)
  5. Type the following commands it should show the version and other info
    • gcc -v
    • This is merely used to confirm setting the PATH variable worked.
  6. Once gcc works, navigate to “bin” folder of MinGW install and make a copy of “mingw32-make” and rename it to “make”. (This executable provide “make”) This step is for convenience with CMake 😉

Step 2. Install CMake

  1. Download (https://cmake.org/download/)
  2. Run the installer
    • It’ll ask whether it is okay to include CMake binary location to the PATH variable – Tick yes, preferably system wide.
  3. Open a command line (cmd.exe) and run the following
    • cmake --version
    • Provided everyting works, the output will show the CMake version.

Step 3. Build and Install OpenCV

  1. Download OpenCV from http://opencv.org/downloads.html
    • I built 3.1.0 with default options.
  2. Extract the archive (ie: D:\opencv-3_1_0)
  3. Open CMD and change directory to source folder of opencv. From this step, all commands would be executed through CMD unless otherwise noted.
    • D:
    • cd D:\opencv-3_1_0\source
    • First command is not needed if OpenCV resides in C: for other partitions enter the partition name to change to that, then use the cd command. (I find this inconvenient 😛 )
  4. Run CMake. The main change is additional “MinGW Makefiles” parameter. Other than that, this step is pretty much going with standard OpenCV Documentation. Add necessary arguments fitting the needs!.
    • cmake -G "MinGW Makefiles" [other arguments] ../build
    • “../build” at end pointed “build” directory as destination of MakeFile
  5. Once Cmake configures successfully, change to the build directory and execute make.
    • cd ../build
    • make -j5
    • I used -j5 as my computer have 4 logical processors so 5 threads is well enough to fully load it! If the computer have 8 cores, use -j8 or -j9
    • Use the multithreaded compile option (-j5) with caution, some laptops tend to go into thermal shutdown with maximum load!!
  6. If the build complete successfully, next run make install
    • make install
    • This step will finalize the install.

Step 3. Build and Install Boost

  1. Download and extract boost archieve from (http://www.boost.org/users/download/)
  2. Make sure to download the source package!
  3. Extract the archive, enter the directory from CMD.exe (example below)
    • cd "D:\Program Files\boost\"
  4. Run the following commands
    • bootstrap.bat gcc
    • b2 --build-dir=build cxxflags="-std=c++11" -j5 --with-filesystem --with-system define=BOOST_SYSTEM_NO_DEPRECATED toolset=gcc stage
    • Note the use of “toolset=gcc”, “-j5” options. These are self explanatory!
    •  “–with-<library_name>” flag  is used to explicitly include the necessary ones only. If you choose to build all libraries, then don’t specify this at all.
  5. If everything works fine, then Boost Build is complete!. Navigate to “stage” folder and go through the inner folders, there would be the compiled DLL files. (ie: libboost_system_xxx_mingw_xx.dll)

Step 4. Setting up CMake Script to work with windows

  1. Usage
    • cmake -G "MinGW Makefiles" .
  2. I mashed up OpenCV and Boost CMake example scripts and some of my experiments to come up with the following CMake script.
  3. This script is a bare-bone CMake script, it has to be modified to suite your own project. I’m not an expert in CMake, so there would be room for improvement.
  4. I’ve tested the script with following configurations for the same exact project.
    • Kubuntu (16.04 LTS) with CMake 3.2.2, OpenCV 3.1.0, Boost 1.58
    • Windows 7 Professional, with CMake 3.6.0-rc_2, OpenCV 3.1.0, Boost 1.61
  5. The script is an updated version featured in my previous post on Boost, OpenCV, CUDA and CMake on Linux (https://tuxbotix.net/2016/03/18/nvidia-optimus-bumblebee-and-cuda-on-kbuntu-15-10/)
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)

set(execFiles test.cpp)

if(WIN32)
 set(OpenCV_DIR "D:\opencv-3-10\build") #Change this
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

if(CMAKE_COMPILER_IS_GNUCXX)
 set (CMAKE_CXX_FLAGS "-std=c++11 -lm")
 if(WIN32)
 set(BOOST_ROOT "F:\Program Files\boost_1_61_0") #change this, critical!
 endif()
endif()

find_package( OpenCV REQUIRED )
FIND_PACKAGE( Boost 1.58 COMPONENTS filesystem system REQUIRED )
if(Boost_FOUND)
 include_directories(${Boost_INCLUDE_DIRS})
endif()
message(boost ${BOOST_INCLUDEDIR})

LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
set(CMAKE_BUILD_TYPE Release)

add_executable (testApp ${execFiles})

target_link_libraries(testApp ${OpenCV_LIBS} ${Boost_LIBRARIES})

Possible Issues, Observations

I came across a few issues while setting up GNU Toolchain on Windows as well as configuring Boost, Opencv.

  1. CMake complains “CMake was unable to find a build program corresponding to “MinGW Makefiles”. CMAKE_MAKE_PROGRAM is not set.”
    • Cause : Seems to be CMake not recognizing “mingw32-make” as the make program despite CMake documentation saying it works! (https://cmake.org/cmake/help/v3.6/generator/MinGW%20Makefiles.html)
    • Workaround is making a copy of mingw32-make and rename it as “make”
    • The workaround may clash with existing “make” executable in the PATH. So take care!!
  2. When configuring Boost, “c1 is not recognized as an internal command”
    • Cause : Not specifying toolchain when executing bootstrap.bat and b2.exe
  3. Windows shows errors “libboost_system_xx_mingw_xx.dll” is not installed or “libopencv_imgproc310.dll is not installed” or similar errors
    • Cause : Windows cannot locate the DLL files.
    • Simplest fix is just copying the necessary DLL files and package them when distributing.
    • Warning : Always check legal matters (license agreement) before packing libraries that are not owned by you. Even if the libraries are open source, the license type may restrict distribution in binary format like this.