Problems with c++ 'string' and how to compile a shared objec

Hello,
First the problem: Root (3.05 on linux) doesn’t seem to handle c++ strings very well. I have two examples below of code that compiles and run fine as standard c++. This runs fine on Root 3.10 on cygwin. Does anybody know what’s going on here?

What I do to get around this problem is use a CDF makefile to compile the code into a shared object and load this code.  The problem is that I can't use the same makefile on cygwin.  So, what makefile can I use?  Is it possible to have one makefile that doesn't change (too much) going back and forth between SGIs, linux, and cygwin?

Thanks for your help ,
  Charles

cplager+root@physics.ucla.edu


— try2.c —
#include
#include

using namespace std;
// global variable
const string kFred[1] = {“Fred Flinstone”};

void try2()
{
cout << "hi " << kFred[0] << endl;
}


— load2.c —
{
gSystem->CompileMacro(“try2.c”, “k”);
try2();
}


---- output of load 2 —
cdf_val@b0dap30> root -l -b -q load2.c
root [0]
Processing load2.c…
Info in TUnixSystem::ACLiC: creating shared library /mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/try2_c.so
Error: Illegal initialization of kFred[1]. Constructor exists FILE:/mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/try2.c LINE:6
Warning: Error occured during reading source files
Warning: Error occured during dictionary source generation
!!!Removing /mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/fileYwme1W.cxx /mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/fileYwme1W.h !!!
Error: rootcint: error loading headers…
Error in : Dictionary generation failed!
Error: Function try2() is not defined in current scope FILE:load2.c LINE:3
Possible candidates are…
filename line:size busy function type and name
*** Interpreter error recovered ***
cdf_val@b0dap30>


— try3.c —
#include
#include

class Blah
{
public:
static std::string name() {return m_name;}
private:
static std::string m_name;
};

using namespace std;
string Blah::m_name = “Fred”;

void try3()
{
cout << "hi " << Blah::name() << endl;
}


— output of root —
cdf_val@b0dap30> root -l -b -q load3.c
root [0]
Processing load3.c…
Info in TUnixSystem::ACLiC: creating shared library /mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/try3_c.so
"/mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/fileYnEHbG.cxx", line 351: error #20:
identifier “m_name” is undefined
G__memvar_setup((void*)(&m_name),117,0,0,G__get_linked_tagnum(&G__fileYnEHbGLN_basic_stringlEcharcOchar_traitslEchargRcOallocatorlEchargRsPgR),G__defined_typename(“string”),-1,1,“m_name=”,0,(char*)NULL);
^

1 error detected in the compilation of “/mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/fileYnEHbG.cxx”.
KCC: Compilation failed.
Error: “/mnt/autofs/cdf/cdf-cf-data1/cdf_val/CrossSectionFits/newArea/fileYnEHbG.o”: No such file or directory.
Error in : Compilation failed!
Error: Function try3() is not defined in current scope FILE:load3.c LINE:3
Possible candidates are…
filename line:size busy function type and name
*** Interpreter error recovered ***


— output of try2 and try3 when compiled using g++ —
— (only change I made was void try2 -> void main) —
cdf_val@b0dap30> try2
hi Fred Flinstone
cdf_val@b0dap30> try3
hi Fred

Hi Charles,

CINT has some difficulties with some global variable initialization.

Instead of
const string kFred[1] = {“Fred Flinstone”};
Use
#ifdef CINT
const string kFred[1];
#else
const string kFred[1] = {“Fred Flinstone”};
#endif

Insteaf of
using namespace std;
string Blah::m_name = “Fred”;
do
using namespace std;
#ifndef CINT
string Blah::m_name = “Fred”;
#endif

Cheers,
Philippe