Saving branch of vector problem in VS2022

___ Hi,
When run my root program in VS2022, it’s faild, because I want to save branch of vector into one tree. But the same code can run in linux. Can you help me? Thank you very much!
my code is following:

#include
#include <TFile.h>

#include <TNtuple.h>
#include <TH2.h>
#include <TProfile.h>
#include <TCanvas.h>
#include <TFrame.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TRandom3.h>
#include <TBenchmark.h>
#include <TInterpreter.h>
#pragma comment (lib, “libCore.lib”)
#pragma comment (lib, “libTree.lib”)
#pragma comment (lib, “libRIO.lib”)
#pragma comment (lib, “libHist.lib”)
//#include <windows.h>
using namespace std;
int main() {
//gInterpreter->GenerateDictionary(“vector”, “vector”);
// gInterpreter->GenerateDictionary(“vector”, “vector”);
std::vector<Int_t> waveform;
int pp[10];

/*
for (int j = 0; j < 10; j++) {
	waveform.push_back(j);

}
for (int j = 0; j < waveform.size(); j++) {
	std::cout << waveform[j] << std::endl;
}
*/
//TH1F istogramma("istogramma", "istogramma", 10, -5., 5.);
TFile* outputfile = new TFile("aa.root", "RECREATE");
//TFile* outputfile = 0;
//outputfile = TFile::Open("aa.root", "RECREATE");
TTree* dataTree = new TTree("dataTree", "Tree with vector");
dataTree->Branch("test", &waveform);
dataTree->Branch("pp", pp);
for (int j = 0; j < 10; j++) {
	waveform.clear();
	for (Int_t i = 0; i < 10; i++) {
		pp[i] = i;
		waveform.push_back(j);
	}
	dataTree->Fill();
}

outputfile->Write();
outputfile->Close();
std::cout << "ppppppp" << std::endl;
return 0;


}

and the error information is following:
屏幕截图 2022-12-02 071952

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
_Platform:VS2022
_Compiler:6.26/10


I don’t any ‘obvious’ problem and you code as is works ‘for me’ on linux (when compiling as an executable and run through valgrind).

How do you use your code?

I can’t run it in windows

Hi have no problem with this code:

#include <TFile.h>
#include <TTree.h>
#include <iostream>

int main(int argc, char* argv[])
{
   std::vector<Int_t> waveform;
   int pp[10];
   TFile* outputfile = new TFile("aa.root", "RECREATE");
   TTree* dataTree = new TTree("dataTree", "Tree with vector");
   dataTree->Branch("test", &waveform);
   dataTree->Branch("pp", pp);
   for (int j = 0; j < 10; j++) {
      waveform.clear();
      for (Int_t i = 0; i < 10; i++) {
         pp[i] = i;
         waveform.push_back(j);
      }
      dataTree->Fill();
   }
   outputfile->Write();
   outputfile->Close();
   std::cout << "ppppppp" << std::endl;
   return 0;
}

compiled with:

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

You probably want: dataTree->Branch("pp", pp, "pp[10]/I");

I think the link is ok! It just can’t run in windows! I can’t resolve it.

For branch of array , it’s ok! I have tried it! For the branch of vector , it’s failed!

How did you compile your code?

I use the VS debug button to link and run.
屏幕截图 2022-12-08 151549

Well, you must verify that you are using the same compiler flags than ROOT itself…

Sorry, I am not familiar with the VS. Would you tell me how can I check the compiler flags in VS? Thank you!

Read the online documentation of how to set the compiler options in Visual Studio for those flags:

Or, like I did, from a x86 Native Tools Command Prompt for VS 2022 (for 32 bit) or a x64 Native Tools Command Prompt for VS 2022 (for 64bit) use this command to compile your code:

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

I have added the complier flags into VS, by this following way:


But, the link is wrong,and the error is following:

How can I fix it? Thank You!

That won’t work. Try to build in RelWithDebInfo and instead of adding the flags in Additional options, try to find how to set them in Visual Studio options, as mentioned in the link I posted before. And if you build in Debug mode, make sure to link with the Release runtime libraries (/MD flag) and use the Debug version of ROOT. You cannot mix debug and release build with Visual Studio

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.