Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with: redeclaration of C++ built-in type ‘char16_t’

In a C++11 project I have to use external C library. This library main header file defines

typedef uint16_t        char16_t;  

And because of it compilation of the C++ program which includes this library fails, with the message:

redeclaration of C++ built-in type ‘char16_t’

The only idea I have is to repackage whole library but because char16_t is pervasive in this library it would be very time consuming (if even possible). Are there some sensible ways of dealing with this problem?

Edit:

I have also another idea of removing problematic line and replacing every occurrence of char16_t with uint16_t but I would have to modify third party library headers and I do not particularly like this idea (there can be more similar errors). So I also wonder whether there is some good way of dealing of broader problem of incompatibilities between C++ and C when including headers.

like image 581
Trismegistos Avatar asked Jun 23 '14 15:06

Trismegistos


2 Answers

You could use a macro to rename the library type while keeping it unrelated to the new language type char16_t:

#define char16_t LIBRARY_char16_t
#include <library>
#undef char16_t

Then, the library header will be compiled in your code base such that the typedef has the name LIBRARY_char16_t.

The library itself is still compiled such that the type in question is typedef'ed to uint16_t so you should not attempt to change this (by e.g. removing the typedef) in order to stay binary-compatible with the compiled library.

like image 131
leemes Avatar answered Nov 15 '22 06:11

leemes


C++11 defines char32_t and char16_t as built in types. This error only happens if you are using C++11. i.e. in your Application.mk file you have:

APP_CPPFLAGS += -std=c++11

You can either remove the C++11 support, OR use the following workaround, that should probably be a part of the official Android source (if not already).

in file /frameworks/native/include/utils/Unicode.h

#if __cplusplus <= 199711L
typedef uint32_t char32_t;
typedef uint16_t char16_t;
#endif

It is based on answers from a question about char16/32_t with C++11

like image 43
Shoham Avatar answered Nov 15 '22 05:11

Shoham