
Building OpenOCD under Windows using MSYS2
About
In past, building OpenOCD under Windows was really complicated since it has a lot of dependencies and not was easily available under windows. With MSYS2 this task has become very simple and, if you need to use the latest stable version of OpenOCD in MSYS2, it is available as binary and could be installed with a single command.
In some cases, you need to build the development version of OpenOCD or, maybe, to apply some patches from OpenOCD Gerrit repository. Using MSYS2 and some PKGBUILD from the upstream repository, this operation is easy to accomplish.
Introducing MSYS2
MSYS2, which stand for Minimal SYStem 2, is a compact application based on Cygwin and MinGW-w64 which runs under Windows. It allow to use autotools, revision control systems and build native Windows applications using MINGW-w64 toolchains. The installation of packages is simplified since creators have ported Arch Linux’s Pacman. This brings many powerful features such as dependency resolution and simple complete system upgrades. When launched MSYS2 appears to be a simple terminal.
MSYS2 consists of three subsystems (here and out MSYSTEM) and their corresponding package repositories. They are msys2, mingw32, and mingw64.
- The mingw subsystems provide native Windows programs and are the main focus of the project. These programs are built to cooperate well with other Windows programs, independently of the other subsystems.
- The msys2 subsystem provides an emulated mostly-POSIX-compliant environment for building software, package management, and shell scripting. These programs live in a virtual single-root filesystem (the root is the MSYS2 installation directory). Some effort is made to have the programs work well with native Windows programs, but it’s not seamless.
Preliminary notes
First time I wrote this article I was looking for a simple recipe to build OpenOCD under Windows and create a standalone suitable for ChibiStudio. The initial purpose has been extended and this article now is addressed to the reader who wants:
- install OpenOCD in MSYS2 from binary;
- compile and install OpenOCD in MSYS2 from development repository;
- export OpenOCD as stand alone.
Setup of MSYS2

