The entry function in Linux main

Source: Internet
Author: User

Main () function, presumably everyone is not strange, from the beginning of the program, we began to write main (), we all know that main is the entrance of the program. That main as a function, and who calls it, how it is called, returned to whom, what is returned? This time, let's talk about the problem.


1. Form of the main () function
First of all, the definition of the main function, the early start of the C program must have used the definition of void main () {}, in fact, the C + + standard, never defined void main ().
There are only two definitions of main in the C standard:
int main (void)
int main (int argc, char *argv[])
There are only two definitions of main in the C + + standard:
int main ()
int main (int argc, char *argv[])

In other words: When your program does not need command line arguments with int main (), use int main (int argc, char *argv[] When command line arguments are required)

However, standard standards, on different platforms, different compilers in the definition of main () always have its own implementation, such as the early compiler to void Main () support (now GCC also support, but will give a warning). In particular, because of historical reasons, on the Unix-like platform, most also support
int main (int argc, char *argv[], char *envp[])
How to use it we'll talk about it later.

2. Return of the main () function
int main (...) means that you need to return an int value, and if you don't write it, some compiler will automatically add a return 0, and some will return a random value. To avoid unnecessary problems, it is recommended that you add a return 0 when you write it, but you can't waste much time, can you?
So a complete test.c file should be:
int main (int argc, char *argv[])
{
return 0;
}
Of course we can also try to have main return a long, double or even struct, changing the parameter definition in the main function. This can be compiled on some compilers, but there may be some warnings (such as GCC). But when running, if the compiler can do a good job of conversion, such as return long,float. If not, such as returning a struct, or main (int argc, char *argv0,char *argv1,char *argv2), will cause segmentation fault.


3. Call and return of main ()
After understanding the definition and return form of the main () function, let's take a look at how the main function is called and "return" to whom. In the "GCC compilation process", we reviewed the process from the source code to the executable program, and in the article "How the application is executed on Linux," we reviewed how the executable was loaded by the operating system, and today we continue this process.
As mentioned above, either in Load_elf_binary () or using a dynamic-link library, the application's entry is finally executed. But this entrance is not main. but _start ().
Perform
Gcc-o Test test.c
Readelf-a Test
You can see that the entry point address of the test file is 0x80482e0, and in the future, this address is the address of. Text (the beginning of the code snippet) and the address of the _start (). In the _start () will call __libc_start_main (), mainly do some initialization of the program, interested students can read glibc in the source code, the comments are very clear. Then the protagonist comes in, and at the end of the __libc_start_main () call
int result = Main (argc, argv, __environ main_auxvec_param);//This is the invocation of the main function under Unix-like, so we understand the origin of the formal parameters in the main function.
The return value of the main function is placed in result and exits with this value.
Exit (result);

Note: Although the main function is a special function, it is the entry for the program to run, but it is also a function that can be called. Such as:

int    Main ()      {          if(...)               return   0 ;          Main ();           return   0 ;      }  


However, be careful to invoke the method, and exit the condition to avoid infinite recursion.

4. Executing programs in shell
Through the previous and above analysis, we finally have a basic understanding of the implementation of the application process, and then again: in an interactive shell-typed./test, this shell fork ()/clone () out of a child process that executes

Execve ("./test", char * const argv[], char * const envp[])

Execve load./test, and take the parameters argv[],envp[] step-by-step pass down. After loading./test, start execution from the entrance to the./test, which is _start () in the Elf file, _start () calls __libc_start_main (), and finally to main.

int main (int argc, char *argv[], char *envp[])

Look at this. The definition of main is similar to the EXECVE, yes, the parameters in main are execve step-by-step. ARGC is the number of command-line arguments, argv[] A pointer to each parameter (note argv[0] is usually the program name, Argv[1] Start is the command-line argument. This is set by the shell), envp[] stores the environment variable table. However, only int main (int argc, char *argv[]) is defined in standard C, so the Unix-like platform also provides a global variable environ to the environment variable table.
extern char **environ;
Of course, you can also use getenv and putenv to access specific environment variables.

Yes, the parent shell is still waiting ()./test, yes, the value of the return of the main function in test, after being __libc_start_main () exit, is finally caught by the parent shell and can be accessed with $.
such as $>./test
$> echo $?
You can get the value returned by test. In this way, you know the meaning of return in the main () function and how to use it in the shell. Although it is possible to return any value, it is also recommended that the program end gracefully with a return of. So when someone calls your program with a shell script, it can be $? equals zero to determine if your program is executing properly.

Finally, summarize:
1. Avoid using void main (), use int main () or int main (int argc, char *argv[]) as much as possible.
2. Remember to return int at the end of main, preferably with return 0, to indicate the normal end of the program.
3. The main function, like a normal function, can also be called.
4. The value of main return will eventually be returned to its caller, such as a program executed in the shell, which can be used in the shell to get its return value.
5. In the Unix-like environment, you can use int main (int argc, char *argv[], Char *envp[]), extern char **environ; , getenv () and other ways to get environment variables.

The entry function in Linux main

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.