title: Sun CC not linking to or compiling in fstream template in Shared Object
body: <p>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.</p>
  
  <p>I've pulled this out of the legacy system I am working and reproduced it here in this example.</p>
  
  <p>The C / C++ shared object (main.c):</p>
  
  <pre><code>extern "C" {
  int xmain()
  {
      fstream logstr("/tmp/log.txt", ios_base::out | ios_base::app);
      if(logstr.is_open())
      {
          logstr&lt;&lt;"Helloworld"&lt;&lt;endl;
          logstr.close();
      }
      printf("Hello world\n");
      return 0;
  }
  }
  </code></pre>
  
  <p>And a C main program (main2.c) :</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Compile the library</p>
  
  <pre><code>CC -g  -o main.o -c main.C
  CC -g -G -o libmain.so main.o -lCrun
  </code></pre>
  
  <p>Compile the C main program</p>
  
  <pre><code>cc -g -o main2.o -c main2.c
  cc -g -o main2 main2.o -ldl
  </code></pre>
  
  <p>Running this produces the following error on SunOS 5.8:</p>
  
  <pre><code>./main2 | c++filt
  Loading library
  Failed to open ld.so.1: main2: fatal: relocation error: file ./libmain.so: symbol std::basic_fstream&lt;char,std::char_traits&lt;char&gt; &gt;::~basic_fstream(): referenced symbol not found
  </code></pre>
  
  <p>This is confirmed by an nm:</p>
  
  <pre><code>nm libmain.so | c++filt | grep stream
  [55]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream&lt;char,std::char_traits&lt;char&gt; &gt;::~basic_fstream()
  [65]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream&lt;char,std::char_traits&lt;char&gt; &gt;::basic_fstream(const char*,int,long)
  [64]    |         0|       0|FUNC |GLOB |0    |UNDEF  |void std::basic_fstream&lt;char,std::char_traits&lt;char&gt; &gt;::close()
  [53]    |         0|       0|FUNC |GLOB |0    |UNDEF  |bool std::basic_fstream&lt;char,std::char_traits&lt;char&gt; &gt;::is_open()
  </code></pre>
  
  <p>Compiler version I am using:</p>
  
  <pre><code>CC -V
  CC: Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-11 2002/10/31
  </code></pre>
  
  <p>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?).</p>
  
  <p>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).</p>