Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Who calls main()

Who calls main()

Scheduled Pinned Locked Moved C / C++ / MFC
c++performance
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    P L B 3 Replies Last reply
    0
    • L Lost User

      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

      P Offline
      P Offline
      Paul Michalik
      wrote on last edited by
      #2

      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 enters main. At least on Windows you will be able to trace back the runtime-code as well. Cheers, Paul

      1 Reply Last reply
      0
      • L Lost User

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • L Lost User

            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.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Lost User

              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

              B Offline
              B Offline
              bob16972
              wrote on last edited by
              #6

              The book "Linkers & Loaders" by John R. Levine is a good resource for questions involving this and similar subject matter.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups