Skip to content

How to use OpenBLAS in Microsoft Visual Studio

zchothia edited this page Nov 14, 2012 · 71 revisions

As of OpenBLAS v0.2.3, it is currently only possible on Windows to build OpenBLAS with MinGW. The resulting library can be used in Visual Studio, but it can only be linked dynamically. This configuration has not been thoroughly tested and should be considered experimental.

Incompatible x86 calling conventions

Due to incompatibilities between the calling conventions of MinGW and Visual Studio you will need to make the following modifications (32-bit only):

  1. Use the newer GCC 4.7.0. The older GCC (<4.7.0) has an ABI incompatibility for returning aggregate structures larger than 8 bytes with MSVC.

  2. Ensure the correct stack alignment:

--- a/Makefile.rule
+++ b/Makefile.rule
@@ -118,6 +118,9 @@ else
 COMMON_OPT += -O2
 endif

+# Stack alignment: compatibility with Visual Studio
+COMMON_OPT += -mincoming-stack-boundary=2
+
 # Profiling flags
 COMMON_PROF = -pg

Further details on why this is necessary are listed here: http://list.rdcps.ac.cn/pipermail/openblas/2012-May/000082.html

Build OpenBLAS on Windows OS

  1. Install the MinGW (GCC) compiler suite, either 32-bit (http://www.mingw.org/) or 64-bit (http://mingw-w64.sourceforge.net/). In addition, please install MSYS with MinGW.
  2. Build OpenBLAS in the MSYS shell. Usually, you can just type "make". OpenBLAS will detect the compiler and CPU automatically.
  3. After the build is complete, OpenBLAS will generate the static library "libopenblas.lib" and the shared dll library "libopenblas.dll" in the folder. You can type "make PREFIX=/your/installation/path install" to install the library to a certain location.

Generate import library

  1. First, you will need to have the lib.exe tool in the Visual Studio command prompt.
  2. Open the command prompt and type cd OPENBLAS_TOP_DIR/exports, where OPENBLAS_TOP_DIR is the main folder of your OpenBLAS installation.
  3. For a 32-bit library, type lib /machine:i386 /def:libopenblas.def. For 64-bit, type lib /machine:X64 /def:libopenblas.def.
  4. This will generate the import library "libopenblas.lib" and the export library "libopenblas.exp" in OPENBLAS_TOP_DIR/exports. Although these two files have the same name, they are totally different.

Use OpenBLAS .dll library in Visual Studio

  1. Copy the import library "OPENBLAS_TOP_DIR/exports/libopenblas.lib" and .dll library "libopenblas.dll" into the same folder.
  2. Please follow the documentation about using third-party .dll libraries in MS Visual Studio 2008 or 2010. Make sure to link against a library for the correct architecture. For example, you may receive an error such as "The application was unable to start correctly (0xc00007b)" which typically indicates a mismatch between 32/64-bit libraries.

Notice. If you need CBLAS, you should include cblas.h in /your/installation/path/include in Visual Studio. Please read this page.

Limitations

  • Both static and dynamic linking are supported with MinGW. With Visual Studio, however, only dynamic linking is supported and so you should use the import library.
  • Debugging from Visual Studio does not work because MinGW and Visual Studio have incompatible formats for debug information (PDB vs. DWARF/STABS). You should either debug with GDB on the command-line or with a visual frontend, for instance Eclipse or Qt Creator.
Clone this wiki locally