C++ Linux 上的 tchar.h

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/895804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:49:37  来源:igfitidea点击:

tchar.h on linux

c++linuxinternationalizationtchar

提问by Sudarshan

I am trying to write cross platform i18n C++ code. Since most linux system prefer to use UTF-8 as the character encoding, I thought that I should use string on linux and wstring on Windows. Is tchar.h available on linux? What is an equivalent replacement on for tchar.h on Linux?

我正在尝试编写跨平台 i18n C++ 代码。由于大多数 linux 系统更喜欢使用 UTF-8 作为字符编码,我认为我应该在 linux 上使用 string,在 Windows 上使用 wstring。tchar.h 在 linux 上可用吗?Linux 上 tchar.h 的等效替代品是什么?

回答by Paul Sonier

You may find thisarticle to be useful. In particular, near the end they discuss a bit about using TCHAR and dealing with Windows code.

您可能会发现这篇文章很有用。特别是,在接近尾声时,他们讨论了一些关于使用 TCHAR 和处理 Windows 代码的问题。

The article summarization is:

文章总结如下:

TCHAR will be translated into a wide character data type when compiling this code with the GNU C Compiler (most portable libraries define TCHAR in their headers and refer to wchar_t). This, in fact, was how I turned my C++ program into an anagram generator: I used standard C++ strings filled with UTF-8 and fed the data with pointers casted to wchar_t to library functions. UTF-8 data interpreted as UTF-32 equals garbage (but it is tremendously useful for obfuscation of data and bugs).

使用 GNU C 编译器编译此代码时,TCHAR 将被转换为宽字符数据类型(大多数可移植库在其头文件中定义 TCHAR 并参考 wchar_t)。事实上,这就是我如何将我的 C++ 程序变成一个字谜生成器:我使用标准的 C++ 字符串填充了 UTF-8,并将数据与转换为 wchar_t 的指针提供给库函数。解释为 UTF-32 的 UTF-8 数据等于垃圾(但它对于混淆数据和错误非常有用)。

回答by J.R. Heisey

To build my Windows centric source I was able to use the tchar.h file from Cygwin's found in the cygwin\usr\include\w32api folder.

为了构建以 Windows 为中心的源代码,我可以使用 Cygwin\usr\include\w32api 文件夹中的 tchar.h 文件。

I am actually building using Android NDK. For my purposes I made a copy of tchar.h and patched it.

我实际上是使用 Android NDK 构建的。为了我的目的,我复制了 tchar.h 并修补了它。

//#include <crtdefs.h>

#define _CRTIMP

//#define _strninc(_pc,_sz) (((_pc)+(_sz)))
//  _CRTIMP size_t __cdecl __strncnt(const char *_Str,size_t _Cnt);