为什么有一个定义_tmain的宏?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Visual C++ 特有的功能,不是 C++ 的一部分。
大多数 Windows API 函数都有两个版本:以
W
结尾的版本,用于宽字符串(wchar_t
字符串)和以结尾的版本A
,用于窄字符串(char
字符串)。实际的 Windows API“函数”没有任何后缀,并被定义为宏,根据设置扩展到正确的版本。T
名称(例如_TCHAR
和_tmain
)具有相同的目的:它们是根据您的编译设置扩展为正确名称的宏,因此wchar_t
和wmain
用于宽字符支持,或char
和main
用于窄字符支持。这个想法是,如果您使用与字符类型无关的名称(
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 inA
, 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, sowchar_t
andwmain
for wide character support, orchar
andmain
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.因为 Microsoft 认为向 C++ 添加 Unicode 支持的最佳方法是添加
TCHAR
类型,该类型 #define 为char
或wchar_t
取决于项目属性的价值>配置属性>一般>字符集。_tmain
也 #define 为main
(需要char
)或wmain
(需要wchar_t
s) 取决于该设置。Because Microsoft decided that the best way to add Unicode support to C++ was to add a
TCHAR
type, which is #defined to eitherchar
orwchar_t
depending on the value of Project Properties > Configuration Properties > General > Character Set._tmain
is also #defined to eithermain
(which takeschar
s) orwmain
(which takeswchar_t
s) depending on that setting.