# Using Root in Windows Visual Studio 2019

**URL:** <https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621>\
**Category:** Newbie\
**Created:** [January 17, 2020, 10:10pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621 "2020-01-17T22:10:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rhiannon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rhiannon/32/20703_2.png) [@Rhiannon](https://root-forum.cern.ch/u/Rhiannon)\
**Post date:** [January 17, 2020, 10:10pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/1 "2020-01-17T22:10:04Z")

</div>

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:

- You have to connect the correct .dll in Visual Studio.
- You have to connect the correct .lib in Visual Studio. (For both I use this step for step guide: [[https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019](https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019)])
- You have to do something with stuff called “headers”, like including similar commands like this in the beginning

```auto
#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](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

```auto
#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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 20, 2020, 8:15am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/2 "2020-01-20T08:15:51Z")

</div>

I think @bellenot can help you.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 20, 2020, 9:47am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/3 "2020-01-20T09:47:50Z")

</div>

Hi,

> [@Rhiannon](#):
>
> I also copy pasted the content of the include folder into the same folder as my visual studio project

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

> [@Rhiannon](#):
>
> So far I get build errors.

What kind of errors?

---

<div class="post-metadata">

**Author:** ![Rhiannon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rhiannon/32/20703_2.png) [@Rhiannon](https://root-forum.cern.ch/u/Rhiannon)\
**Post date:** [January 20, 2020, 6:49pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/4 "2020-01-20T18:49:36Z")

</div>

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__clrcall`anonymous 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? 😖

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 20, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/5 "2020-01-20T20:51:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rhiannon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rhiannon/32/20703_2.png) [@Rhiannon](https://root-forum.cern.ch/u/Rhiannon)\
**Post date:** [January 20, 2020, 8:53pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/6 "2020-01-20T20:53:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 21, 2020, 7:50am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/7 "2020-01-21T07:50:00Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 4, 2020, 8:28pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/8 "2020-08-04T20:28:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 5, 2020, 6:52am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/9 "2020-08-05T06:52:55Z")

</div>

> [@spyhunter01](#):
>
> ROOT looks fine if you write the code on a terminal not as a macro to call it in anywhere else on your PC.

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.

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 5, 2020, 10:48am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/10 "2020-08-05T10:48:27Z")

</div>

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

Example:  
{

[TCanvas](https://root.cern.ch/doc/master/classTCanvas.html) \*[c1](https://root.cern.ch/doc/master/legend1_8C.html#a1259eec59a19080ae1da77e79182c60e) = new [TCanvas](https://root.cern.ch/doc/master/classTCanvas.html)(“c1”,“A Simple Graph Example”,200,10,500,300);

[Double\_t](https://root.cern.ch/doc/master/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) [x](https://root.cern.ch/doc/master/legend1_8C.html#a13c6713ae496caa8195647f76887f926)[100], [y](https://root.cern.ch/doc/master/legend1_8C.html#a1380cd153a0fc78015dd604dbcb6c841)[100];

[Int\_t](https://root.cern.ch/doc/master/RtypesCore_8h.html#a3885b911a54b47a4e61671f45dd45d0b) [n](https://root.cern.ch/doc/master/legend1_8C.html#a16daaa7b596941b23915a1ac1be5b42c) = 20;

for ([Int\_t](https://root.cern.ch/doc/master/RtypesCore_8h.html#a3885b911a54b47a4e61671f45dd45d0b) i=0;i\<[n](https://root.cern.ch/doc/master/legend1_8C.html#a16daaa7b596941b23915a1ac1be5b42c);i++) {

[x](https://root.cern.ch/doc/master/legend1_8C.html#a13c6713ae496caa8195647f76887f926)[i] = i\*0.1;

[y](https://root.cern.ch/doc/master/legend1_8C.html#a1380cd153a0fc78015dd604dbcb6c841)[i] = 10\*[sin](https://root.cern.ch/doc/master/TMath_8h.html#ad910bc71940a50c2a35d308c236013b6)([x](https://root.cern.ch/doc/master/legend1_8C.html#a13c6713ae496caa8195647f76887f926)[i]+0.2);

}

[TGraph](https://root.cern.ch/doc/master/classTGraph.html)\* [gr](https://root.cern.ch/doc/master/legend1_8C.html#a81e8067fc8f8d5a6217c06912ca906a0) = new [TGraph](https://root.cern.ch/doc/master/classTGraph.html#af67c25bf5dbcc17ef0bab90904b06da6)([n](https://root.cern.ch/doc/master/legend1_8C.html#a16daaa7b596941b23915a1ac1be5b42c),[x](https://root.cern.ch/doc/master/legend1_8C.html#a13c6713ae496caa8195647f76887f926),[y](https://root.cern.ch/doc/master/legend1_8C.html#a1380cd153a0fc78015dd604dbcb6c841));

[gr](https://root.cern.ch/doc/master/legend1_8C.html#a81e8067fc8f8d5a6217c06912ca906a0)-\>[Draw](https://root.cern.ch/doc/master/classTGraph.html#a7ee6d3572ef075dd4fa1a6deb939a986)(“AC\*”);

}

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 5, 2020, 11:48am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/11 "2020-08-05T11:48:40Z")

</div>

Yes, and if you write the same code in a file, you can also execute it, like most of the [tutorials](https://root.cern/doc/master/group__Tutorials.html). Do you mean it doesn’t work for you?

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 9, 2020, 9:09pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/12 "2020-08-09T21:09:32Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 10, 2020, 6:26am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/13 "2020-08-10T06:26:01Z")

</div>

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:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/3/9350159b9e32c203243ab594105cbe22f4cbbb8f.png)

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 10, 2020, 1:12pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/14 "2020-08-10T13:12:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 10, 2020, 1:16pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/15 "2020-08-10T13:16:39Z")

</div>

> [@spyhunter01](#):
>
> Can I create a folder on my desktop to put my codes and run from there like on linux?

Absolutely.

> [@spyhunter01](#):
>
> Plus, I’d like to be able to use compiler in visual basic to check the code.

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

> [@spyhunter01](#):
>
> What is the most efficient way of storing the codes and recalling them to run on windows?

Same than Linux, standard C++ files

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 12, 2020, 8:18pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/16 "2020-08-12T20:18:00Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 12, 2020, 8:24pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/17 "2020-08-12T20:24:05Z")

</div>

Not necessary, depends on the paths… Just try!

---

<div class="post-metadata">

**Author:** ![g.nalin](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/g.nalin/32/13565_2.png) [@g.nalin](https://root-forum.cern.ch/u/g.nalin)\
**Post date:** [August 28, 2020, 3:31pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/18 "2020-08-28T15:31:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [August 28, 2020, 3:40pm UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/19 "2020-08-28T15:40:28Z")

</div>

> [@g.nalin](#):
>
> I uninstalled VS17 and now root 5.34 (that I want to keep) complains when called from the console

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)

> [@g.nalin](#):
>
> I can’t access anymore to the detailed instructions to properly link VS and root 5.34

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.

---

<div class="post-metadata">

**Author:** ![g.nalin](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/g.nalin/32/13565_2.png) [@g.nalin](https://root-forum.cern.ch/u/g.nalin)\
**Post date:** [August 31, 2020, 8:42am UTC](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621/20 "2020-08-31T08:42:52Z")

</div>

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:

```auto
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.

[Next page](https://root-forum.cern.ch/t/using-root-in-windows-visual-studio-2019/37621.md?page=2)
