Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What runs before main()?

Tags:

People also ask

What is executed before main?

Output. This is executed before main.

Is the main The first function to run?

The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main, although it can terminate at other points in the program for a variety of reasons.

What happens before main Linux?

The dynamic linker does lots of stuff before main when you run a program. The OS kernel also does a bunch. The entity that does "this stuff" is sometimes called "startup code". It sets things up, then calls main() , captures the return value from main() , and then does any cleanup.


After testing on msvc8, I found:

  1. Parse GetCommandLine() to argc and argv
  2. Standard C Library initialization
  3. C++ Constructor of global variables

These three things are called before entering main().

My questions are:

  1. Will this execution order be different when I porting my program to different compiler (gcc or armcc), or different platform?
  2. What stuff does Standard C Library initialization do? So far I know setlocale() is a must.
  3. Is it safe to call standard C functions inside C++ constructor of global variables?