Can't compile a ROOT 6 macro on Windows 10

So I can’t quite figure out how to compile my code on Windows 10. I installed root using the latest .exe installer for windows and installed it into directory D:\ROOT\root_v6.28.04, since there was some problem for ROOT with adding itself to PATH (“path variable is too long”) I had to manually add the root_v6.28.04, bin and macro folders to PATH, then just ran a visual studio code inside macro folder and used root inside the built-in terminal of VS code.

I know my macro runs well using root’s C++ interpreter (it just draws some 2D gaussian distribution), however when I try to compile the code using root histogram_2D.cpp+ it returns this code which I don’t know how to interpret:

root [0] 
Processing histogram_2D.cpp+...
Info in <TWinNTSystem::ACLiC>: creating shared library D:/ROOT/root_v6.28.04/macros/histogram_2D_cpp.dll

==========================================
=============== STACKTRACE ===============
==========================================


================ Thread 0 ================
  libCore!TWinNTSystem::DispatchSignals()
  ucrtbase!seh_filter_exe()
  root!Init_thread_header()
  VCRUNTIME140!_C_specific_handler()
  ntdll!_chkstk()
  ntdll!RtlRaiseException()
  ntdll!KiUserExceptionDispatcher()
  libCore!TWinNTSystem::GetLibraries()
  libCore!TSystem::CompileMacro()
  libCling!TCling::ProcessLine()
  libCling!TCling::ProcessLineSynch()
  libCore!TApplication::ExecuteFile()
  libCore!TApplication::ProcessLine()
  libRint!TRint::ProcessLineNr()
  libRint!TRint::Run()
  root!??
  root!??
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

================ Thread 1 ================
  ntdll!NtWaitForWorkViaWorkerFactory()
  ntdll!RtlInitializeResource()
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

================ Thread 2 ================
  ntdll!NtWaitForWorkViaWorkerFactory()
  ntdll!RtlInitializeResource()
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

================ Thread 3 ================
  ntdll!NtWaitForWorkViaWorkerFactory()
  ntdll!RtlInitializeResource()
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

================ Thread 4 ================
  ntdll!NtDelayExecution()
  KERNELBASE!SleepEx()
  libCore!TWinNTSystem::TimerThread()
  libCore!TWinNTSystem::ThreadStub()
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

================ Thread 5 ================
  win32u!NtUserGetMessage()
  USER32!GetMessageA()
  libCore!TWinNTSystem::FreeDirectory()
  KERNEL32!BaseThreadInitThunk()
  ntdll!RtlUserThreadStart()

==========================================
============= END STACKTRACE =============
==========================================

Nothing is being shown/drawn unlike just running it normally, the library histogram_2D_cpp.dll isn’t even being created too. It seems like I installed gcc/g++ in my system, but the problem persists. Do I need to rename my function from void histogram_2D() to int main()? (It didn’t help when I did). I have all the required ROOT classes included in my program, so that shouldn’t be a problem. Honeslty I’m not even sure what output of a compilation should I expect? An exe file or perhaps something else? There’s a bunch being created in my macro folder like .d, .rootmap, ACLiC_map.in, etc.

I know visual studio code doesn’t come with a compiler by default, but is there a way to still compile my C++ code, while editing it in VS code? I really don’t want to use visual studio because it creates a bunch of extra files and folders I don’t need in my macros folder.

ROOT Version: 6.28.04
Platform: Windows 10
Compiler: Not Provided


In order to use ACLiC, you must be in a Visual Studio environment, i.e. in a x64 Native Tools Command Prompt for VS 2022

Is there a way to make that work outside of visual studio? I can launch the command prompt as an admin, but when I try to compile it there I’m getting this error:
D:\ROOT\root_v6.28.04\macros\histogram_2D_cpp_ACLiC_dict.cxx(7): fatal error C1083: Can't open include file: stddef.h: No such file or directory

Yes, as I said, open a x64 Native Tools Command Prompt for VS 2022 (or a x86 Native Tools Command Prompt for VS 2022 for 32 bit) and run from there

That’s what I did and I get an error described in my previous comment.

Did you install the Desktop development with C++ component of Visual Studio?

ROOT → Blog Posts → How to run ROOT Macros in VS Code

Yep I did, last month I reinstalled VS to get the new VS 2022 and now checked again that I have that component installed.

Can you share your histogram_2D.cpp file?

Here it is. I temporarily commented out the gStyle because I’m not sure how to make it recognizeable by VS Code and stop compilation errors that whine about it being undefined, but that’s not the main problem here.

#include <iostream>
#include <TCanvas.h>
#include <TRandom2.h>
#include <TH2F.h>

void histogram_2D(double num_points = 1e7, bool enable_rainbow = 0){

    if (num_points < 0){
        std::cout << std::endl;
        std::cout << "---------------------------------------------------" << std::endl;
        std::cout << "Exception! Number of points should be non-negative" << std::endl;
        std::cout << "---------------------------------------------------" << std::endl;
        throw 1;
    }
    int maximum = num_points;

    TCanvas* c = new TCanvas();
    c->SetTicks();

    if (enable_rainbow){
        std::cout << enable_rainbow;
        //gStyle->SetPalette(kRainBow);
    }

    TH2F* hist = new TH2F("hist","2D Histogram",300,-25,25,300,-25,25);
    hist->SetStats(0);
    hist->SetAxisColor(kRed,"Y");
    hist->SetXTitle("X axis");
    hist->SetYTitle("Y axis");
    hist->SetTickLength(0.05,"Y");
    hist->SetContour(1000);

    TRandom2* rnd = new TRandom2();

    for (size_t i = 0; i < maximum; ++i)
    {
        double x = rnd->Gaus(0,0.4);
        double y = rnd->Gaus(-1,1.5);
        hist->Fill(-10*x-4*y,4*y);
    }
    
    hist->Draw("colz");

    c->Print("histogram_2D_1.pdf");
}

I don’t see any problem with your code:

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.6.2
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Users\bellenot>cd \root-dev

C:\root-dev>build\x64\release\bin\thisroot.bat

C:\root-dev>root -l histogram_2D.cpp++
root [0]
Processing histogram_2D.cpp++...
Info in <TWinNTSystem::ACLiC>: creating shared library C:/root-dev/histogram_2D_cpp.dll
histogram_2D_cpp_ACLiC_dict.cxx
   Creating library C:/root-dev\histogram_2D_cpp.lib and object C:/root-dev\histogram_2D_cpp.exp
histogram_2D_cpp_ACLiC_dict.cxx
   Creating library C:/root-dev\histogram_2D_cpp.lib and object C:/root-dev\histogram_2D_cpp.exp
Info in <TCanvas::Print>: pdf file histogram_2D_1.pdf has been created
root [1]