Sunday, 15 December 2013

How To Use MathGL Under Windows with the Microsoft Visual Studio 2010 64 bit compiler


This solution is heavily based on the solution provided in the following tutorial :!topic/mathgl/t-X9eg-5joU.
My thanks to RK for helping me find a solution.
I have found some changes in the packages since RK's post and I also needed 64 bit GLUT libraries so I have made minor some changes to his procedures.

Note : While this solution worked, I found that the MathGL functionality didn't quite stack up to that available in GNUPlot, see here for further details :


  Microsoft Visual Studio 2010 :
  Microsoft SDK v7.1 64 bit compiler. Installation instructions are here :
  MathGL 2.2 source code :
  FreeGlut 2.8.1 (Martin Payne's Windows MSVC binaries :
  CMake 2.8.11 :

1. Follow steps 1 to 3 in RK's tutorial except using freeglut instead of the original GLUT.
2. Before running CMake-gui ensure that you have configured the Microsoft Compiler environment using :
  "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
3. Follow steps 4 to 6 in RK's tutorial and choose "Visual Studio 10 Win64" in step 6.
4. Tick the Advanced box in CMake-gui to view all of the requried entries (e.g  GLUT_INCLUDE_DIR and GLUT_glut_LIBRARY)
5. Follow the rest of RK's tutorial
6. In your chosen source code folder create the following file (e.g. example.cpp) :

#pragma warning( disable: 4190 )    // Disable complex number warning

#include <mgl2/glut.h>

int sample(mglGraph *gr)
  mglData dat (100);
  for (long i = 0; i < 40; i++)
    gr->NewFrame ();          // start frame
    gr->Box ();               // some plotting
    gr->Axis ();              // draw axis
    for (long j = 0; j < dat.nx; j++)
      dat.a[j] = sin (M_PI*j/dat.nx+M_PI*0.05*i);
    gr->Plot (dat, "b");      // "b" is colour ??
    gr->EndFrame ();          // end frame
    gr->WriteFrame ();        // save frame
  return 0;

int main (int argc, char **argv)
  mglGLUT gr (sample, "MathGL examples");
  return 0;

7. Open a command window into your source folder and ensure the Microsoft Compiler environment is correctly configured, as per step 2. above.
8. Configure the compiler environment variables to locate the header and library files e.g. :
  set INCLUDE=%INCLUDE%;c:\mathgl-2.2\include
  set LIB=%LIB%;c:\mathgl_v2.2\src\Release;C:\mathgl_v2.2\widgets\Release
9. Compile example.cpp using the following command line :
  cl /EHsc example.cpp mgl-glut.lib
10. Copy the required dlls to the current folder :
  copy /Y C:\Users\johne2\Desktop\Graphics\mathgl\mathgl_v2.2\mathgl-2.2-cmake\src\Release\*.dll
  copy /Y C:\Users\johne2\Desktop\Graphics\mathgl\mathgl_v2.2\mathgl-2.2-cmake\widgets\Release\*.dll
  copy /Y C:\Users\johne2\Desktop\Graphics\mathgl\Glut\freeglut\bin\x64\*.dll
11. Execute example.exe and you should see a scrolling sine wave.
12. Play with MathGL :-)

If you have found this solution useful then please do hit the Google (+1) button so that others may be able to find it as well.
Numerix-DSP Libraries :


  1. hi~ do you have a thorough guide? without using the glut? i can't seem to make it work.. i just need the png library which exports graphs to files.. hope you can help..

  2. Hi Ralph, These are all the instructions I have. As I mentioned, after doing this, I moved back to GnuPlot
    I hope you manage to resolve your problem.
    Good luck.

  3. Thanks for the guide. Yet, I can't figure out what steps 18-19 of RK exactly mean :(
    In fact, Visual studio does not know . Could you please clarify a bit more?