[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


Which library contains the following symbol? std::char_tr...

<< Back to: Available C++ Libraries FAQ

Question by Code
Submitted on 10/14/2003
Related FAQ: Available C++ Libraries FAQ
Rating: Rate this question: Vote
Which library contains the following symbol?

std::char_traits<unsigned short>::assign(unsigned short*, unsigned, unsigned short)

When i compile program with stdc++ library, error message comes with:
std::char_traits<unsigned short>::assign(unsigned short*, unsigned, unsigned short);
undefined reference to `std::char_traits<unsigned short>::copy(unsigned short*, unsigned short const*, unsigned)'
undefined reference to `std::char_traits<unsigned short>::compare(unsigned short const*, unsigned short const*, unsigned)'
...

gcc and glibc are gcc-3.2.2-5 glibc-2.3.2-4.80.6

What happened?


Answer by Ed Zavada
Submitted on 8/29/2005
Rating: Not yet rated Rate this answer: Vote
The problem is that this should be in the libstdc++ library, but it's not. The solution is to create a new source file (let's call it gcc_fix.cpp) with the following code:

#include <cwchar>
#include <bits/char_traits.h>


namespace std {

  typedef fpos<mbstate_t>       wstreampos;

  /// char_traits specializations
  template<>
    struct char_traits<unsigned short>
    {
      typedef unsigned short    char_type;
      typedef unsigned int        int_type;
      typedef streamoff    off_type;
      typedef wstreampos    pos_type;
      typedef mbstate_t    state_type;
      
      static void
      assign(char_type& __c1, const char_type& __c2)
      { __c1 = __c2; }

      static bool
      eq(const char_type& __c1, const char_type& __c2)
      { return __c1 == __c2; }

      static bool
      lt(const char_type& __c1, const char_type& __c2)
      { return __c1 < __c2; }

      static int
      compare(const char_type* __s1, const char_type* __s2, size_t __n)
      { return wmemcmp((wchar_t*)__s1, (wchar_t*)__s2, __n); }

      static size_t
      length(const char_type* __s)
      { return wcslen((wchar_t*)__s); }

      static const char_type*
      find(const char_type* __s, size_t __n, const char_type& __a)
      { return (char_type*)wmemchr((wchar_t*)__s, __a, __n); }

      static char_type*
      move(char_type* __s1, const char_type* __s2, int_type __n)
      { return (char_type*)wmemmove((wchar_t*)__s1, (wchar_t*)__s2, __n); }

      static char_type*
      copy(char_type* __s1, const char_type* __s2, size_t __n)
      { return (char_type*)wmemcpy((wchar_t*)__s1, (wchar_t*)__s2, __n); }

      static char_type*
      assign(char_type* __s, size_t __n, char_type __a)
      { return (char_type*)wmemset((wchar_t*)__s, __a, __n); }

      static char_type
      to_char_type(const int_type& __c) { return char_type(__c); }

      static int_type
      to_int_type(const char_type& __c) { return int_type(__c); }

      static bool
      eq_int_type(const int_type& __c1, const int_type& __c2)
      { return __c1 == __c2; }

      static int_type
      eof() { return static_cast<int_type>(WEOF); }

      static int_type
      not_eof(const int_type& __c)
      { return eq_int_type(__c, eof()) ? 0 : __c; }
  };
  
}

Then you need to compile it and link it into your project. Be sure to use the following flags when you compile: -fno-inline -fno-default-inline -fshort-wchar

The no-inline flags ensure that the missing functions are actually generated and put into the object file. The short-wchar flag is essential for making the implementation provided work correctly, otherwise it will be 32 bit wchar_t and the wxxx() functions used in implementation will be working with the correct data sizes

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Available C++ Libraries FAQ


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.