How to deal with: overriding the built-in type C ++ 'char16_t

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

typedef uint16_t char16_t; 

And because of this, compiling a C ++ program that includes this library fails, with the message:

 redeclaration of C++ built-in type 'char16_t' 

The only idea I have is to repackage the entire library, but since char16_t common in this library, it will be very time consuming (at least possible). Are there any reasonable ways to solve this problem?

Edit:

I have another idea to remove the problematic line and replace every occurrence of char16_t with uint16_t, but I would have to change the headers of third-party libraries, and I don't particularly like this idea (there may be other similar errors). So I also wonder if there is a good way to solve the wider incompatibility problem between C ++ and C when including headers.

+6
source share
2 answers

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

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

Then the library header will be compiled into your code base so that typedef is named LIBRARY_char16_t .

The library itself is still compiled in such a way that the type in question is typedef'ed to uint16_t , so you should not try to change this (for example, by deleting typedef) to remain binary compatible with the compiled library.

+12
source

C ++ 11 defines char32_t and char16_t as built-in types. This error only occurs when using C ++ 11. that is, in the Application.mk file that you have:

 APP_CPPFLAGS += -std=c++11 

You can either remove support for C ++ 11, OR , using the following workaround, which should probably be part of the official Android source (if it hasn't already been).

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 the answers of the question about char16 / 32_t with C ++ 11

+2
source

Source: https://habr.com/ru/post/971241/


All Articles