Configure PLplot for the Visual Studio IDE

From PLplotWiki
Revision as of 02:32, 29 June 2012 by Philrosenberg (Talk | contribs) (changed fix for hanging problem when using your own wxwidgets app and provided options to specify unicode)

Jump to: navigation, search

CMake allows you to generate Visual Studio solution and project files.

As of version 2.8.2 of CMake, here are a few caveats:

  • If you use Intel Fortran, the solution/project files may cause errors when building. These affect only the Fortran libraries and examples. It is due to a small bug in CMake that should be fixed with the next release.
  • For some reason you can not run the examples directly. We have not found out yet what is the matter, but there is a workaround - see below.

Workaround for examples built with Visual Studio

  • Build the PLplot libraries and examples via the ALL_BUILD project
  • Open a DOS box in the Debug directory for any one of these examples
  • Set the following environment variables:
  set path=<build>\dll\Debug;%PATH% 
  set PLPLOT_DRV_DIR=<build>\drivers    
  set PLPLOT_LIB=<source>
  where: <build> is the directory where PLplot was built
  and the directory "<source>\data" holds the .pal and .fnt files
  • In this environment you can run the sample program


Walkthrough building PLPLot with Visual Studio

Having just been throught this process I thought I'd make sure it was available online for others. I've just compiled PLPLot 5.9.9 with wxWidgets 2.8 using Microsoft Visual Studio 2008 Express on a Windows 7 machine. It is a bit of an arduous process but it should work. My reason for wanting to do this is that I have wxWidgets compiled using Visual Studio in various differet configurations (with/without unicode, static/dynamic) and needed PLPlot with matching configurations and similar ease of changing proerties/recompiling. Here are the instructions. If you are not using wxWidgets then you can ignore the wxWidget specific instructions.

If you want to write GUI applications using wxWidgets then you probably already have a working sln file for compiling wxWidgets. If not then download one from wxPack - I found this the most reliable way to get a wxWidgets working on Windows. If you have just installed wxPack then navigate to the install folder and open the sln file. Now Go to Build->Batch Build, select all and build. I think some projects in the solution depend on others so it might take three or four attempts to get everything to compile. I never got the Universal configurations to compile, but don't use them so am not worried.

Now, to get antialiased graphics to work with wxWidgets you can use wxWidgets Built in antialiasing using the graphics context or you can use the AGG library. I seem to remember AGG was quite easy to build, so I'll leave that for you to work out. For Fonts again you have the option of wxWidgets internal or the FreeType library. Again you need to build FreeType if you want it.

If you want to use wxWidgets antialiasing you must recompile wxWidgets. In your wxWidgets directory find all instances of setup.h or setup0.h and set

  #define wxUSE_GRAPHICS_CONTEXT 1

while I was at it I also set

  #define wxUSE_POSTSCRIPT 1

This isn't needed for plplot but I thought as I was recompiling anyway I'd add it in.

Now right click the project core, and select properties. Under librarian->General->Additional dependancies or Linker->Input additional Dependancies add gdiplus.lib

Now go to Build->Batch Build and Build all. If you get errors about a manifest then go to Build-> Batch Build and ReBuild all. You may need to do repeated builds to get everything built as above. If you got the manifest error and rebuilt then do additional builds, not rebuilds until everything is built.

Download the PlPlot Source and unzip it. In this example I've chosen to unzip to D:\plplot-5.9.9. Make a new directory alongside this, I've chosen to call it D:\plplot-5.9.9_staticbuild.

Install Cmake if you don't already have it.

open a command prompt and enter

  c:\program files\microsoft visual studio 9.0\common7\tools\vsvars32.bat

This may be different if you installed visual studio elsewhere, have a different version of visual studio or I guess the 32 may be a 64 on 64 bit machines.

Then

  d:\
  cd d:\plplot-5.9.9_staticbuild

Obviously modify this if you want to build somewhere else Now you can use Cmake to generate your visual studio solution/project files. The default command is

  cmake d:/plplot-5.9.9 -G "Visual Studio 9 2008" -DPL_DOUBLE=ON -DBUILD_SHARED_LIBS=OFF

This will make your project files with plplot using double precision (in theory) to generate static libraries. There are some other options you might want to add, such as

  -DENABLE_wxwidgets=ON
  -DHAVE_AGG=ON
  -DHAVE_FREETYPE=ON
  -DLIB_TAG="s"
  -DCMAKE_CXX_FLAGS="/D_UNICODE /DUNICODE"