MSYS2 is available as executable on SourceForge. Setup is straightforward and creates a folder usually under C: named msys64 (or msys32 in case of 32-bit system).
After the installation, the shell could be launched through one of the three .exe which are available in the installation folder. Each exe is related to one of the three MSYSTEM.
Usually we are interested to build software which cooperates with other Windows programs so we should use mingw32 or mingw64.
To be compatible with both 32-bit and 64-bit operating systems, the best choice is to use mingw32. In this case we should launch mingw32.exe.
It is possible to check current MSYSTEM through this command:
echo $MSYSTEM
To avoid any problem related to low permission levels, it is a good idea to launch the .exe right-clicking and choosing Run as Administrator.
Note that, here classical copy and paste hotkeys don’t work. It is possible to use it right clicking on terminal or using CTRL+Ins and Shift+Ins instead of CTRL+C and CTRL+V.
Install OpenOCD from binary
This procedure is quite simple. We can install the 32-bit version using the command
pacman -S mingw-w64-i686-openocd-git
or the 64-bit version using
pacman -S mingw-w64-x86_64-openocd-git
After the setup it is possible to use OpenOCD in MSYS2 like a native command. As example to launch openocd for a STM32 Nucleo-64 F401 we can just use the command
openocd -f "board/st_nucleo_f4.cfg"
It is simple and it is suitable for most of the scenarios. It is also possible to create a standalone version extracting OpenOCD and its dependencies from the folder C:\msys64\mingw32 (or C:\msys64\mingw64 if we are using the 64-bit version).
The only restriction is we are using the last built by MSYS2 maintainers. In certain cases we want to use the current development version and hence we need to build it from source code.
Building OpenOCD from repository
Configuring MSYS2
To compile packages requires additional configuration in MSYS2. First of all we need to update it
pacman -Syuu
repeating this step until it says there are no packages to update.
Note that, every time we updating it is required to restart MSYS2. It is also a good idea to do that when installing big packages like base-devel or mingw-toolchain
We also need to install the base development kit
pacman -S base-devel
And the mingw toolchain. If we want to install the 32-bit version the command is
pacman -S mingw-w64-i686-toolchain
the 64-bit version requires this command
pacman -S mingw-w64-x86_64-toolchain
We need to clone the repository of the packages so we need also to install git:
pacman -S git
Note that at first use git requires to configure our identity. To do this we must use these commands (obviously using your identity and your email):
git config --global user.name "John Doe" git config --global user.email "johndoe@example.com"
As suggested by Kristof (see comments below), the following commands enlarge the memory allocated to git, preventing crashes when cloning large repositories:
git config --global pack.windowMemory "100m" git config --global pack.packSizeLimit "100m" git config --global pack.threads "1"
Cloning the upstream packages repository
As said, each MSYSTEM has its own package repository. We need to clone the MINGW-packages repository which contains packages for a big number of tools including OpenOCD and its dependencies.
In what follow I will use my home folder (C:/msys64/home/RoccoMarco) as workspace, so I will clone the repository here using the git clone command
cd ~ git clone https://github.com/Alexpux/MINGW-packages.git
If git clone ends successfully, into our home folder we will find a new folder named MINGW-packages. This folder contains a big number of sub-folders. Every subfolder contains a PKGBUILD which is a set of instruction required to build a tool resolving its dependencies.
Building OpenOCD
Now we are able to build OpenOCD from its repository. If we want to build the 32-bit version we have to issue
export MINGW_INSTALLS=mingw32 cd ~/MINGW-packages/mingw-w64-openocd-git makepkg-mingw -sLf
Otherwise
export MINGW_INSTALLS=mingw64 cd ~/MINGW-packages/mingw-w64-openocd-git makepkg-mingw -sLf
The procedure would required some time (depending on your PC). When ends in the folder mingw-w64-openocd-git there would be:
- a folder named pkg containing the built package;
- a folder named src containing the source code;
- a tar containing the built package and the source code ready to be installed;
Installing the fresh built OpenOCD
We are now able to install it in MSYS2 using the command
pacman -U mingw-w64-*-openocd-*-any.pkg.tar.xz
which install OpenOCD from the tar just created. Like for installation from binary this allow to use OpenOCD in MSYS2 as a native command. The difference is we are now using the latest development version of OpenOCD.
Exporting the fresh built OpenOCD as stand alone
If we want to use OpenOCD as standalone, we need to build also its dependencies and copy them in a separate folder with the binary of OpenOCD. If we have installed OpenOCD, these dependencies are already available in the folder C:\msys64\mingw32 (or C:\msys64\mingw64 if we are using the 64-bit version).
By the way, it is quite simple to build them using the same procedure adopted for OpenOCD. We can do this using these commands
cd ~/MINGW-packages/mingw-w64-libusb makepkg-mingw -sLf
cd ~/mingw-packages/mingw-w64-libusb-compat-git makepkg-mingw -sLf
cd ~/mingw-packages/mingw-w64-hidapi makepkg-mingw -sLf
cd ~/mingw-packages/mingw-w64-winpthreads-git makepkg-mingw -sLf
cd ~/mingw-packages/mingw-w64-libftdi makepkg-mingw -sLf
Now we have all the pieces to create the stand alone folder. The next commands will copy these pieces into a unique folder rearranging it to obtain something similar to that one released by the OpenOCD team through the Freddie Chopin website.
if [ "$MINGW_INSTALLS" == "mingw32" ]; then MINGW_PACKAGE_PREFIX="mingw-w64-i686" MINGW_FOLD="mingw32" else MINGW_PACKAGE_PREFIX="mingw-w64-x86_64" MINGW_FOLD="mingw64" fi cp -avr ~/mingw-packages/mingw-w64-libusb/pkg/${MINGW_PACKAGE_PREFIX}-libusb/${MINGW_FOLD}/ ~/ cp -avr ~/mingw-packages/mingw-w64-libusb-compat-git/pkg/${MINGW_PACKAGE_PREFIX}-libusb-compat-git/${MINGW_FOLD}/ ~/ cp -avr ~/mingw-packages/mingw-w64-hidapi/pkg/${MINGW_PACKAGE_PREFIX}-hidapi/${MINGW_FOLD}/ ~/ cp -avr ~/mingw-packages/mingw-w64-winpthreads-git/pkg/${MINGW_PACKAGE_PREFIX}-libwinpthread-git/${MINGW_FOLD}/ ~/ cp -avr ~/mingw-packages/mingw-w64-openocd-git/pkg/${MINGW_PACKAGE_PREFIX}-openocd-git/${MINGW_FOLD}/ ~/ cp -avr ~/mingw-packages/mingw-w64-libftdi/pkg/${MINGW_PACKAGE_PREFIX}-libftdi/${MINGW_FOLD}/ ~/ mv ~/${MINGW_FOLD} ~/openocd mv ~/openocd/share/openocd/scripts ~/openocd/scripts cp -avr /${MINGW_FOLD}/bin/libgcc_s_dw2-1.dll ~/openocd/bin cp -avr /${MINGW_FOLD}/bin/libjaylink-0.dll ~/openocd/bin rm -rf ~/openocd/share
This will create the standalone folder named openocd in home (in my case C:\msys64\home\RoccoMarco).
Additional notes
How to patch OpenOCD
If we also want to patch the repository a good way could be duplicate the folder /mingw-packages/mingw-w64-openocd-git and edit its PKGBUILD adding some git commands.
As example some time ago I had to add two patches by Rémi PRUD’HOMME to support STM32F746G Discovery and this is how I edited the PKGBUILD
pkgver() { mv "${srcdir}/code" "${srcdir}/${_realname}" cd "${srcdir}/${_realname}" printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" printf "%s.%s.%s" "$(git fetch http://openocd.zylin.com/openocd refs/changes/54/2754/2)" "$(git fetch http://openocd.zylin.com/openocd refs/changes/55/2755/4)" "$(git checkout FETCH_HEAD)" }
What I have done is more add a printf() in the function pkgver() containing additional git commands.
Read OpenOCD documentation from MSYS2
If we have installed OpenOCD in MSYS2, it is possible to browse the documentation just using the command:
info openocd
Thanks for this great article!!! It is so helpful.
One item of errata: this command:
>>pacman -S ${MINGW_PACKAGE_PREFIX}-ftdi
didn’t work for me: package not found error.
I suppose you instead meant:
>>pacman -S ${MINGW_PACKAGE_PREFIX}-libftdi
I am having difficulty building the first mingw package:
$ cd ~/mingw-packages/mingw-w64-libusb
$ makepkg –config ${MAKEPKG_CONFIG} -i
==> ERROR: Cannot find the strip binary required for object file stripping.
I’m running windows 7 here. Don’t know if that would mess it up. I followed all your other instructions carefully.
I found a solution to the “missing strip binary” problem. Just run:
>> export PATH=${PATH}:/mingw64/bin
The “strip.exe” binary was sitting in /mingw64/bin
Hey,
Thanks for these comments. I need to update the article. I think something is changed in last months. I will make some tries…
Hi Rocco,
I have the following error at the end:
$ makepkg –config ${MAKEPKG_CONFIG}
==> ERROR: conflicts should be an array
==> ERROR: provides should be an array
What should I do?
Thanks so much for your help
Dear, as you can see on top of this article, I have updated it yesterday since I had to rebuilt openocd and I experimented some problems.
My suggestion is to delete the old files and restart the whole procedure. By the way, if you want to skip the procedure, I have a copy of OpenOCD. I can add it in the download section.
Hi Rocco,
That would be awesome!
Will your copy work on a 64-bit Windows 10 machine?
PS: I just did the whole procedure today. So probably your update yesterday didn’t work for my specific case. Anyway, I will be extremely happy to get your executable 🙂
Yes, I am using it in ChibiStudio and I have tested it on Win7 32bit and Win7 64bit. I have shared it to the ChibiOS community and I haven’t received any report ’til now. It is now available on the download page in the last section.
Hi Rocco.
Sadly it doesn’t work. I get an error when starting up “openocd.exe”:
” The program cannot get started because libgcc_s_dw2-1.dll is missing on your computer. You can possibly solve this issue by reinstalling this program on your computer”.
Can you post the arguments you have used while launching openocd? What interface are you using?
This is how I open it:
>”C:\Apps\OpenOCD-0.9.0-Win32\bin\openocd” -f “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\interface\stlink-v2.cfg” -f “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\target\stm32f7x.cfg” -s “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts”
I’ve been reading a bit on StackOverflow about this missing “libgcc_s_dw2-1.dll” file. Apparently this issue arises if you’ve built the executable without certain compiler flags. The quickest fix is to find that dll file inside your compiler (I cannot find it in mine), and put it also in the download section. I just need to put that dll in the same folder as the “openocd.exe”, and then it should work 🙂
Here is the StackOverflow question related to the issue:
http://stackoverflow.com/questions/4702732/the-program-cant-start-because-libgcc-s-dw2-1-dll-is-missing
Thank you so much for your help 🙂
Take a look to chapter 2.6 of this article. There are commands I use to launch it from bash. Note that according to your environment variables you should or should not use the whole path to launch openocd.exe.
Note: I have deleted the original content of my folder “OpenOCD-0.9.0-Win32”, and replaced it with the content from your downloaded zip folder. So I have sort of ‘updated’ my OpenOCD version in a manual way 🙂
I just downloaded the missing dll here:
http://www.opendll.com/index.php?arch=32Bit&file-download=libgcc_s_dw2-1.dll&version#
Now it looks like OpenOCD starts to function!
Thank you so much 🙂
Although OpenOCD now starts up without errors, and even connects to the chip, I still cannot flash my STM32F7 microcontroller. The error I got initially (with the ‘older’ version of OpenOCD) still persists. I have explained everything in detail on this StackOverflow post: http://stackoverflow.com/questions/38033130/how-to-use-the-gdb-gnu-debugger-and-openocd-for-microcontroller-debugging-fr
Carmine Noviello, the writer of this book: https://leanpub.com/mastering-stm32
told me that he got my “.elf” file flashed through GDB – OpenOCD without problem to the STM32F7 microcontroller. He compiled the newest OpenOCD source code on his Macintosh.
Sadly Windows 10 seems to have some more troubles..
Kristof the problem is informations are too… fragmentary. I cannot help you in this way.
Are you using a toolchain? The toolchain usually provides a gdb server? The toolchain usually uses openocd? What compiler are you using?
I have always used ChibiStudio (eclipse, arm-none-eabi toolchain, openocd, gcc). I have just used my version on OpenOCD to flash and run a demo on STM32F7 using a pc with Win 10 64bit.
Hi Rocco,
I took on a personal mission to learn the whole embedded chain (compile – link – flash – debug) manually, without any IDE involved. So I’m using the windows cmd terminal to go through every step.
I have dedicated a detailed StackOverflow question to this procedure. You can find it here:
http://stackoverflow.com/questions/38033130/how-to-use-the-gdb-gnu-debugger-and-openocd-for-microcontroller-debugging-fr
Thank you so much for your help. You are really a kind guy!
I appreciate it so much. I’m sure that the community is grateful to you, for all the interesting articles on your website!
While I appreciate a lot your effort, I have a couple of tips if you are resolute to proceed in this way:
1. Uninstall winzozz!
2. Destroy every trace of winzozz using fire. Be careful. Use gloves and goggles: safety first!
3. If you are to ignoring first 2 tips, install an eclipse based toolchain like chibistudio in order to understand if compiler, cross compiler, gdb server and debugger work fine
4. Try to remove, automatisms one at a time.
Good luck 🙂
Hi, I am trying to follow the steps above, but failed at the following step. Any idea? Please help. Thanks.
$ makepkg-mingw -sLf
==> Making package: mingw-w64-openocd-git 0.10.0.rc1.r1.gc404ff5-1 (15 Nov 2017 23:04:57)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Updating openocd git repo...
Fetching origin
==> Validating source files with sha256sums...
openocd ... Skipped
==> Extracting sources...
-> Creating working copy of code git repo...
Reset branch 'makepkg'
==> Starting prepare()...
which: no libtoolize in (/mingw32/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/)
which: no glibtoolize in (/mingw32/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/)
./bootstrap: Error: libtool is required
==> ERROR: A failure occurred in prepare().
Aborting...
Hi,
usually I follow this procedure from time to time in order to build a fresh OpenOCD for ChibiStudio. Everytime I usually update this guide which actually is a reminder for my self. I run this procedure last time few month ago and it worked. I am not an expert but I think that maybe the problem is that you do not have installed the base development packages
pacman -S base-devel
Let me know if you succeed or we will find a way out together and I will update this guide. Thanks for your feedback.
Thanks for the great tutorial!
Since I needed to build a specific version of OpenOCD, I wasn’t sure how to specify this in the source URL. In case anyone else needs it here is an example:
git+https://github.com/ntfreak/openocd.git#tag=v0.10.0-rc2
And here is the link where this is documented:
https://jlk.fjfi.cvut.cz/arch/manpages/man/PKGBUILD.5#USING_VCS_SOURCES
Thanks for commenting and sharing additional info.
I just used this page again to make a fresh OpenOCD installation. Thanks!
Please consider adding the following commands at the end of paragraph 4.1:
# git config –global pack.windowMemory “100m”
# git config –global pack.packSizeLimit “100m”
# git config –global pack.threads “1”
These commands enlarge the memory allocated to git, preventing crashes when cloning large repositories.
Hey Kristof,
thanks for commenting. I am updating the article right now.
Hi Rocco, that’s great :-). Is the patch you mentioned in paragraph 7.1 still needed for the latest openocd version?
Hey there,
as far as I know, that patch has been already merged long time ago. I left the paragraph as example in case user has to patch something else…
Hello Rocco, Thank you very much for this great valuable blog..I was able to execute all the steps you mentioned here successfully and able to build my custom modified openocd binary very fast..I appreciate the clean descriptions of steps in this article.. I was skeptical about the build with so many dependencies of many components but overall I didn’t see any issues. Only problem I see is the time taken to complete the build.configure script takes much time to finish before compilation starts.I wanted to know if there is a way to override the configure procedure for successive makes.
Hello Vimal,
thanks. To be honest, I wrote this guide to myself the first time I was able to build OpenOCD. Since then, I am using and updating it every time I am building OpenOCD under Windows. Also, there are many contribution to this article for other users whom experienced issues during the process.
That said, i think this is the topic I am less confident of in the entire blog. If there is any way to override the configure procedure, I do not know it. If you will find anything interesting about this, please come back and contribute: I will update he article with your finding.
Hello Rocco,
For the purpose of making openOCD builds faster I edited the /mingw-packages/mingw-w64-openocd-git/ PKGBUILD and now my openOCD build process will complete within 10 minutes, earlier with the original PKGBUILD it took around 45 minutes to 1 hour to get finished.The edited PKGBUILD file is copied below.I removed the git repo update in PKGBUILD along with pkgver(), prepare() function invocations and ./configure script run.The removal of git repo update from PKGBUILD is anyway a must if you have changed the openOCD source code for something since otherwise when you build it without removing git repo update your local changes will be replaced with the original git repo code.
# Maintainer: Andrew Leech
# Contributor: Ray Donnelly
# Contributor: Rocco Marco Guglielmi
_realname=openocd
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.10.0.r1293.g7c88e76a7
pkgrel=1
pkgdesc="OpenOCD - Open On-Chip Debugger (mingw-w64)"
arch=('any')
url="http://openocd.org/"
license=("GPLv2")
options=('!ccache')
depends=("${MINGW_PACKAGE_PREFIX}-hidapi"
"${MINGW_PACKAGE_PREFIX}-libusb"
"${MINGW_PACKAGE_PREFIX}-libusb-compat"
"${MINGW_PACKAGE_PREFIX}-libftdi"
"${MINGW_PACKAGE_PREFIX}-libjaylink")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
"${MINGW_PACKAGE_PREFIX}-pkg-config"
)
build() {
cd "${srcdir}"/${_realname}
make
}
package() {
cd "${srcdir}"/${_realname}
make DESTDIR="${pkgdir}" install
install -Dm644 COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
# bsdtar can't convert non-ascii filenames to UTF-8 when run in
# non-UTF-8 locale. It proceeds to pack them anyway embedding raw
# binary filenames in the traditional tar headers.
}
Hello Vimal,
thanks for contributing. Next time I am going to build OpenOCD I’ll give a try to your mods and eventually fix\update the article.
Hello Rocco,
I am now looking for a way to build the open OCD arm binary for a cortex A core running in Linux.I have found some options like XPack Open OCD which provides build scripts for compiling OpenOCD source code for ARM platform but it needs a dedicated ARM hardware (The current platform for Arm GNU/Linux and Windows production builds is a Debian 9, running on a ROCK Pi 4 with 4 GB of RAM and 256 GB of fast M.2 SSD.). Is there any way to cross compile the OpenOCD source for ARM platform in Windows/Linux ?
Hello Vimal,
to be honest I ma not so expert. I barely found a way to build OpenOCD under Windows and what you are seeing it’s a tutorial for my self I am updating every time I am building openocd.
I would suggest you to go ask on their official IRC channel
http://openocd.org/discussion/irc/