Compiling in MS Visual C++ 6 (solved)

Hi,
I started with root yesterday by downloading the binary:
ftp://root.cern.ch/root/root_v4.02.00.win32gdk.tar.gz

I have windows XP on my pc and am running MS VC++ 6.0 Standard.
I have tried to build a file that works in root using the standard VC++ method. I followed the steps given in parts 1-3 of the document associated with the binary i downloaded. I was unable to understand section 4 :blush: at all, I don’t even know what the solution explorer window is! So, I followed the instructions (the steps labelled 1 and 2) given here…

d0.phys.washington.edu/~haas/win … iling.html

… to tell VC++ where the root lib and .h files are located. This worked for the test hworld.cxx. However the executable that is created and stored in the debug folder only runs on my pc. On other pc’s you get an error saying that libcore.dll cannot be found (+ other stuff). Anyway, I thought that the build and linking process included the appropriate code so that the executable would work on any pc??

I would really grateful if someone could explain the recipe for building an executable using VC++ and the test file hworld.cxx that would work on all pc’s.

Thanks

Gareth

Hi Gareth,
the best way is to write a script that you compile on your computer using root itself:

root root [0] .L myScript.C+will create a file called myScript_C.dll. You can copy this file to another computer, start root there, and call “.L myScript_C.dll” and then call a method of your macro.

If you build an executable (that you then copy to another computer etc) you’ll have to ship the root dlls with it, and you’ll have to build it in release mode, as the other computer will probably not have the windows debug libraries. All this is far more complicated than just installing root on the other pc, and then loading your dll.

Axel.

thank you Axel, I understood that the executable should be standalone (ie. would inclube all the code it required to run) once you had linked it. Obviously i am wrong about this.

Anyway, does the fact that the test program compiles ok mean that i have set up things ok? I ask because I have written a program that works when executed in root, but when i try to compile it in C++ i get the following error:

[quote]Compiling…
Kinematic_Factor.cpp
c:\root\include\riosfwd.h(25) : error C2874: using-declaration causes a multiple declaration of 'istream’
c:\program files\microsoft visual studio\vc98\include\iosfwd(256) : see declaration of 'istream’
c:\root\include\riosfwd.h(26) : error C2874: using-declaration causes a multiple declaration of 'ostream’
c:\program files\microsoft visual studio\vc98\include\iosfwd(257) : see declaration of 'ostream’
Error executing cl.exe.

Kinematic_Factor.obj - 2 error(s), 0 warning(s)[/quote]

The only 2 root classes i use (and hence include) are:
TH1D.h and TCanvas.h

Gareth

Hi Gareth,
try to #include “Windows4Root.h” before including the first root header. Does that fix it? And no, linking does not mean that you’ve just build a stand-alone exe - it will still need the root dlls, and even more files from a root installation at run time.
Axel.

no, it doesn’t fix the problem. I still get the error as shown in my last message. I have posted the code i have written below, having removed a couple of fuctions so it is easy to see what i am doing:

[code]
#include “Windows4Root.h”
#include “iostream.h”
#include “TCanvas.h”
#include “TH1D.h”

//Functions//

double kinematic_facto(double m_ion_kg, double m_target_kg, short i)
{
double result, radians;
radians = i * ((3.1427 * 2) / 360);
result = pow((pow((pow(m_target_kg, 2) - pow(m_ion_kg, 2) * pow(sin(radians), 2)), 0.5) + m_ion_kg * cos(radians)) / (m_target_kg + m_ion_kg), 2); // calculation of kinematic factor
return result; // return result to main program
}

/////////////////////////// MAIN PROGRAM //////////////////////////////////
int main()
{
double angle[181];
int z_ion, z_target, choice;
double m_ion, m_ion_kg, m_target, m_target_kg, theta, KF, E_ion, RCS, cRCS, E;
const double e = 1.6e-19, pi= 3.1427, amu = 6.61e-31;
cout << “What do you want to calculate?” << endl;
cout << “kinematic factor = 1” << endl;
cout << “Rutherford cross section = 2” << endl;
cout << “Corrected cross section = 3” << endl;
cin >> choice; // read in user choice
if (choice == 1)
{
cout << “Please enter the mass of the incident ion (amu):”;
cin >> m_ion; // read in ion mass
m_ion_kg = m_ion * amu; // convert to kg
cout << "Please enter the mass of the target atom (amu): ";
cin >> m_target; // read in target mass
m_target_kg = m_target * amu;
for (short i=0; i < 181; i++)
{
KF = kinematic_facto(m_ion_kg, m_target_kg, i); // call kinematic factor function
angle[i] = KF;
cout << i << " "<< angle[i] << endl; // display result
}

	// Create canvas for graph
	{
	TCanvas *c1 = new TCanvas( "c1", "Kin_Fac",180,0);
	TH1D *hist = new TH1D( "hist", "Kin_Fac", 180, 0, 180);
	for(i= 0; i<181; i++);
		{
		hist->Fill(i, angle[i]);
	}
hist->Draw();
c1-> Update();
}
}

else
	cout << "please enter 1 as this is a test" << endl;	

return 0;

}

