Monday, May 19, 2014

GSL on Windows

Compiling GSL (Gnu Scientific Library) natively with Visual Studio 2013 Express

  1. Install GSL
    1. Download GSL 1.16 and unpack it.
    2. Download VS sln files from Brian Gladmann's page GSL for Windows. Unpack it into the same directory as GSL. It will add a new directory (build.vc11) and replace a couple files.
    3. If compiling for 64-bit Intel, you might need to change build.vc11/fp-win.c because 64-bit Intel does not allow setting the precision of floating point (see Boost Ticket #4964). On line 40, replace
      
      mask |= _MCW_PC;
      
      
      with
      
      #ifndef _WIN64
       mask |= _MCW_PC;
      #endif
      
      
    4. Read build.vc11/gsl.vc11.readme.txt file. It contains detailed instructions. Basically there will be 4 solution files in build.vc11 directory:
      gsl.dll.sln
      gsl.lib.sln
      test.gsl.dll.sln
      test.gsl.lib.sln
      
      Building any of these is similar so I will go through the steps for building gsl.lib.sln.
    5. Double click gsl.lib.sln to bring up MSVS. Select the solution and change the Solution Platform from Win32 to x64. Then for each project, change the Platform toolset to v120 (in Configuration Properties-->General).
    6. Since I choose to use gslhdrs, I add the project as a dependency of the cblaslib project. And in Configuration Manager, I uncheck the gsldefs project so it won't get built.
    7. Select "Build Solution" and it should build hopefully with no errors.
    8. The default code generation flag is Multi-threaded static runtime (ie, /MTd, /MT). If you switch to a Multi-threaded DLL runtime build (ie, /MDd, /MD), you might encounter errors similar to this:

      error C2375: 'gsl_acosh' : redefinition; different linkage in the file math.h

      This is because the build.vc11/config.h (which is normally generated by configure on Unix-type systems) tells the compiler it (ie, the compiler itself) doesn't have an implementation of acosh when it actually does and therefore does not need a GSL implementation.

      To fix this, go into build.vc11/config.h and change

      
      /* Define to 1 if you have the declaration of `acosh', and to 0 if you don't.
       */
      #undef HAVE_DECL_ACOSH
      
      to
      
      /* Define to 1 if you have the declaration of `acosh', and to 0 if you don't.
       */
      #define HAVE_DECL_ACOSH 1
      
      And repeat for similar problems referring to math.h. Then rebuild the Solution.
    9. I recommend building test.gsl.lib.sln as well. After building, choose any of the projects to run.
  2. Using GSL
    1. If you plan to use the DLL version of GSL in your application, you must pass -DGSL_DLL to the compiler (in Configuration Properties-->C/C++-->Preprocessor-->Preprocessor Definitions) when building your application.