为什么有一个定义_t​​main的宏?

发布于 2024-10-16 11:31:40 字数 551 浏览 1 评论 0原文

我是 C++ 编码新手,有 Java 和 C# 背景。我对 #define 术语的激增感到困惑,从最基本的开始:

#define _tmain wmain

当我多年前第一次学习一点 C 语言时,主要功能是:

int main(int argc, char *argv[])

在我创建的 Visual C++ 项目中,它的主要功能是:

int _tmain(int argc, _TCHAR* argv[])

我只是想知道为什么需要将名称从 wmain 转换为 _tmain?为什么不直接使用原始的 C main 函数原型呢?

一般来说,似乎有很多 #define 重命名一些看起来很清楚的东西,一些看起来更神秘和不太清楚的东西(我的意思是 wmain_tmain ? ?)。

感谢您容忍这个可能是一个非常明显的问题。

I am new to C++ coding, coming from Java and C# background. I'm puzzled by the proliferation of #define terms starting with the most basic:

#define _tmain wmain

When I first learned a smattering of C ages ago, the main function was:

int main(int argc, char *argv[])

In the Visual C++ project I created, it made the main function:

int _tmain(int argc, _TCHAR* argv[])

I'm just wondering why there needed to be a name translation from wmain to _tmain? Why not just use the original C main function prototype?

In general there seems to be lot of #define renaming something which looks pretty clear to start with, to something which looks more mysterious and less clear (I mean wmain to _tmain ??).

Thanks for tolerating what may be a very obvious question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-23 11:31:40

这是 Visual C++ 特有的功能,不是 C++ 的一部分。

大多数 Windows API 函数都有两个版本:以 W 结尾的版本,用于宽字符串(wchar_t 字符串)和以 结尾的版本A,用于窄字符串(char 字符串)。实际的 Windows API“函数”没有任何后缀,并被定义为宏,根据设置扩展到正确的版本。

T 名称(例如 _TCHAR_tmain)具有相同的目的:它们是根据您的编译设置扩展为正确名称的宏,因此 wchar_twmain 用于宽字符支持,或 charmain 用于窄字符支持。

这个想法是,如果您使用与字符类型无关的名称(T 名称)编写代码,则可以将代码编译为使用窄字符 (ASCII) 或宽字符 (Unicode),而无需更改它。代价是您的代码的可移植性较差。

This is a Visual C++-specific feature, it's not a part of C++.

Most of the Windows API functions have two versions: those that end in W, which are for use with wide character strings (wchar_t strings) and those that end in A, which are for use with narrow character strings (char strings). The actual Windows API "functions" don't have any suffix and are defined as macros that expand to the right version depending on the settings.

The T names (like _TCHAR and _tmain) are for the same purpose: they are macros that expand to the right name depending on your compilation settings, so wchar_t and wmain for wide character support, or char and main for narrow character support.

The idea is that if you write your code using the character-type-agnostic names (the T names), you can compile your code to use narrow characters (ASCII) or wide characters (Unicode) without changing it. The tradeoff is that your code is less portable.

白昼 2024-10-23 11:31:40

因为 Microsoft 认为向 C++ 添加 Unicode 支持的最佳方法是添加 TCHAR 类型,该类型 #define 为 charwchar_t取决于项目属性的价值>配置属性>一般>字符集。 _tmain 也 #define 为 main (需要 char)或 wmain (需要 wchar_ts) 取决于该设置。

Because Microsoft decided that the best way to add Unicode support to C++ was to add a TCHAR type, which is #defined to either char or wchar_t depending on the value of Project Properties > Configuration Properties > General > Character Set. _tmain is also #defined to either main (which takes chars) or wmain (which takes wchar_ts) depending on that setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文