////////////////////////////////// END OF PROGRAM /////////////////////////////////////////////////////////////////////////[/code]

note, with minor adjustments this program runs in root itself but gives the iostream error quoted above when i try to compile in VC++. Also note i am quite new to C++ and am just trying to get simple programs to work before moving on.

Hi Gareth,

In your code, replace

#include “iostream.h”

by

#include “Riostream.h”

It will works :wink:

Cheers,
Bertrand.

Then i get:

Compiling...
Kinematic_Factor.cpp
c:\root\include\tdatime.h(85) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(85) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(87) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(87) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(89) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(89) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(91) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(91) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(93) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(93) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(95) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
c:\root\include\tdatime.h(95) : error C2248: 'fDatime' : cannot access protected member declared in class 'TDatime'
        c:\root\include\tdatime.h(32) : see declaration of 'fDatime'
Error executing cl.exe.

Kinematic_Factor.obj - 12 error(s), 0 warning(s)

There is a bug in MSVC6, it has been solved in one service pack.
Try to apply latest service pack for MSVC6 (or move to MSVC7).
This will solve the problem.

Bertrand.

Here is the link :

http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp5/default.aspx

HTH,
Bertrand.

ok, updating to service pack 5 as i type. I’ll let you know how it goes

right,
the code now compiles/builds and runs. However when it should be plotting the graph on the canvas i create it says the following in the command window that it opens:

Warning in TWinNTSystem::SetProgName: Wrong Program path

any idea what this is about, or is it merely down to my code doing somethng wrong.

All help recieved so far has been brilliant, cheers guys :smiley:

Take a look at your modified code below :

#include "Windows4Root.h" 
#include "TApplication.h"
#include "RIOStream.h" 
#include "TCanvas.h" 
#include "TH1D.h" 


//Functions// 

double kinematic_facto(double m_ion_kg, double m_target_kg, short i) 
{ 
   double result, radians; 
   radians = i * ((3.1427 * 2) / 360); 
   result = pow((pow((pow(m_target_kg, 2) - 
            pow(m_ion_kg, 2) * 
            pow(sin(radians), 2)), 0.5) + 
            m_ion_kg * cos(radians)) / 
            (m_target_kg + m_ion_kg), 2);    // calculation of kinematic factor 
   return result;                            // return result to main program 
} 

/////////////////////////// MAIN PROGRAM ////////////////////////////////// 
int main(int argc,char *argv[]) 
{ 
   double angle[181];             
   int z_ion, z_target, choice; 
   double m_ion, m_ion_kg, m_target, m_target_kg, theta, KF, E_ion, RCS, cRCS, E; 
   const double e = 1.6e-19, pi= 3.1427, amu = 6.61e-31; 

   TApplication *myroot = new TApplication("MyRoot", &argc, argv);
   
   cout << "What do you want to calculate?" << endl; 
   cout << "kinematic factor = 1" << endl; 
   cout << "Rutherford cross section = 2" << endl; 
   cout << "Corrected cross section = 3" << endl; 
   cin >> choice;                                                  // read in user choice 
   if (choice == 1) { 
      cout << "Please enter the mass of the incident ion (amu):"; 
      cin >> m_ion;                                                // read in ion mass 
      m_ion_kg = m_ion * amu;                                      // convert to kg 
      cout << "Please enter the mass of the target atom (amu): "; 
      cin >> m_target;                                             // read in target mass 
      m_target_kg = m_target * amu; 
      for (short i=0; i < 181; i++) { 
         KF = kinematic_facto(m_ion_kg, m_target_kg, i);           // call kinematic factor function 
         angle[i] = KF; 
         cout << i << "   "<< angle[i] << endl;                    // display result 
      } 

      // Create canvas for graph 
      TCanvas *c1 = new TCanvas( "c1", "Kin_Fac",50, 50, 600, 400);
      TH1D *hist = new TH1D( "hist", "Kin_Fac", 180, 0, 180); 
      for(i= 0; i<181; i++) { 
         hist->Fill(i, angle[i]); 
      } 
      hist->Draw(); 
      c1->Modified();
      c1->Update(); 
   } 

   else 
      cout << "please enter 1 as this is a test" << endl;    
    
   myroot->Run(kTRUE);
   delete myroot;
   return 0; 
} 

////////////////////////////////// END OF PROGRAM /////////////////////////////////////////////////////

I added TApplication() and slightly modified your code.

Bertrand.

CustomReAlloc2>: passed oldsize 200, should be 458778 Fatal in <CustomReAlloc2>: unreasonable size (458778) aborting Warning in <TWinNTSystem::StackTrace>: this method must be overridden!is what i obtain now

does it run on your C++?

sorry about this, once i get one program to run i’ll leave you alone i promise

Do not link with libNew.lib.

Bertrand.

cheers bertrand, you just made my day. :smiley:
I was just reading about this libnew.lib issue on the other help threads. I’ll now get back to the manual and learn how to do things properly!

Always happy to help :stuck_out_tongue:
And I confirm that reading the documentation could be a good idea :wink:
Cheers, Bertrand.