Who calls main()
-
Hello, I am C,C++/VC++ developer. I wanted to know, as in every C/C++ program there is startup function main(), who calls this function. I know OS, but I want some in detail answer. Also I wanted to know how my program actually starts, means how it is loaded in memory, who loads it and etc.. Thanks in advance. Regards
-
Hello, I am C,C++/VC++ developer. I wanted to know, as in every C/C++ program there is startup function main(), who calls this function. I know OS, but I want some in detail answer. Also I wanted to know how my program actually starts, means how it is loaded in memory, who loads it and etc.. Thanks in advance. Regards
This question can only be answered by looking at a specific OS. In general the OS starts the respective runtime which in turn calls into
main
after doing some initialization work. If you want to know the exact call stack for YOUR system, just hold on in the properly configured debugger at some point of the program and trace back to the roots... Use breakpoints on visible global objects with non-trivial constructors to see what's happening before the program entersmain
. At least on Windows you will be able to trace back the runtime-code as well. Cheers, Paul -
Hello, I am C,C++/VC++ developer. I wanted to know, as in every C/C++ program there is startup function main(), who calls this function. I know OS, but I want some in detail answer. Also I wanted to know how my program actually starts, means how it is loaded in memory, who loads it and etc.. Thanks in advance. Regards
The operating system takes care of loading an executable file into memory, and then starts executing it at a specific point in the code, known as the "entry point". It has a special field in the EXE file format, which obviously gets filled by the linker from the information it got from the compiler. The entry point's name may vary, in C it normally is called main, and sometimes WinMain. In other languages it may be called differently, it is basically a convention between the programmer and the compiler. For Windows, you can get the EXE file format specification here[^]. That will keep you busy for a while! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
The operating system takes care of loading an executable file into memory, and then starts executing it at a specific point in the code, known as the "entry point". It has a special field in the EXE file format, which obviously gets filled by the linker from the information it got from the compiler. The entry point's name may vary, in C it normally is called main, and sometimes WinMain. In other languages it may be called differently, it is basically a convention between the programmer and the compiler. For Windows, you can get the EXE file format specification here[^]. That will keep you busy for a while! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
The entry point's name may vary, in C it normally is called main, and sometimes WinMain.
Not quite; these are the start points for the user part of the application, and are called by the initialisation routine, which is why
(Win)main()
has to have a specific signature. The actual entry point of the exe is much earlier and is part of the framework library that is automatically added by the linker.The best things in life are not things.
-
Luc Pattyn wrote:
The entry point's name may vary, in C it normally is called main, and sometimes WinMain.
Not quite; these are the start points for the user part of the application, and are called by the initialisation routine, which is why
(Win)main()
has to have a specific signature. The actual entry point of the exe is much earlier and is part of the framework library that is automatically added by the linker.The best things in life are not things.
You're quite right. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hello, I am C,C++/VC++ developer. I wanted to know, as in every C/C++ program there is startup function main(), who calls this function. I know OS, but I want some in detail answer. Also I wanted to know how my program actually starts, means how it is loaded in memory, who loads it and etc.. Thanks in advance. Regards