C++中_tmain()和main()有什么区别? [英] What is the difference between _tmain() and main() in C++?

查看:31
本文介绍了C++中_tmain()和main()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下 main() 方法运行我的 C++ 应用程序,一切正常:

If I run my C++ application with the following main() method everything is OK:

int main(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

我得到了我的期望并且我的论点被打印出来了.

I get what I expect and my arguments are printed out.

但是,如果我使用 _tmain:

However, if I use _tmain:

int _tmain(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

它只显示每个参数的第一个字符.

It just displays the first character of each argument.

造成这种情况的区别是什么?

What is the difference causing this?

推荐答案

_tmain 在 C++ 中不存在.main 可以.

_tmain does not exist in C++. main does.

_tmain 是微软的扩展.

main 根据 C++ 标准,是程序的入口点.它具有以下两个签名之一:

main is, according to the C++ standard, the program's entry point. It has one of these two signatures:

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

微软添加了一个 wmain,用这个替换第二个签名:

Microsoft has added a wmain which replaces the second signature with this:

int wmain(int argc, wchar_t* argv[]);

然后,为了更容易地在 Unicode (UTF-16) 和它们的多字节字符集之间切换,他们定义了 _tmain,如果启用了 Unicode,它会被编译为 wmain,否则为 main.

And then, to make it easier to switch between Unicode (UTF-16) and their multibyte character set, they've defined _tmain which, if Unicode is enabled, is compiled as wmain, and otherwise as main.

至于你问题的第二部分,谜题的第一部分是你的主要功能是错误的.wmain 应该采用 wchar_t 参数,而不是 char.由于编译器不会对 main 函数强制执行此操作,因此您会得到一个程序,其中将 wchar_t 字符串数组传递给 main 函数, 将它们解释为 char 字符串.

As for the second part of your question, the first part of the puzzle is that your main function is wrong. wmain should take a wchar_t argument, not char. Since the compiler doesn't enforce this for the main function, you get a program where an array of wchar_t strings are passed to the main function, which interprets them as char strings.

现在,在启用 Unicode 时 Windows 使用的字符集 UTF-16 中,所有 ASCII 字符都表示为一对字节 \0 后跟 ASCII 值.

Now, in UTF-16, the character set used by Windows when Unicode is enabled, all the ASCII characters are represented as the pair of bytes \0 followed by the ASCII value.

由于 x86 CPU 是小端的,这些字节的顺序被交换,所以 ASCII 值在前,然后是空字节.

And since the x86 CPU is little-endian, the order of these bytes are swapped, so that the ASCII value comes first, then followed by a null byte.

而在一个字符字符串中,字符串通常是如何终止的?是的,通过一个空字节.所以你的程序看到一堆字符串,每个一个字节长.

And in a char string, how is the string usually terminated? Yep, by a null byte. So your program sees a bunch of strings, each one byte long.

通常,在进行 Windows 编程时,您有三个选择:

In general, you have three options when doing Windows programming:

  • 显式使用 Unicode(调用 wmain,对于每个接受字符相关参数的 Windows API 函数,调用函数的 -W 版本.而不是 CreateWindow,调用 CreateWindowW).而不是使用 char 使用 wchar_t,等等
  • 明确禁用 Unicode.调用 main 和 CreateWindowA,并使用 char 作为字符串.
  • 允许两者.(调用 _tmain 和 CreateWindow,它们解析为 main/_tmain 和 CreateWindowA/CreateWindowW),并使用 TCHAR 而不是 char/wchar_t.
  • Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the -W version of the function. Instead of CreateWindow, call CreateWindowW). And instead of using char use wchar_t, and so on
  • Explicitly disable Unicode. Call main, and CreateWindowA, and use char for strings.
  • Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_t.

同样适用于 windows.h 定义的字符串类型:LPCTSTR 解析为 LPCSTR 或 LPCWSTR,并且对于包含 char 或 wchar_t 的所有其他类型,始终存在可以使用的 -T- 版本.

The same applies to the string types defined by windows.h: LPCTSTR resolves to either LPCSTR or LPCWSTR, and for every other type that includes char or wchar_t, a -T- version always exists which can be used instead.

请注意,所有这些都是 Microsoft 特定的.TCHAR 不是标准的 C++ 类型,它是 windows.h 中定义的宏.wmain 和 _tmain 也仅由 Microsoft 定义.

Note that all of this is Microsoft specific. TCHAR is not a standard C++ type, it is a macro defined in windows.h. wmain and _tmain are also defined by Microsoft only.

这篇关于C++中_tmain()和main()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