The information provided here is based upon my own experience using Visual C++ 2010 Express in conjunction with a number of tutorials which have been listed through the tutorial where relevant and at the end.
Before you start it is worth noting, there is no need to set up environment variables in Windows. Whilst some tutorials seem to focus on them, they are not actually required to access the ROOT libraries or the include folders. They do, however, make your life simpler, but I suspect that if you are looking into compiling ROOT code on Windows, you probably aren’t looking to take the easy way out.
Firstly you need to download and install a C++ compiler. I personally chose to use Visual C++ 2010 Express as it is free. It is my understanding that if you wish to debug your code you may either need a different version or possibly have to buy Visual Studio. However for our purposes, this should be fine. Note that you should use the version of VC++ to guide your choice of ROOT binary file. I took the VC++10 version and installed it. It is important here you install somewhere easily accessible. To simplify things, I partitioned my hard drive and gave over some space to a partition I’ve named “Computing” and installed ROOT in the root of that partition. At this stage then you should have: A compiler installed and ROOT installed. The next section focuses on the setup of the working environment inside VC++, which whilst looks hectic, is actually quite straight forward once you understand what is occurring.
Setting Up Visual C++ 2010 Express
The first thing to understand is that to compile a standalone ROOT application you need to point VC++ in the direction of everything it needs to include during compilation. In Linux this is straight forward as we can use the environment variables to point to the libraries and just include a line of code in the makefile. Here we need to specify these in the project.
Open VC++ and choose: New Project > Win32 Application> Console and Empty Project options
Right-click the project name now shown in the Solution Explorer and select Properties.
We now enter a large table with multiple data values and options. There are 3 important options here we need to append to in order to get a ROOT code to compile.
In C/C++> General > Additional Include Directories : add
In C/C++> Advanced> Forced Include File: add
In Linker> Input> Additional Dependencies: add
These are the only tweaks required to allow VC++ to see ROOT, however it is not the end of the story as there are some requirements on the way the project is built. You can see in C/C++> Precompiled Header, that there is a file listed as StdAfx.h. If you wish, you can precompile a header file which includes the setups and header filed you wish to use from ROOT, but it is not required. However, the following is required in the setting up of your C++ code.
The following is based upon the tutorial provided by Martin Zeman as part of his Prague ROOT TApplication Template file. The main point he makes is that CINT and C++ compilers will look for different points in which to enter the C++ code. As we all know, every C++ code is required to have a main function but if you have used CINT macros, you will know that they instead require the same filename as function-name. Due to this, the code is written to comprise of the main analysis you wish to carry out, which is contained within a function of the same name as the program, as if it might be a ROOT macro, and the a main function which uses the TApplication class it run the function.
In addition to these functions, the start of the file needs to contain:
#if ( !defined(__CINT__) || defined(__MAKECINT__) ) #pragma once . . . #endif
In between the #if statements, you need to include all the header files on which your ROOT code will depend upon. Some examples are:
// STD #include
#include // ROOT #include "TROOT.h" // core #include "TSystem.h" #include "TApplication.h" // Root Data Types #include "TH1.h" #include "TString.h" #include "TFile.h" #include "TTree.h" // Root Graphics #include "TCanvas.h" // Root Math #include "TRandom.h" #include "TMath.h"
I will attach a link to a template which has the majority of possible candidates. You should find that once your program compiles also, a list of such possible header files should appear in the list of External Dependencies in the Solution Explorer.
Ideally after this, pressing F7 will allow your program to be compiled and built. At the very least any error messages you get should be ones related to not initialising a class correctly, rather than something like TROOT.h could not be found, as that indicates the libraries have not linked correctly.
Additional Information Regarding the Use of Compilers
One thing to consider though is that any executable compiled in Windows (and in particular in this case, using Visual C++) will only be executable under Windows. However, I have successfully compiled a large executable made up of about 25 C++ and 25 header files, which was written on Linux and came with a makefile to compile with gcc. I imported the source code into VC++ and linked in the libraries as indicated above and the program compiled. The compiler picked out a few things that needed tweaking, such as throwing an error if the atan2 function was using an int and not a float, but they were quickly solved. I did have one other issue with a class declaration I could not solve, but as it was not being used, I commented it out and the program compiled successfully. However, I don’t have the NTuples to test the program with, but in terms of compiling and running, I know it is working.
In addition, if you wish to compile on Windows for Linux, it is possible using Cygwin tools, but I have not explored this option yet, so I do not know how used friendly it is, but there are cross-compilers available (see here) which may be useful, and in this instance, it should be possible to set up environment variables (I presume?) in Cygwin as you would in Linux which means the commands used in Linux to include libraries (such as root-config --libs and root-config --glibs) should be possible, as should being able to define a makefile for use in conjunction with the Linux compiler. That said, it should also be possible to use the gcc compiler in Cygwin to compile Windows executables too, just like VC++ does, but using a makefile rather than an IDE.
And of course for completeness, the other option to compile under Windows for execution on Linux is to install VMWare Player (which is free) which sets up a virtual machine on your computer, meaning you could be running a Linux OS in the virtual machine and compile it there.
The ultimate upshot of this though, for my own learning, is that there shouldn’t need to be any difference between C++/header files written and compiled on Linux and written and compiled in Windows, as was shown by the ability to successfully compile the analysis program I had previously compiled on Linux.
I will update this as I learn more, but for now this should work!
Further information on using VC++ can be found here :http://www-hep2.fzu.cz/twiki/bin/view/ATLAS/WindowsRelated