Root on windows (Visual studio 2010)


I would like to execute ROOT in C++ codes with Windows. I use Visual studio 2010 to write my programs as a result I downloaded ROOT as an .exe named (something like root-visualstudio2010.exe). After installation I have a folder which contents include et lib folders which I specified in Visual studio 2010.

The code is :

#include <iostream>
#include "TROOT.h"
#include "TCanvas.h"

using namespace std;

int main(int argc, char *argv[])
	TCanvas *canv = new TCanvas("c1", "", 200, 10, 1280, 720);

	return 0;

Visual studio find the TCanvas but there are errors when I compile :
“Don’t know TObject …”

Did I forget something to work with root in C++?

It seems that Visual studio sees .h but not .cpp, where are the .cpp files by the way?



How do you compile your code?
And BTW: Posting code? Read this first!

Cheers, Bertrand.

alright for the code colors.

It seems that Visual Studio doesn’t see the .cpp ROOT files

OK, let me ask again: How do you compile your code?

Visual studio allows to compile C++ with outside libraries (the software contains everything usefull to compile, I just clic on the run button).
I included (in Visual Studio options) the folder “include” and “lib” from the main ROOT folder (as usual when I want to include libraries). It seems to work because TCanvas is known now but there are still errors probably due to .cpp not found by Visual Studio.

The errors are “can not resolve symbol … function … TCanvas (TObject and other classes)”


Note that you can also compile from the command line (it’s even simpler…). For example:

C:\Users\bellenot\rootdev>cl -nologo -MD -GR -EHsc -I%ROOTSYS%\include -FIw32pragma.h main.cpp /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGpad.lib libGui.lib /out:davidpicard.exe

Thanks, do you know if there is a tutorial to easily setup Root with Visual Studio 10 on windows?

Simply download a Visual Studio 2010 binary distribution from here, install it, and run…

It’s what I did, but I still can’t use TCanvas and the other ROOT classes

Well, if you can’t give more details, I’m afraid I won’t be able to help you…

Indeed sorry, this is what I just did:

(I have Visual Studio 2010 installed on my windows 7)

  • I downloaded “Windows Visual Studio 2010 -” from your link
  • I extracted it on my disk C:\
  • I have now a folder “root” here: C:\root which contains folders : lib, include and others
  • I run this file : C:\root\bin\thisroot.bat
  • In the project option (in Visual Studio), I specified the include and lib folder. Thanks to this, the “TCanvas” is known because there is no red underline anymore
  • I compile with the run button

Visual studio shows 5 errors which say “can not resolve symbol __cdecl::TCanvas” and 4 other errors like this.

Should I give more details? Thanks for your help

OK, I see, thanks for the details. You have to add libCore.lib libGpad.lib libGui.lib in the list of libraries to link against (in the Configuration Properties->Linker->Input I think)
And BTW, you may have to add libraries if you use other classes…

Oh I see, but I added the lib folder, is it not enough?

No, Visual Studio (like any other linker) cannot guess which libraries to link against

Alright, thanks a lot there is no errors anymore! The program lanchs but it says “no libCore.dll found”, did I forget somehting again?

Yes, %ROOTSYS%\bin (e.g. C:\root\bin) must be in your PATH

I think the .bat did it, I can see it. It could be another reason?

when you run thisroot.bat, it’s only valid for the command prompt into which you call it. If you want, you can add the ROOTSYS environment variable pointing to C:\root, and add %ROOTSYS%\bin in the PATH environment variable, so it will be available everywhere

Alright, perfect, thousand of thanks!! A last question, how can I know the lib I need? (in linker)
For example I want to create now a .root with TFile and I have this error again which is due to the missing lib in linker

If you go on the ROOT Reference Documentation of a given class, you’ll find the information on the bottom of the page. For example, if you go on the TH1F Class Reference page and scroll down, you can see this picture:
showing the library and its dependencies.
You can also add all the libraries, Visual Studio will link only against the needed ones