C++ No main() function?
Asked Answered
M

5

15

I'm a graduate programmer and when it comes to C++ I expect there to be a main() function everytime.

However I've been given a project written in C++ with Visual Studio 6.0 and it doesn't have a main() function. I really can't figure out how this program executes or where it begins.

I have seen examples of the Macro that can be used to change the name of the main() function, however this code shows no sign of that practice.

Can anyone suggest what I should be looking for?

Malayoindonesian answered 12/7, 2011 at 10:15 Comment(6)
You have to give a little bit more context. Windows programs have a "winmain" function, so no main, for example.Eyler
thanks for your quick reply. I believe this is a windows application. Using the Microsoft foundation Classes. If a class is extending from CWinApp, will this be seen as the entry point by the compiler?Malayoindonesian
Are you sure this is not a lib or dll project?Gaivn
Erm, well I'm not sure to be honest. It does output an executable file so I assumed it was a program. No winmain function neither.Malayoindonesian
I'm afraid it doesn't contain a WinMain() function. There's a class with the same name as the project, and it extends CWinApp so I'm wondering if the program initialises the class. But like I said I 've never seen anything like this before. There is also a MainFrame class in MainFrm.cpp, I believe this is a generated form??Malayoindonesian
If you are using Visual Studio, you can set a breakpoint at whereever you think the program could start at (e.g. the constructor of the class inheriting from CWinApp) and work your way up through the stack trace. Does not always help but could do for your case.Miyamoto
H
14

Maybe the main function is in a library, and the program startes with a virtual function call on a static object. That's what happens in MFC-applications.

The program derives a class from CWinApp and instanciates it once as a static variable. MFC then knows a pointer (that was set up by the constructor of CWinApp, and calls the virtual function InitInstance() on that pointer.

See, here's where the software from the program takes over...

Heptane answered 12/7, 2011 at 10:19 Comment(5)
This does seem to be how the program is acting. I have a class with the same name as the project and it extends from CWinApp. Also in the CPP file it creates a variable of itself. Is it safe to assume the constructor of this class is the entry point? (replacing main()?))Malayoindonesian
This is a implementation dependant solution, because I know of linkers that refuse to look for entrypoints in libraries. Not portable!Heptane
No!!!! Member functions can not be entrypoints, because there is no object to start with.Heptane
CWinApp::CWinApp() is called and it stores a pointer to itself, so that MFC-main function knows where the user created the derived class' object.Heptane
Yeah that makes perfect sense. Thanks for your time, going to read up some more on this WinMFC. Thank you.Malayoindonesian
H
14

First, it can be a library, DLL or static library and not have a main(). And second, it can be a windows application which has another entry point, such as WinMain(). Also in Visual Studio there is a _tmain() function, or other, which is a kind of a wrapper around main()

Heterogeneous answered 12/7, 2011 at 10:17 Comment(0)
H
14

Maybe the main function is in a library, and the program startes with a virtual function call on a static object. That's what happens in MFC-applications.

The program derives a class from CWinApp and instanciates it once as a static variable. MFC then knows a pointer (that was set up by the constructor of CWinApp, and calls the virtual function InitInstance() on that pointer.

See, here's where the software from the program takes over...

Heptane answered 12/7, 2011 at 10:19 Comment(5)
This does seem to be how the program is acting. I have a class with the same name as the project and it extends from CWinApp. Also in the CPP file it creates a variable of itself. Is it safe to assume the constructor of this class is the entry point? (replacing main()?))Malayoindonesian
This is a implementation dependant solution, because I know of linkers that refuse to look for entrypoints in libraries. Not portable!Heptane
No!!!! Member functions can not be entrypoints, because there is no object to start with.Heptane
CWinApp::CWinApp() is called and it stores a pointer to itself, so that MFC-main function knows where the user created the derived class' object.Heptane
Yeah that makes perfect sense. Thanks for your time, going to read up some more on this WinMFC. Thank you.Malayoindonesian
C
3

Although the standard states that program must have a main function, this does not have to written by the application developer if the libraries that are linked to have the main function defined in them. Also, some linkers allow you to redefine the entry point to something other than main.

Is there a WinMain at all?

Changchun answered 12/7, 2011 at 10:18 Comment(0)
S
2

If you are writing Win console application - you need a main() but if you are dealing with DLL, you are expecting a DllMain() for window application, you should expect an WinMain()

Sculpin answered 12/7, 2011 at 10:38 Comment(0)
H
1

Just for information, since this is not a general case, in C (and I suppose in C++) there could be programs without a main function. (I worked with one some years ago). Granted it was a embedded programming environment, not windows.

The trick was that the start up code in assembler called a completely different function after finished executing.

So even our teachers taught us about there is always main in C/C++ that is not an absolute truth

Harper answered 2/7, 2018 at 5:39 Comment(1)
The terminology used by the Standard is that in a freestanding implementation the entry point could be anything; in a hosted implementation the entry point must be main. Although Windows compilers tend to behave like hosted implementations in every way other than having different entry points!Scrummage

© 2022 - 2024 — McMap. All rights reserved.