Home | News | Documentation | Download

Using Root in Windows Visual Studio 2019

Hello,

I started learning C++/Visual Studio about 3 weeks ago and Root yesterday, so please assume I have no idea about anything.

My problem in a short summary:
How do I connect root to an existing C++ Code in Windows Visual Studio 2019, so I can use root commands in it?

More Details:
In the context of my Master Thesis I got a Data Acquisition Card (TimeTagger4-2G by Cronologic) for my Multichannel Plate. Unfortunately it was quite cheap, because it came without a ready to use software. Only a C++ example program, some .dll, some .lib and the Windows specific drivers (hence why I use windows visual studio) were included. The example program I managed to build, run and it does what it is supposed to do. Since I need to make histograms out of my data someone suggested I use root for this. I started learning root yesterday.
So how can I include the root commands in the already existing C++ example Code?

  • using Windows Visual Studio 2019
  • OS: Windows 10 64bit
  • root version: root_v6.18.04

What I do understand so far:

#inlcude <TH1.h>

What I tried so far:
I installed root by downloading the Pro .exe file from here: [https://root.cern.ch/downloading-root] and double clicking on it, when the download was complete. I connected the bin folder with all the .dll to my visual studio project and also the lib folder with all the .lib. I also copy pasted the content of the include folder into the same folder as my visual studio project. The only thing I changed in the code itself was to add in the beginning the command

#include "TH1.h" 

So far I get build errors. I assume I do something fundamentally wrong, but I can’t find a solution by Google or reading the user guide, because everything I find assumes a higher level of competence from the reader than I have, especially regarding windows visual studio.

I think @bellenot can help you.

Hi,

This is not needed, you simply have to add the include directory to the list of include directories in Visual Studio.

What kind of errors?

I get a lot of errors and warnings (around 100) after adding the single line for the TH1 header into the code. I guess they are all caused by linking stuff wrong? I’ll give one example for every type of error and warning I get.

  • Warning:C4267 'argument': conversion from 'size_t' to 'Ssiz_t', possible loss of data C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\TString.h Line:648

  • Warning:LNK4248 unresolved typeref token (01000016) for 'TClass'; image may not run test5 C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\Source.obj Line:1

  • Warning:LNK4272 library machine type 'x86' conflicts with target machine type 'x64' C:\Program Files (x86)\root_v6.18.04\lib\libHist.lib Line:1
    (comment from me about this warning: As I understand it, I have to run it as type ‘x64’, because the code of the data acquisition card seem to have a problem otherwise and doesn’t build)

  • Error:LNK2028 unresolved token (0A000588) "public: __cdecl TVersionCheck::TVersionCheck(int)" (??0TVersionCheck@@$$FQEAA@H@Z) referenced in function "void __clrcallanonymous namespace’::dynamic initializer for 'void __clrcall gVersionCheck::A0x13e4cadf(void)''(void)" (???__E?A0x13e4cadf@gVersionCheck@@YMXXZ@?A0x13e4cadf@@$$FYMXXZ) C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\Source.obj Line:1

  • Error:LNK2001 unresolved external symbol "protected: virtual void __cdecl TObject::DoError(int,char const *,char const *,char *)const " (?DoError@TObject@@MEBAXHPEBD0PEAD@Z) C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\Source.obj Line:1

  • Error:LNK2019 unresolved external symbol "public: static bool __cdecl TObject::GetObjectStat(void)" (?GetObjectStat@TObject@@$$FSA_NXZ) referenced in function "public: __cdecl TObject::TObject(class TObject const &)" (??0TObject@@$$FQEAA@AEBV0@@Z) C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\Source.obj Line:1

  • Error:LNK1120 72 unresolved externals C:\Users\Administrator\Documents\TimeTagger4 software developement\test5\test5\x64\Debug\test5.exe Line:1

Edit: I played around a bit and googled some more. Could it be that root does not work with x64 systems and my data acquisition card works only with x64 bit, so they are incompatible by default? :confounded:

So some of the warnings are harmless, but Root is only available in 32 bit for the time being on Windows

That explains a lot. Thank you very much for your help. Guess I’m back to square one and have to find another option.

Well, you have several options. You can use a socket to communicate with ROOT, or you can simply save your data in a file and read it back with ROOT…

Hi guys,
I have both microsoft visual studio 2017-2019 and a root installed in my laptop with windows 10. The command seems running in ROOT terminal on windows. However, I don’t want to spend too much time to learn about bash in windows or how to use terminal on windows. Is there any way to make visual studio to see the ROOT that I have already installed to run C++ codes for ROOT.

Note: My codes written in C++ works fine in visual studio, and the ROOT looks fine if you write the code on a terminal not as a macro to call it in anywhere else on your PC.
Thanks.

What do you mean? ROOT can run (or compile) any C++ code from anywhere on your PC, you can also create your own executable using the ROOT libraries, like any other external libraries in Visual Studio.

I mean that if you write a macro line by line on a ROOT terminal on Windows, it works.

Example:
{

TCanvas *c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,500,300);

Double_t x[100], y[100];

Int_t n = 20;

for (Int_t i=0;i<n;i++) {

x[i] = i*0.1;

y[i] = 10*sin(x[i]+0.2);

}

TGraph* gr = new TGraph(n,x,y);

gr->Draw(“AC*”);

}

