What happens before main in C++?

前端 未结 3 1068
梦谈多话
梦谈多话 2020-12-06 07:34

I know in C, before application can get started in main(), some entity must:

  1. Initialize Global variables
  2. Set the stack pointer to the lo
3条回答
  •  有刺的猬
    2020-12-06 08:23

    Question 1- What's that entity that does this stuff? Who writes it?

    The C compiler team writes it.

    What happens is OS specific but basically it does things like deal with command line arguments, open/connect stdin, stdout, stderr, etc..

    If you dig around the gcc or clang source I'm sure you can find the code1. You can pass in options to the linker to not include this code if you program doesn't need it. For example if you don't read arguments nor use files and you want to do whatever other setup yourself you can pass in arguments not to include the startup code.

    Question 2- Are there additional things in C++?

    Yes, there's no such thing as constructors and destructors in C so if nothing else C++ has to deal with those.

    1: here's an example

提交回复
热议问题