C++, OpenCV and Gtk3 Windows dev environment

Development environment for Windows

Bad news are that Windows doesn’t have standard system locations for libraries as Linux and Mac OS X have. In consequence, to obtain the Gtk and OpenCV libraries, you need to use some non-standard procedure that involves installing a special package manager called pacman and a special development environment called MinGW.

Good news are that doing this has become easy with the existence of a project called MSYS2, and the fact that Visual Studio is compatible with MinGW.

Install M2SYS

Go to MSYS2 homepage and download the M2SYS installer that best corresponds to your system (probably the 64 one, as the i686 is actually 32 bits).

Install it with all default options, in C:\msys64 folder.

After installation you’ll have:

  • The MSYS2 MSYS environment, where you can use pacman to install components.
  • The MSYS2 MinGW 64-bit environment, which stands for Minimal GNU for Windows, and where you can use all tools and libraries installed via pacman.
  • You also get the MSYS2 MinGW32, to access 32 bit tools; you shouldn’t install any of those if your system is 64 bits.

As explained in the download instructions, the first thing to do is to upgrade the package database. Open the MSYS2 MSYS and type the following command:

$ pacman -Syuu

Should you encounter the message below, close the MSYS2 MSYS window with your mouse, open it again, and run the same command, until it terminates normally.

warning: terminate MSYS2 without returning to shell and check for updates again
warning: for example close your terminal window instead of calling exit

If you’re looking for a particular package, you can use a command similar to the following. Assuming you look for CMake:

$ pacman -Ss cmake

Build a great linux-like toolkit

Inspired by Alexey Pavlov’s gist, I advise to install the following packages and tools, so you have a linux – lookalike environment where you can compile applications for windows using Linux tools:

$ pacman -S base base-devel net-utils git ruby wget man
$ pacman -S msys/openssh msys/vim msys/bc nano msys/tmux
$ pacman -S gzip zip unzip msys/p7zip tar msys/tree
$ pacman -S msys/winpty msys/ed msys/pwgen msys/zsh
$ pacman -S mingw64/mingw-w64-x86_64-jq
$ pacman -S msys/screenfetch
$ pacman -S mingw-w64-x86_64-toolchain
$ pacman -S mingw64/mingw-w64-x86_64-cmake

After downloading and installing all those packages, you can verify if they’re present testing some. To do this, open the MSYS2 MingGW 64 bit environment, and test some commands:

$ git --version
$ make --version
$ cmake --version
$ gcc --version
$ pkg-config --version

See also:

Installing Gtk and OpenCV

You can install the Gtk and OpenCV libraries using pacman in MSYS2 MSYS, with the following commands:

$ pacman -S mingw64/mingw-w64-x86_64-gtkmm3
$ pacman -S mingw64/mingw-w64-x86_64-opencv

Building with MinGW64

To build the project, open the MSYS2 MinGW 64-bit, locate an appropriate folder (the drive units are mapped to the root folder, for example C:\Users\me\Document\Development\ would be /c/Users/me/Development):

$ cd /c/where/your/root/folder/is
$ git clone https://github.com/cpp-tutorial/raspberry-cpp-gtk-opencv.git
$ cd raspberry-cpp-gtk-opencv
$ mkdir build
$ cd build
$ cmake -G”Unix Makefiles” ../src/
$ make
$ ./racapp.exe

If you want to debug with gdb, you can use slightly different cmake command. If you already build the project, do not hesitate in deleting the build folder and creating it again:

$ cd ..
$ rm -rf build
$ mkdir build
$ cd build
$ cmake -G"Unix Makefiles" 
  -DCMAKE_BUILD_TYPE=Debug ../src
$ make
$ gdb ./rascapp.exe

After that you’re in gdb. To find your way, look in Linux instructions.

Making MinGW tools available everywhere

By default, MSYS2 installs libraries and tools in the C:\Msys2\MinGW64\bin folder. Add it to the PATH environment variable, so that all applications can find the tools:

Once you’re done, you can open the command line, and verify if the same tools are available from it:

$ pkg-config --version
$ pkg-config gtkmm-3.0 --cflags --libs
$ pkg-config opencv-3.0 --cflags --libs

This step is not strictly necessary if you’re going to use Code::Blocks, but it doesn’t hurt either.

Installing Code::Blocks in Windows and configuring MSYS2 toolchain

The default IDE in Windows is Visual Studio. However, Visual Studio expects a special format for debugging symbols that is not the one produced by gcc and g++ in MinGW. In a cross platform project, if you don’t want to make your life miserable following hyper complex installation procedures, is better to leave it out.

I chose Code::Blocks because:

  • It exists in all three major platforms, so you can use it everywhere, including Raspberry Pi, with similar configuration.
  • It’s pretty popular, so you can look for instructions and community support.
  • It has a Windows installation wizard that takes cares of all configuration details.

The installation procedure is completely straight forward, at least in Windows 10

  1. Install a Windows binary release of Code::Blocks from official site: http://www.codeblocks.org/downloads.
  2. Among all versions, chose the normal setup (you already installed the mingw toolchain in the previous step).
  3. Install it with all default options.
  4. Launch application

If there are no error messages, you’re done. Otherwise, look at the troubleshooting.

Building with Code::Blocks

The easiest path is to use CMake to prepare a Code::Blocks project for you. Assuming that you already cloned the project in a known location, and your MSYS2 MinGW 64-bits terminal is already there, this is the sequence of commands:

$ mkdir codeb
$ cd codeb
$ cmake -DCMAKE_BUILD_TYPE=Debug
  -G"CodeBlocks - Unix Makefiles" ../src/

Follow the rest of the procedure from Code::Blocks:

  • From the menu, File → Open…, look for the codeb folder that you just created, and for the rascam.cbp project file in it (note that the name of the project is the same as specified in CMakeLists.txt).
  • If you like, go to ProjectProject Tree and modify Display folders as on disk and Hide folder name.
  • Select one of the source files and place a break point on it by clicking in the gray area.
  • In the toolbar, select the rascapp target.
  • In the toolbar, launch the debugger.
  • It should launch, and stop at the breakpoint.
  • To see the variables, go to DebugDebugging windowsWatches.

Troubleshooting

invalid or corrupted package (PGP signature)

Occasionally you may found the following error message when downloading, updating or upgrading with pacman:

(4/4) checking package integrity [###################################################################] 100%
error: jsoncpp: signature from “Alexey Pavlov (Alexpux) ”
is invalid
:: File /var/cache/pacman/pkg/jsoncpp-1.8.4-1-any.pkg.tar.xz is
corrupted (invalid or corrupted package (PGP signature)).

It is a transitory problem related with authoring websites being unavailable, and you can solve it by just waiting (see a discussion about signature problems from “Alexey Pavlov (Alexpux)”). Alternatively, you can deactivate the signature checking by editing /etc/pacman.conf (use Vim from MinGW64) and changing the following entry:

SigLevel = Never

Code::Blocks cannot find the compiler

At launching, Code::Blocks may complain that it doesn’t find the compiler toolchain. If you have installed it with MSYS2 as described above, it is just a matter of configuration:

  • At launching, just accept the dialog requiring you to choose an existing toolchain.
  • In the main menu, open SettingsCompiler…
  • In the dialog, look for Global compiler settings section, and Toolchain executables tab.
  • Ensure that the path is correct. If you installed MSYS2 with default options, it should be C:\msys64\mingw64.
  • Also ensure that all mentioned program files correspond to existing executables in that folder.
  • Accept everything, and do the same with SettingsDebugger…

Note: I had this problem when I installed Code::Blocks with mingw-setup. I guess it expects a standard MinGW installation, and not the MSYS2 one.

Code::Blocks cannot save configuration

When closing the application, or when saving all data, Code::Blocks may complain that it cannot save configuration, and display an error message. To solve this:

  • Take note of the path where it tries to save the configuration file.
  • Create the same path, and an empty file with the same name.
  • Make this file writable.

Note: I had this problem when I installed Code::Blocks with mingw-setup. I guess it expects a standard MinGW installation, and not the MSYS2 one.



My Personal Notes arrow_drop_up

Electronics and Computer Science engineer in finance by day and embedded systems by night I like to explore around embedded systems onboard of model cars and planes Im using Microchip PIC micro controllers, Raspberry PI, GtK, OpenCV and C/C++

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.