Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you really need a main() in C++?

Tags:

c++

From what I can tell you can kick off all the action in a constructor when you create a global object. So do you really need a main() function in C++ or is it just legacy?

I can understand that it could be considered bad practice to do so. I'm just asking out of curiosity.

like image 914
onemasse Avatar asked Nov 21 '10 14:11

onemasse


People also ask

Is it compulsory to have main () in C?

No, the ISO C standard states that a main function is only required for a hosted environment (such as one with an underlying OS). For a freestanding environment like an embedded system (or an operating system itself), it's implementation defined.

Why we are using main () in C in?

A main() function is a user-defined function in C that means we can pass parameters to the main() function according to the requirement of a program. A main() function is used to invoke the programming code at the run time, not at the compile time of a program.

Can we run the C program without main method?

The answer is yes. We can write program, that has no main() function. In many places, we have seen that the main() is the entry point of a program execution. Just from the programmers perspective this is true.

Why is main () important?

The Importance of the main() Function in C Programming It's the core of every program. The “main ()” function indicates the beginning of a C++ program is executed, the control goes directly to the main() function. The statements within this function are the main body of the C++ program.


1 Answers

If you want to run your program on a hosted C++ implementation, you need a main function. That's just how things are defined. You can leave it empty if you want of course. On the technical side of things, the linker wants to resolve the main symbol that's used in the runtime library (which has no clue of your special intentions to omit it - it just still emits a call to it). If the Standard specified that main is optional, then of course implementations could come up with solutions, but that would need to happen in a parallel universe.

If you go with the "Execution starts in the constructor of my global object", beware that you set yourself up to many problems related to the order of constructions of namespace scope objects defined in different translation units (So what is the entry point? The answer is: You will have multiple entry points, and what entry point is executed first is unspecified!). In C++03 you aren't even guaranteed that cout is properly constructed (in C++0x you have a guarantee that it is, before any code tries to use it, as long as there is a preceeding include of <iostream>).

You don't have those problems and don't need to work around them (wich can be very tricky) if you properly start executing things in ::main.


As mentioned in the comments, there are however several systems that hide main from the user by having him tell the name of a class which is instantiated within main. This works similar to the following example

class MyApp { public:   MyApp(std::vector<std::string> const& argv);    int run() {       /* code comes here */       return 0;   }; };  IMPLEMENT_APP(MyApp); 

To the user of this system, it's completely hidden that there is a main function, but that macro would actually define such a main function as follows

#define IMPLEMENT_APP(AppClass) \   int main(int argc, char **argv) { \     AppClass m(std::vector<std::string>(argv, argv + argc)); \     return m.run(); \   } 

This doesn't have the problem of unspecified order of construction mentioned above. The benefit of them is that they work with different forms of higher level entry points. For example, Windows GUI programs start up in a WinMain function - IMPLEMENT_APP could then define such a function instead on that platform.

like image 192
Johannes Schaub - litb Avatar answered Sep 23 '22 18:09

Johannes Schaub - litb