This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Nakuli Patidar wrote:1>Is main() function a User Define Function or Pre Defined Function?
main() is a user-defined function, which must have a particular signature, and must not be called directly from within a C++ program (though it may be called from within a C program).
Nakuli Patidar wrote:
2>Can i run a c/c++ program without using main() function?
Every C and C++ program must have a main() function, unless it is running on a "freestanding implementation", (a term used to imply a limited implementation, probably on an embedded system without an underlying OS). However, that main() function may be provided by a library, so the user-provided entry point is different.
AJ's knowledge of the C++ spec is superb, and I bow to his knowledge. I am being sincere, not sarcastic. In all my decades of C and C++ programming, I never tried calling main() from within main() -- but I find AJ's comment fascinating.
I have one thing to add: Generally, you are going to need a main() in an freestanding program (such as an EXE file in Windows or old MS-DOS)... otherwise, there is no entry point.
However, it strikes me that certain kinds of executables -- specifically DLLs or Windows programs -- may not have a main() function. That is because their entry points are through other means. A Windows program, for example, is in some ways more like a DLL (dynamic-link library) than a freestanding execution. Windows will call the program through its own special entry point that it recognizes (WinMain, not predefined in C or C++ itself).
AJ -- correct me on this if I'm not mistaken.
But all that really matters is the following: you obviously need a main() function in a freestanding program (as opposed to a program called by Windows or through a DLL) because otherwise C++ has no idea what to use as the entry point... that is, where to start executing.
An interesting contrast is assembly language... which is very close (has a 1-to-1 relationship with) machine language. The first assembly-language or machine instruction is where the program starts: just start executing the first line!
But higher level languages, including C++, C#, Java, Basic, LISP, etc., do not work that way. The reason is that in a higher level language, there is a certain amount of overhead... even for C and C++... that must be set up so that the rest of the program can correctly execute. Stack area and heap area must be set aside, certain variables associated with the run-time library must be initialized, and other stuff must be set up that normally you don't have to think about.
Furthermore, the syntax of higher-level languages usually would make things too sloppy to just have executable statements standing outside of function or procedure definitions... although very old versions of Basic (BASICA) for many years did just that!!
So where does a program begin executing? Another solution, I suppose, would be for the program to just start executing with the first function that appears in the program -- the one nearest to line 1.
But there is an exellent reason C++ (and other languages) do not do that. The reason is: some programs may have multiple modules... meaning that the program consists of code from multiple source files, compiled and then linked together.
In that case, which function in which module should begin executing first? Again, C++ would have no way to know. The easiest solution by far is just to use the reserved word "main" to always mean the entry point for a stand-alone executable program. And so that's what C and C++ do.
== Brian Overland
Anthony Aj Williams
Joined: Jun 10, 2011
Brian Overland wrote:Generally, you are going to need a main() in an freestanding program (such as an EXE file in Windows or old MS-DOS)... otherwise, there is no entry point.
I think you are using "freestanding" here in a different sense to that used in the standard. A "freestanding implementation" is essentially one without an OS, and with a limited standard library. The standard specifies fewer constraints on freestanding implementations. Most compilers that target "normal" operating systems like Windows and MacOS are "hosted implementations", which provide a full standard library.
Brian Overland wrote:However, it strikes me that certain kinds of executables -- specifically DLLs or Windows programs -- may not have a main() function. That is because their entry points are through other means. A Windows program, for example, is in some ways more like a DLL (dynamic-link library) than a freestanding execution. Windows will call the program through its own special entry point that it recognizes (WinMain, not predefined in C or C++ itself).
DLLs are not covered by the C or C++ standards, and as such platforms can provide their own rules for them.
On Windows, you can write a main function if you like, but the compiler will probably build you a console app rather than a GUI app. The entry point called by the OS is actually something else entirely, and calls the constructors for global variables, and initializes the runtime library before calling the user-provided entry point.
The use of WinMain is a non-standard extension, but then if you're writing a Windows GUI you're already using non-portable facilities.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com