I have an issue with Sun CC (6.2 mainly, but also seems to happen with 12.1) where by I cannot seem to either compile in or link to the fstream template in a Shared Object.
I've pulled this out of the legacy system I am working and reproduced it here in this example.
The C / C++ shared object (main.c):
extern "C" {
int xmain()
{
fstream logstr("/tmp/log.txt", ios_base::out | ios_base::app);
if(logstr.is_open())
{
logstr<<"Helloworld"<<endl;
logstr.close();
}
printf("Hello world\n");
return 0;
}
}
And a C main program (main2.c) :
int main()
{
void *fd;
xman *xx;
printf("Loading library\n");
fd = dlopen("libmain.so", RTLD_GLOBAL | RTLD_NOW);
if(fd==NULL)
{
printf("Failed to open %s\n",dlerror());
return -1;
}
printf("library loaded\n");
xx = (xman *)dlsym(fd, "xmain");
(*xx)();
return 0;
}
Compile the library
CC -g -o main.o -c main.C
CC -g -G -o libmain.so main.o -lCrun
Compile the C main program
cc -g -o main2.o -c main2.c
cc -g -o main2 main2.o -ldl
Running this produces the following error on SunOS 5.8:
./main2 | c++filt
Loading library
Failed to open ld.so.1: main2: fatal: relocation error: file ./libmain.so: symbol std::basic_fstream<char,std::char_traits<char> >::~basic_fstream(): referenced symbol not found
This is confirmed by an nm:
nm libmain.so | c++filt | grep stream
[55] | 0| 0|FUNC |GLOB |0 |UNDEF |std::basic_fstream<char,std::char_traits<char> >::~basic_fstream()
[65] | 0| 0|FUNC |GLOB |0 |UNDEF |std::basic_fstream<char,std::char_traits<char> >::basic_fstream(const char*,int,long)
[64] | 0| 0|FUNC |GLOB |0 |UNDEF |void std::basic_fstream<char,std::char_traits<char> >::close()
[53] | 0| 0|FUNC |GLOB |0 |UNDEF |bool std::basic_fstream<char,std::char_traits<char> >::is_open()
Compiler version I am using:
CC -V
CC: Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-11 2002/10/31
Should I be trying to convince Sun CC to compile fstream into my library? Or is there a library which I can link against? I can't change to using stlport4 (http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html) as it's not available on Sun CC 6.2 (or 5.3... what's with the version numbering?).
This seems to work fine when compiling in Linux and linking against libstdc++. I'm guessing that fstream is compiled into libstdc++? (nm seems to confirm this).