为什么C ++应用程序没有int main函数? [英] Why a C++ application does not have an int main function ?

查看:305
本文介绍了为什么C ++应用程序没有int main函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello World,我正在使用我的第一个C ++项目,即使没有此语言的经验。到目前为止,我已经读过一个C ++项目必须有一个初始函数名int Main,但是这个特定的没有任何。



为什么?是否可以为C ++应用程序设置另一个入口点?我怎么能这样做?,如果这是标准方法,我在哪里可以找到这个特殊功能?



提前致谢。



我尝试了什么:



我没有尝试任何关于这个问题的东西,该应用程序的工作原理很好,所以我非常好奇。

Hello World again, I´m working with my first C++ project, even without a previous experience in this language. So far by now I´ve read that a C++ project MUST have an initial function name "int Main", but this specific one just doesn´t have any.

Why ?. Is it posible to set another entry point for a C++ app ?. How can I do it ?, where can I find this particular function, if it´s a standard method ?

Thanks in advance.

What I have tried:

I have not tried nothing at all about this matter, the app works fine, so I´m very intrigued.

推荐答案

对于Windows GUI应用程序,有一个 WinMain入口点(Windows) [ ^ ](另请参阅演练:创建Windows桌面应用程序(C ++)) a> [ ^ ])。这将通过隐藏的 main 函数实现调用,该函数在构建时添加到您的程序中。



创建时MFC应用程序甚至 WinMain 实现都会自动添加到您的程序中。该实现调用 CWinApp 派生类的 InitInstance ,这是MFC应用程序的可见入口点。 />


如果要编写Windows控制台程序,则必须实现C / C ++样式的主函数。但是,可以使用Microsoft特定的扩展 _tmain wmain (它们再次被隐藏调用)提供Unicode和多字节字符串之间转换的实现)。另请参见 main:程序启动 [ ^ ]。
With Windows GUI applications, there is the WinMain entry point (Windows)[^] (see also Walkthrough: Creating Windows Desktop Applications (C++)[^]). This will be called by a hidden main function implementation that is added to your program upon building.

When creating a MFC application even a WinMain implementation is automatically added to your program. The implementation calls InitInstance of your CWinApp derived class which is then the "visible" entry point for MFC applications.

If you are going to write a Windows console program, you must implement a C/C++ style main function. However, there are the Microsoft specific extensions _tmain and wmain which can be used instead (they are again called by a hidden implementation that provides conversions between Unicode and multi byte character strings). See also main: Program Startup[^].


首先,请看:< a href =https://en.wikipedia.org/wiki/Entry_point#C_and_C.2B.2B>入口点 - 维基百科,免费的百科全书。



您的问题表明您关注的是返回 int 值。即使不是这样,知道它的作用也是一件好事。



这主要是历史问题。入口级函数的传统在所有情况下都返回0,除了异常终止的情况。在所有(或几乎所有)操作系统中,库都使用它来检查进程的状态。当进程终止时,您的代码可以从状态中读取此值。此外,当应用程序在一种或另一种批处理文件中同步执行时,启动命令可以返回此值,该值可以分配给某种批处理/脚本变量,可以进一步使用,例如,fork进一步执行。同时,这只是传统或文化,而不是任何标准。返回0的传统足够稳定,所有其他值完全取决于应用程序;如果它们是必不可少的,可以在特定于应用程序的文档中进行描述。如果使用的入口点函数返回为void,则仍然返回0到运行时系统;这是默认值。



随着时间的推移,这种技术的重要性下降了;大多数应用程序根本无视返回任何内容的可能有两个重要原因:结构异常处理和图形UI,以及在某种程度上,线程。从功能中恢复状态的可怕技术被消除;使用它将是愚蠢的。例外情况要强大得多。通常,所有应用程序都会捕获所有线程中的所有异常,并显示应用程序本身问题的更多信息性诊断。将异常抛入运行时系统,执行环境是相对罕见的,仅在最简单的应用程序中使用或表示低技术。此外,面向事件的UI应用程序(这意味着,几乎所有这些应用程序,如果你只采用体面的应用程序)捕获UI主事件中的所有异常并立即显示问题诊断。



-SA
First of all, please see: Entry point — Wikipedia, the free encyclopedia.

Your question suggests that your concern is the return int value. Even if it is not the case, knowing what it does is something good to know.

This is mostly a matter of history. There is a tradition for entry-level functions to return 0 in all cases except the cases of abnormal termination. In all (or almost all) OS, there are the libraries uses to check up the status of the process. When a process is terminated, your code can read this value from the status. Also, when the application is executed synchronously in one or another kind of a batch file, the starting command can return this value, which can be assigned to some kind of the batch/script variable which can further be used, for example, to fork further execution. At the same time, this is just the tradition or culture, not any kind of standard. The tradition to return 0 is stable enough, and all other values are totally application-dependent; if they are essential, they can be described in application-specific documentation. If the entry point function used the return is void, 0 is still returned to the runtime system; this is the default.

With time, the importance of this technique goes down; most applications simply ignore the possibility to return anything. There are two important reasons for that: structural exception handling and then graphics UI, and, to certain extent, threading. The dreaded technique of returning "status" from the function is eliminated; using it would be just silly. Exceptions are much more powerful. Normally, all applications catch all exceptions in all threads and show much more informative diagnostics of the problems in the application itself. Throwing the exceptions into the runtime system, execution environment, is relatively rare, used only in the simplest application or are indicative of low-tech. Moreover, event-oriented UI applications (that means, nearly all of them, if you take only the decent ones) catch all exceptions in the UI main event and show the problem diagnostics immediately.

—SA


这篇关于为什么C ++应用程序没有int main函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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