-
Notifications
You must be signed in to change notification settings - Fork 1.5k
How to use OpenBLAS in Microsoft Visual Studio
As of OpenBLAS v0.2.15, we support MinGW and Visual Studio (using CMake to generate visual studio solution files) to build OpenBLAS on Windows.
A fully-optimized OpenBLAS that can be statically or dynamically linked to your application can currently be built for the 64-bit architecture with the LLVM compiler infrastructure. We're going to use Miniconda3 to grab all of the tools we need, since some of them are in an experimental status. Before you begin, you'll need to have Microsoft Visual Studio 2015 or newer installed.
- Install Miniconda3 for 64 bits. It should be easily found in your preferred search engine's results.
- Open the "Anaconda Command Prompt," which should be available in the start menu.
- Change to a good directory to build
- Now install all of the tools we need:
conda config --add channels conda-forge
conda install -y cmake flang clangdev perl
conda install -y -c isuruf kitware-ninja
- Activate the MSVC environment for 64 bits with
vcvarsall x64
. Search on the web for the particular steps for your version of MSVC. On Windows 10 with Visual Studio 2017, this would be
/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2017/Community/VC/Auxiliary/Build/vcvars64.bat
Confirm that the environment is active by typing link
.
6. Now configure the project with CMake. Starting in the project directory, execute the following. In addition, execute any additional configuration commands to build static or shared libraries or to further customize the build.
set "LIB=%CONDA_INSTALL_LOCN%\Library\lib;%LIB%"
set "CPATH=%CONDA_INSTALL_LOCN%\Library\include;%CPATH%
mkdir build
cd build
cmake .. -G "Ninja" -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_C_COMPILER=clang-cl -DCMAKE_Fortran_COMPILER=flang -DBUILD_WITHOUT_LAPACK=no -DNOFORTRAN=0 -DDYNAMIC_ARCH=ON -DCMAKE_BUILD_TYPE=Release
- Build the project:
cmake --build . --config Release
To build OpenBLAS for the 32-bit architecture, you'll need to use the builtin Visual Studio compilers.
[Notice] This method may produce binaries which demonstrate significantly lower performance than those built with the other methods. You may consider building for the 32-bit architecture using the GNU (MinGW) ABI.
# do this from powershell so cmake can find visual studio
cmake -G "Visual Studio 14 Win64" -DCMAKE_BUILD_TYPE=Release .
Note that this step depends on perl, so you'll need to install perl for windows, and put perl on your path so VS can start perl (http://stackoverflow.com/questions/3051049/active-perl-installation-on-windows-operating-system).
Step 2 will build the OpenBLAS solution, open it in VS, and build the projects. Note that the dependencies do not seem to be automatically configured: if you try to build libopenblas directly, it will fail with a message saying that some .obj files aren't found, but if you build the projects libopenblas depends on before building libopenblas, the build will succeed.
OpenBLAS can be built for use on the Universal Windows Platform using a two step process since commit c66b842.
1. Follow steps 1 and 2 above to build the Visual Studio solution files for Windows. This builds the helper executables which are required when building the OpenBLAS Visual Studio solution files for UWP in step 2.
2. Remove the generated CMakeCache.txt and CMakeFiles directory from the OpenBLAS source directory and re-run CMake with the following options:
# do this to build UWP compatible solution files
cmake -G "Visual Studio 14 Win64" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION="10.0" -DCMAKE_SYSTEM_PROCESSOR=AMD64 -DVS_WINRT_COMPONENT=TRUE -DCMAKE_BUILD_TYPE=Release .
This will build the OpenBLAS binaries with the required settings for use with UWP.
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.
Due to incompatibilities between the calling conventions of MinGW and Visual Studio you will need to make the following modifications ( 32-bit only ):
- 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.
- Install the MinGW (GCC) compiler suite, either 32-bit (http://www.mingw.org/) or 64-bit (http://mingw-w64.sourceforge.net/). Be sure to install its gfortran package as well (unless you really want to build the BLAS part of OpenBLAS only) and check that gcc and gfortran are the same version - mixing compilers from different sources or release versions can lead to strange error messages in the linking stage. In addition, please install MSYS with MinGW.
- Build OpenBLAS in the MSYS shell. Usually, you can just type "make". OpenBLAS will detect the compiler and CPU automatically.
- After the build is complete, OpenBLAS will generate the static library "libopenblas.a" 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.
[Notice] We suggest using official MingWin or MingWin-w64 compilers. A user reported that s/he met Unhandled exception
by other compiler suite. https://groups.google.com/forum/#!topic/openblas-users/me2S4LkE55w
Note also that the alternative builds of mingw-w64 available through http://www.msys2.org currently appear to contain a defect that leads to a compilation failure accompanied by the error message
<command-line>:0:4: error: expected identifier or '(' before numeric constant
Please see https://github.com/xianyi/OpenBLAS/issues/1503 for a workaround, or install the mingw-w64-gcc from sourceforge rather than the one (from pacman) that msys2 suggests.
- First, you will need to have the
lib.exe
tool in the Visual Studio command prompt. - Open the command prompt and type
cd OPENBLAS_TOP_DIR/exports
, where OPENBLAS_TOP_DIR is the main folder of your OpenBLAS installation. - For a 32-bit library, type
lib /machine:i386 /def:libopenblas.def
. For 64-bit, typelib /machine:X64 /def:libopenblas.def
. - 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.
- OpenBLAS already generated the import library "libopenblas.dll.a" for "libopenblas.dll".
- Copy the import library (before 0.2.10: "OPENBLAS_TOP_DIR/exports/libopenblas.lib", 0.2.10 and after: "OPENBLAS_TOP_DIR/libopenblas.dll.a") and .dll library "libopenblas.dll" into the same folder(The folder of your project that is going to use the BLAS library. You may need to add the libopenblas.dll.a to the linker input list: properties->Linker->Input).
- 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 (0xc000007b)" 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.
- 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.