On Visual Studio 2022 Root running problem

_ROOT Version: 6.28
_Platform: Windows10, 64bit
_Compiler: Visual studio 2022

It’s a very simple test code. I set the including path, library path, lib input, environment variables. The code is built succeeded. But can not run. I copy all the *.dll files from bin folder to the built exe folder. It still can not run. Please help let me know where I’m wrong. Thank you very much!

#include <TF1.h>
int main(int argc, char** argv)
{
TF1* f1 = new TF1(“f1”, “sin(x)/x” , 0, 10);
f1->Draw();
return 0;
}

2023-02-15 12_39_47-Clipboard

2023-02-15 12_41_50-Clipboard

Welcome to the ROOT Forum!

Make sure you build with the same flags than the ones used to build ROOT. Since you only have one source file, I would suggest to build in the x64 Native Tools Command Prompt for VS 2022. Something like this:

cl -nologo -MD -GR -EHsc -Zc:__cplusplus -std:c++17 RooPlot.cxx -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGui.lib libHist.lib libGpad.lib

And if you want your program (your TCanvas) to stay open, you have to add an instance of TApplication. Something like:

#include <TApplication.h>
#include <TF1.h>

int main(int argc, char** argv)
{
   TApplication app("app", &argc, argv);
   TF1* f1 = new TF1("f1", "sin(x)/x" , 0, 10);
   f1->Draw();
   app.Run();
   return 0;
}

Thank you very much, Bellenot! It works!
I also find a way to build and run from Visual studio 2022 IDE. No environment variable setting required. The compiled exe file needs to be put into the Root/bin folder.

Thanks again Bellenot.

1 Like

You’re very welcome! And note you can also simply add %ROOTSYS%\bin in your PATH