These are pretty self explanitory. The DLIB_TAG option adds s to the end of the project files. I use this to indicate static. The unicode flags cause the library to be built using the unicode character set. Other wise the library is built with multibyte character set. Note that this option must be matched with any projects you create in the future that link against plplot.

You can see CMake options for PLplot for more PLplot specific options. You can also set SHARED_LIBS to OFF to generate dynamic libraries but I haven't tried this yet.

After calling Cmake you should have a sln file in your directory and a config.h file with sensible settings. It might be worth checking your config.h file and your plconfig.h file. I had to uncomment the line

  #define PL_DOUBLE

despite using the DPL_DOUBLE option

I had to do a bit of shuffling of code to get AGG and freetype to work. I had to copy the contents of my AGG include directory and the agg_font_freetype.h file from my agg folders to d:\plplot5.9.9_staticbuild\agg2

Now open the sln file if you haven't done so already. The projects you can see in the solution do all sorts of things it seems including copying libraries, reporting errors online and building the demos. The projects needed for building the libraries are

  • plplot for the main C interface
  • plplotcxx for the c++ interface
  • plplotwxWidgets for the wxplplotstream and wxplplotwindow objects for use in wxWidget applications
  • qsatime - don't know what this does
  • csirocsa - don't know what this does either

I deleted all projects except for these ones to make life easier. You may want to include the examples or something. Instead of compiling these projects into 5 separate libraries I copied the source files from these projects into the plplot project to complile into one library. I didn't copy the plplotwxwidgets files accross because I don't intend to use these classes, but I presume you should be able to do so if you wish.

I had to make some changes to the code to make the library build. I had to add

  #ifdef __cplusplus
  extern "C" {
  #endif

to the beginning and

  #ifdef __cplusplus
  }
  #endif

to the end of csa.h and qsatime.h. You might also need to go through csa.c and add explicit typecasts to all calls to malloc.

Now you need to go through the project properties to ensure everything is as you want it, eg you are linking against the correct runtime library, the resulting libs are called the correct names and in a folder where you want them, etc.

I then went through a whole world of pain because whenever I tried to link one of my wxWidget projects to my new plplot library the code hung during wxWidgets initialisation. I've now traced this to a bug in the wxwidgets.h file. Open this file and search for a the lines

  wxAppInitializer                                                 \
  wxAppInitializer( (wxAppInitializerFunction) wxPLCreateApp );    \

and replace them with

  wxAppInitializer                                                 \
  wxAppInitializer( (wxAppInitializerFunction) (wxApp::GetInitializerFunction()==NULL ? wxPLCreateApp : wxApp::GetInitializerFunction()) );    \

You may want to create dlls as well as static libs. If so create a new folder called plplot-5.9.9_sharedlibs and start the process again from scratch using this folder. If you try to create a shared library solution in the same directory as the static library then your config files will be overwritten and your static build will stop working.

Using the latest development version

This morning (28th june 2012) I tried for the first time to build the latest development version (Trunk). This went rather smoother than when I tried version 5.9.9. Kudos to the developers for improving things and here are the instructions for using it. If you are using wxWidgets I would recommend using Trunk as there have been some bug fixes since 5.9.9.

If you wish to use wxWidgets then install it as described above.

If you wish to use AGG and/or FreeType then download and build those libraries.

Install Tortoise SVN and use this to get the PLplot source from the repository. Details are at subversion access. If you have a preffered method for accessing repositories then you can obviously use this instead.

Install CMake.

As described above, create a working directory for the library, start a cmd prompt and cd to the new directory.

Run the vsvars32.bat file as described above.

Run CMake as described above with suitable options for static linkage and use of wxWidgets/AGG/FreeType.

copy the needed AGG header files into a folder agg2 in your working directory as described above.

Open the .sln file with Visual Studio

In Visual Studio you will see a number of projects. the ones that actually create libraries are csirocsa plplotcxx plplot plplotwxwidgets qsatime You may wish to change settings in these projects. For example you may wish to change the filenames or locations of the generated libraries or the linkages or if you add the four other libraries as Additional Dependancies to plplot then they should be bundled together allowing you to include PLplot as a single library.

Go to Build and Build Solution. You will find that 3 projects fail. These are create_staging, create_staging_announcement and validate_announcement. I'm not exactly sure what they do (something to do with generating a document of changes maybe?) but they are not required to build your libraries so don't worry about them. If more projects than this fail then build again - it might be that if you've added dependancies the build order means the needed libraries don't exist yet when a project that needs them is being built.

I have just submitted the above fix for wxwindows.h as a patch so this should be available in development version soon.