Yes, and if you write the same code in a file, you can also execute it, like most of the tutorials. Do you mean it doesn’t work for you?

On windows, I don’t know how to call the file on windows terminal where I run the ROOT. Should I go through BASH coding? How can I run the ROOT when I open the code in windows visual studio 17 or 19 for instance?

Surely, No problem on linux,

Nothing to do with bash. Simply open a x86 Native Tools Command Prompt for VS 2019, call thisroot.bat from where ROOT is installed, and call .x yourfilename.C to execute the macro. For example:

1 Like

Hi,
It worked. Then, my next step will be where to put my codes to run. For now, I tested my code by putting inside the bin folder where the ROOT is. Can I create a folder on my desktop to put my codes and run from there like on linux? Plus, I’d like to be able to use compiler in visual basic to check the code. Otherwise, should I write the code in somewhere, and just run it in here where you say.

What is the most efficient way of storing the codes and recalling them to run on windows?

Cheers.

Absolutely.

You mean Visual Studio I suppose? Yes, but you will have to compile your code…

Same than Linux, standard C++ files

Alright. I think the only thing I need to change in my codes is the path I used before. The paths in my early codes should be replaced with their Windows paths. Is that right!

Not necessary, depends on the paths… Just try!

Hi, I am not used to the new setting-up procedure for root6.x on visual studio 2019. Could you please redirect me to some references?
I uninstalled VS17 and now root 5.34 (that I want to keep) complains when called from the console. I can’t access anymore to the detailed instructions to properly link VS and root 5.34. Could you refresh to me the procedure through to Native tool Command Prompt?

Thank you for your patience.

Why did you uninstall it? And what is ROOT complaining about? ROOT 5 is not linked to VS17 (which version is it exactly? I think VS17 is not a correct version)

I don’t understand the question. ROOT 5 doesn’t need Visual Studio to work properly, unless you need to build your own ROOT application. Is it the case? If it’s the case, then you need the exact same version of Visual Studio than the one used to build ROOT.

And for ROOT 6, you simply need to install Visual Studio 2019 (the Desktop development with C++ option). Note that ROOT 5 will not work with VS 2019.

I had too many VS and I wanted to unify them into a single version i.e VS2019. I just realise this is stupid, because I need the Platform Toolsets v140 and v141 to compile my own applications with ROOT libraries. I have ROOT version 6.22, 5.34.36 and 5.34.38. The last two come with macros that start at the beginning of the session (whenever ROOT.exe is called).

I downloaded the .zip for all the previous versions and modified the ENV variables accordingly. I called thisroot.bat from the x86 Native Tools Command Prompt for VS2019 and it works for all of them, i.e. upon calling ROOT it displays the right version.

The problems start when I try to compile my macros. I do as usual

root [0] .c mymacro.c

but it is unsuccessful. The subsequenet message I get in consol uon caaling ROOT is the following:

ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on win32)


CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
Error: Function BIT(9) is not defined in current scope  C:\root\5.34.36vs17_aclic/include\TAxis.h(53)
*** Interpreter error recovered ***

And it closes down root. I will check if it remains there after reinstalling the right Platform Toolsets and compiling again my macros.